Running Java EE 7 apps on Oracle Application Container Cloud

Abhishek Gupta
Oracle Developers
Published in
6 min readNov 14, 2017

This blog introduces the Java EE 7 runtime support in Oracle Application Container Cloud.

After a high level overview of the capability, we will look at a simple yet practical employee management application as an example

  • Its built using the classic Java EE components — JPA, EJB, JAX-RS, Servlet, CDI, JSP
  • Leverages Oracle Database Cloud service for persistence
  • You will also see the simplified integration with Oracle Database Cloud service — thanks to Service Bindings and on-the-fly data source creation

Java EE choices

The below diagram represents a spectrum of choices which you have in terms of Java EE on Oracle Cloud (at the time of writing)

Choice of Java EE on Oracle Cloud

Java EE support on Oracle Application Container Cloud

  • Java EE 7: Weblogic 12.2.1.x is used as the runtime (on Oracle JDK 8)
  • Hands-off: You’re not required to manage the Weblogic environment (patching etc.) — in fact you don’t have access to it (SSH, admin console etc.). This is in line with simplified, automated and managed approach of this feature
  • Cloud native, 12-factor style Java EE microservices — stateless, declarative dependency on external resources (DB, messaging layer etc.), externalized configuration (environment variables), CI/CD empowered etc.
  • Elastic — up/down (vertically), in/out (horizontally)
  • Integration: You can talk to different cloud services using service bindings — this includes Oracle Java Cloud, Oracle Database Cloud, Oracle Event Hub Cloud (Kafka), Oracle MySQL Cloud etc.

Sample app

The application is available on Github

Overview

The application is a revamped/reworked version of the existing Java SE based tutorial (employees app). It was packaged as an Uber JAR along with the runtime (Tomcat)

We don’t need to be religious about the bring-you-own-runtime mantra anymore, Java EE 7 (implemented by Weblogic) container is provided by Oracle Application Container Cloud — now, all you need is a (really) slim WAR file

lightweight Java EE WAR

Java EE specifications

To set the context, here are some of the details around which Java EE components were used

  • JPA is used instead of the DataSource APIs (and explicit JDBC code)
  • EJB is used as the facade layer to interact with the underlying data store (Oracle DB using JPA)
  • REST API provided using the de-facto JAX-RS standard
  • Servlet serves as the controller layer
  • CDI is used for Dependency Injection — injecting the EJB reference into the Servlet as well the JAX-RS endpoint
  • JSPs used as is (remain unchanged from the original application)

Accessing Oracle Database Cloud in your Java EE application

Its very simple to access Oracle Database Cloud (or Oracle MySQL Cloud) through your Java EE application. Typically, you would need to setup Data Sources in an app server vendor specific way — you don’t need to do that here!

All you need is to declare dependency on Oracle Database Cloud as a service binding in the deployment.json along with the JNDI name of the data source. This will create the required Weblogic data source on the fly — which is referenced in the JPA persistence.xml

deployment.json

{
“memory”: “2G”,
“instances”: 1,
“services”: [
{
“type”: “DBAAS”,
“name”: “OracleDBOnCloud”,
“username”: <DB user>,
“password”: <DB password>,
“properties”: {
“jndi-name”: “jdbc/dbcs”
}
}]
}

JPA persistence.xml

<?xml version=”1.0" encoding=”UTF-8" standalone=”no”?>
<persistence
xmlns=”http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" version=”2.1" xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name=”employees_PU” transaction-type=”JTA”>
<jta-data-source>jdbc/dbcs</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>

More details in the documentation

Build & deployment

Build

The build process will create employee.war in the target directory

Push to cloud

With Oracle Application Container Cloud, you have multiple options in terms of deploying your applications

  • PSM CLI — this is a powerful command line interface for managing Oracle Cloud services (we will be using this)
  • Console — you can use the UI to manage your applications (more in the documentation)
  • Oracle Developer Cloud Service: Complete CI/CD style. This has been explained in previous blogs and documentation is available here
  • REST API: You can use a cURL command to invoke a REST API to deploy your app. API details here

Download and setup PSM CLI on your machine (using psm setup) — details here

  • cd <code_directory>
  • modify the deployment.json to fill in the details (name, username, password and the jndi-name attributes) corresponding to your Oracle Database cloud service instance
  • psm accs push -n employEEs -r javaee -s hourly -d deployment.json -p target/employee.war

Once executed, an asynchronous process is kicked off and the CLI returns its Job ID for you to track the application creation

push to cloud using CLI

Check your application

Java EE app successfully deployed to Oracle Application Container Cloud

database binding..

.. this was discussed previously

Simplified database connectivity

Test drive the Java EE app

Pre-requisite: bootstrap the database

To connect to the Oracle Database instance on the cloud

Execute the below SQL scripts

  1. to bootstrap the database table
CREATE TABLE EMPLOYEE (
ID INTEGER NOT NULL,
FIRSTNAME VARCHAR(255),
LASTNAME VARCHAR(255),
EMAIL VARCHAR(255),
BIRTHDATE VARCHAR(10),
TITLE VARCHAR(255),
DEPARTMENT VARCHAR(255),
PRIMARY KEY (ID)
);

2. create the sequence for auto generation of primary keys

CREATE SEQUENCE EMPLOYEE_SEQ
START WITH 100
INCREMENT BY 1;

There are two options

  • using the UI
  • using the REST API directly

Enjoy some CRUD using the UI

Landing page
  • Start by creating a new employee
Employee details
created employee
  • create a few more….
list of all employees
  • searching is simple (and so is update and delete)

There is a REST API as well

  • Get all employees
curl -X GET https://employees-<domain>.apaas.us2.oraclecloud.com/rest/employees
  • get employee by ID
curl -X GET https://employees-<domain>.apaas.us2.oraclecloud.com/rest/employees/<id>
  • create a new employee
curl -X POST https://employees-ocloud109.apaas.us2.oraclecloud.com/rest/employees -H “content-type: application/xml” -d “<employee><firstName>Bob</firstName><lastName>Doe</lastName><birthDate>12–12–2007</birthDate><title>CTO</title><department>Engineering</department><email>bob@doe.com</email></employee>”
  • update an existing employee
curl -X PUT https://employees-ocloud109.apaas.us2.oraclecloud.com/rest/employees/<id> -H “content-type: application/xml” -d “<employee><firstName>Bob</firstName><lastName>Doe</lastName><birthDate>12–12–2007</birthDate><title>VP</title><department>Sales</department><email>bob@doe.com</email></employee>”
  • delete an existing employee
curl -X DELETE https://employees-<domain>.apaas.us2.oraclecloud.com/rest/employees/<id>

That’s it for this blog post!

Don’t forget to…

Cheers!

The views expressed in this post are my own and do not necessarily reflect the views of Oracle.

--

--

Abhishek Gupta
Oracle Developers

Principal Developer Advocate at AWS | I ❤️ Databases, Go, Kubernetes