Build & Deploy a Jetty based app using Oracle Cloud
This blog demonstrates the following
- Develop a Java application to expose REST and Websocket services on top of Jetty (in embedded mode)
- Run it on Application Container Cloud
- Continuous integration and delivery using Developer Cloud
Jetty ?
For those who are not familiar with Jetty, it is fundamentally,
- A Web (HTTP) server, and
- A Servlet compliant container
Additionally, one can use it to carve out a custom container since it’s modular nature allows you to plug-in implementations for most of the Java EE Web Profile specifications (e.g. Websocket, CDI, JPA etc.). Details in the documentation
About the sample application
The application is similar to what I had used in a previous blog — keeps track of stock prices of NYSE scrips and exposes them using REST & Websocket endpoints
The important difference is that in this example stock prices are not tracked & broadcasted using EJB + CDI events
combo — instead, a (plain Java SE based)ScheduledExecutorService
is used, which
- uses a background task thread (Runnable) to fetch the price at regular intervals, and
- sends them to each of the connected Websocket clients
The application is available here. Lets explore some of its specific areas
Setting up required containers/runtimes
Let’s look at how Jetty is used to provide REST as well as Websocket based functionality
- JAX-RS container: This application leverages the pluggable Jetty (Servlet) module for Jersey i.e. Jetty Servlet container serves as a runtime for the Jersey (JAX-RS) implementation
- Websocket container: Jetty provides a first class implementation for the Java Websocket API (JSR 356) — thus, its possible leverage that without having to plugin any other implementation (e.g. Tyrus)
Here is a snippet from the pom.xml which shows the required dependencies
Bootstrapping respective containers
Websocket based broadcast
It is implemented in an asynchronous fashion
Setup & deployment
You would need to configure Oracle Developer Cloud for the Continuous Build as well as Deployment process. You can refer to previous blogs for the same (some of the details specific to this example will be highlighted here)
References
- Bootstrap the application in Oracle Developer Cloud — Project & code repository creation
- Continuous integration setup — Configure build job
- Deployment setup — Continuous Deployment (CD) to Oracle Application Container Cloud
Provide Oracle Application Container Cloud (configuration) descriptor
Deployment confirmation in Oracle Developer Cloud
Post-deployment status in Oracle Application Container Cloud
Test the application
You can either look up the price of a scrip (on-demand) using the REST endpoint or stream ORCL price by hooking on to the Websocket endpoint — both options have been summarized below
- Specific stock price: just issue a GET request on the specified URL e.g.
https://<ACCS_APP_URL>/mystocks/api/stocks?ticker=AAPL
- Subscribe to real time feed: use a WebSocket client and access specified endpoint URL e.g.
wss://<ACCS_APP_URL>/mystocks/rt/stocks
I would recommend using Simple WebSocket Client, which can be installed into Chrome browser as a plugin
Test the CI/CD flow
Make some code changes and push them to the Oracle Developer Cloud service Git repo. This should
- Automatically trigger the build, which once successful will
- initiate the deployment process, and
- redeploy the new application version to Oracle Application Container Cloud
Don’t forget to…
- check out the tutorials for Oracle Application Container Cloud — there is something for every runtime !
- other blogs on Application Container Cloud
The views expressed in this post are my own and do not necessarily reflect the views of Oracle.