Creating a Java Hello World Application on Oracle Application Container Cloud Service


Options



Before You Begin

Purpose

In this tutorial you learn how to create a simple REST service in Java using a Maven archetype, and how to generate the application archive to deploy your application on Oracle Application Container Cloud Service.

Time to Complete

Approximately 20 minutes

Background

Oracle Application Container Cloud Service includes Oracle Java SE Cloud Service, which lets you deploy Java applications to the Oracle Cloud.

For your application to run properly on Oracle Application Container Cloud Service, it must comply with the following requirements:

  • The application  must be self-contained. The application must include everything it needs to run independently.
  • Your application must listen to requests on a port provided by the PORT environment variable. Oracle Application Container Cloud uses this port to redirect requests made to your application.
  • The application  must include a manifest.json file. This file specifies information that Oracle Application Container Cloud Service requires to run your application properly. Optionally you can also specify additional information about instance scaling, environment variables, and service bindings in a deployment.json file.
  • The application must be bundled in a .zip, .tgz, or .tar.gz file. The application archive must include all the project dependencies and the manifest.json file at the root.

What Do You Need?

Creating the project

In this section, you create a java project using a Maven archetype. The jersey-quickstart-grizzly2 archetype creates a basic application of a REST service using Jersey that runs on top of a Grizzly container.

  1. Open a command-line window (or Terminal in Linux).

  2. Got to the directory where you want to store the project.

  3. Generate the Maven artifacts:

    mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example -DarchetypeVersion=2.25.1

Preparing the Application for Cloud Deployment

Updating the Main Class to Read the PORT Environment Variable

Open the Main class and update the BASE_URI variable to use the PORT environment variable and the 0.0.0.0 host name.

private static final Optional<String> port = Optional.ofNullable(System.getenv("PORT")); 
public static final String BASE_URI = "http://0.0.0.0:"+port.orElse("8080")+"/myapp/";

Note: You should add the import java.util.Optional;

Creating the manifest.json file

  1. Create the manifest.json file in your root directory project and add the following content:

    {
        "runtime":{
            "majorVersion": "8"
        },
        "command": "java -jar simple-service-1.0-SNAPSHOT-jar-with-dependencies.jar",
        "release": {
            "build": "20170113Build",
            "commit": "commitString",
            "version": "20170113Version"
        },
        "notes": "REST app for testing"
    }

Creating the Maven Script to Package the Application

  1. Create the assembly directory into the src folder.

  2. Create the distribution.xml file in the assembly folder and add the following content:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 
        http://maven.apache.org/xsd/assembly-1.1.3.xsd">
      <id>dist</id>
      <formats>
        <format>zip</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <fileSets>
        <fileSet>
          <directory>${project.basedir}</directory>
          <outputDirectory>/</outputDirectory>
          <includes>
            <include>manifest.json</include>
          </includes>
        </fileSet>
        <fileSet>
          <directory>${project.build.directory}</directory>
          <includes>
          	<include>*.jar</include>
          </includes>
          <excludes>
          	<exclude>*.zip</exclude>
          </excludes>
         </fileSet>
      </fileSets>
    </assembly>
  3. The distribution.xml script generates the simple-service-1.0-SNAPSHOT-dist.zip file in the target directory, that includes the fat jar(simple-service-1.0-SNAPSHOT-jar-with-dependencies.jar) and the manifest.json file.

  4. Open the pom.xml file in a text editor and add following code inside the <plugins> tags.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <!-- Build Executable Jar with all Dependencies -->
                <id>build-jar</id>
                <phase>package</phase>
                <goals>
                        <goal>single</goal>
                </goals>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>${project.artifactId}-${project.version}</finalName>
                    <archive>
                        <manifest>
                                <mainClass>com.example.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
            <execution>
                <!-- Build Archive Zip File -->
                <id>build-zip</id>
                <phase>package</phase>
                <goals>
                        <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/distribution.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>

Creating the Project Archive

In the command line window, build and package your application using the maven command:

mvn clean package

Deploying the Application to Oracle Application Container Cloud Service

In this section, you learn how to deploy your application on Oracle Container Cloud service using the user interface console.

  1. Log in to Oracle Cloud at http://cloud.oracle.com/. Enter the identity domain, user name, and password for your account.

    Oracle Cloud login page
    Description of this image
  2. In the Dashboard, click Instances to open the Oracle Application Container Cloud Service console.

    Instance of Oracle Application Container Cloud Service
    Description of this image
  3. In the Applications list view, click Create Application and select Java SE.

    Oracle Application Container Cloud Service home
    Description of this image
  4. In the Application section, enter a name for your application, select Upload application archive, and click Browse.

    Create Application dialog box
    Description of this image
  5. On the File Upload page, select the simple-service-1.0-SNAPSHOT-dist.zip file, and click Open.

    File Upload dialog box
    Description of this image
  6. Click Create.

    Create Application dialog box
    Description of this image
  7. When the confirmation dialog box is displayed, click OK.

    Confirmation dialog box
    Description of this image

    Your application could take a few minutes to deploy.

Testing Your Application

  1. In the Overview page, click the URL.

    Confirmation dialog box
    Description of this image
  2. Add to the end of the URL the service endpoint: myapp/myresource and press Enter.

    Confirmation dialog box
    Description of this image

The Got it! message means your service is running successful.

What to Learn More?