Run Java EE 8 apps on Oracle Cloud with Payara Micro
source link: https://medium.com/oracledevs/run-java-ee-8-apps-on-oracle-cloud-with-payara-micro-d9b527adaac9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Run Java EE 8 apps on Oracle Cloud with Payara Micro
This blog will demonstrate how to build and deploy a Java EE 8 microservice on Oracle Cloud
- Oracle Application Container Cloud is used as the runtime platform
- The application is in the form of a simple Java EE WAR file which is packaged along with Payara Micro 5 (alpha release) container (bring-your-own-runtime). More on the release announcement here
This makes it easy to leverage the platform enhancements in Java EE 8 and run cloud native, lightweight and scalable apps in the cloud
In case you haven’t heard of Payara Micro, you can start by exploring its website
Java EE 8 specifications
As with every platform release, Java EE 8 comprises of new specifications as well as enhancements to many of the existing components. The sample application in the blog demonstrates the following features in the below mentioned specifications
- Context and Dependency Injection (CDI) 2.0 — asynchronous events producer and consumer
- JAX-RS 2.1 — Server Sent Events feature to be specific
- JSON-B 1.0 (JSON Binding) integration with JAX-RS
Sample application
Here is a high level flow
- The service consists of a timer component which emits events at regular intervals.
- This is consumed by another component which makes the events available over Server Sent Events (SSE) — any (SSE) client (including your browser or a JavaScript app) can hook up to receive these
the project is available on Github for you to grok further
Let’s look at some of the implementation details
What’s going on ?
Here is a summary of the Java EE 8 specs and their respective components
Scheduled (event producer) component
- An
@ApplicationScoped
CDI bean produces asynchronous CDI events (new in CDI 2.0 with Java EE 8) — this is done usingEvent.fireAsync()
- The
ManagedExecutorService
(from the Java EE Concurrency utilities spec) thread pool is used to execute the timer/scheduler - These CDI events are qualified (using a custom
@Qualifier
)
Server Sent Events component
Exposes an endpoint for clients to register/subscribe to events
Broadcasting component
- Wrapper over a
javax.ws.rs.sse.SseBroadcaster
- Handles client registration and maintains active/connected clients
- Asynchronously receives CDI events (using
@ObservesAsync
) and broadcasts it to connected clients - the POJO to JSON (for the client) is handled by the inbuilt
JAX-RS
andJSON-B
integration
Build & deployment
Let’s build and push our application using the cloud
Build
git clone https://github.com/abhirockzz/accs-javaee8.git
mvn clean install
The build process creates the following artifacts in the target
directory
- WAR — our Java EE app
- JAR — this is the Payara Micro 5 container itself
- ZIP — this is the deployable artifact which consists of the the WAR and JAR (bring-your-own-runtime)
Deployment descriptors
A quick word on deployment descriptors — you can read the detailed documentation here (these are just metadata files)
- manifest.json — defines how our application will start. In this case we use the Payara Micro runtime to run our WAR file
{
“runtime”:{“majorVersion”:”8"},
“command”:”java -jar payara-micro.jar --deploy accs-javaee8.war --port $PORT”,
“notes”:”Java EE 8 application on ACCS”
}
- deployment.json — we define the topology of our application. In this case we are deploying 1 instance with 1 GB memory (you are free to choose the desired shape & size)
{
“instances”:1,
“memory”:”1G”
}
Time to deploy our Java EE 8 application to the cloud!
Push to cloud
With Oracle Application Container Cloud, you have multiple options in terms of deploying your applications. This blog will leverage PSM CLI which is a powerful command line interface for managing Oracle Cloud services
other deployment options include REST API, Oracle Developer Cloud and of course the console/UI
- Download and setup PSM CLI on your machine (using
psm setup
) — details here cd <code_directory>
psm accs push -n accsjavaee8 -r java -s hourly -m manifest.json -d deployment.json -p target/accs-javaee8-dist.zip
Once executed, an asynchronous process is kicked off and the CLI returns its Job ID for you to track the application creation
Check out the PSM CLI on Docker blog here
Check your application
Your application should be deployed on Oracle Application Container Cloud and its URL will be available for you to access it
Testing the application
Simple — use your browser to navigate to the application URL
e.g. https://accsjavaee8-<id_domain>.apaas.us2.oraclecloud.com/accs-javaee8
Please note that the URL will be different based on your identity domain
What you need to do next should be self explanatory :-)
You should see a continuous stream of (SSE) events — the (SSE) id is a (long) time stamp and (SSE) data is a JSON payload of the TickTock
POJO
That’s all for this blog..
Don’t forget to check out…
Further reading
Cheers!
The views expressed in this post are my own and do not necessarily reflect the views of Oracle.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK