Oracle Developers
Published in

Oracle Developers

Develop and deploy a Microprofile application on Oracle Cloud

This blog demonstrates how to get started with a simple application based on the Microprofile specification

About the sample application

The application is dead simple — available here. Before diving into its details, let’s look at Microprofile and Wildfly Swarm

Eclipse Microprofile … ?

Microprofile is an open source initiative whose goal is to provide an optimized runtime & platform for running Java based microservices. Here is an overview

  • It’ currently in the Incubation phase as a project under the Eclipse Foundation
  • There are multiple implementations of the specification — Wildfly Swarm, Payara, TomEE, WebSphere Liberty
  • Leverages existing and widely used Java EE specifications along with other technologies
  • As of now, the Microprofile supports JAX-RS, CDI and JSON-P specifications from the Java EE umbrella

Please check the following links for details — Microprofile on Eclipse, microprofile.io

Wildfly Swarm

This example uses Wildfly Swarm as an implementation of the Microprofile specification. You can read more about Wildfly Swarm here

Implementation details

In this section, we will explore how the application uses the supported specifications i.e. CDI, JAX-RS and JSON-P

  • JAX-RS: The JAX-RS resource exposes a REST endpoint to fetch stock prices — it’s actual implementation is realized by internal helper classes
  • JSON-P: Stock data (semi JSON payload) returned by the source (in this case, the Google Finance endpoint) is a cleaned up and then parsed by StockDataParser which uses the JSON-P API

CDI: It serves as the glue and helps in multiple ways

  • Exposes application classes as CDI beans — StockDataParser (@ApplicationScoped) and StockPriceRetriever (@Dependent)
  • Leverages JAX-RS & CDI integration point — designates a JAX-RS @Provider (the ExceptionMapper implementations) as @ApplicationScoped CDI beans — for more details please check section 10.2.3 of the JAX-RS specification
  • beans.xml enables all the CDI magic in the background

JAX-RS resource

@Path("stocks")
public class StockPriceResource {

@Inject
private StockPriceRetriever stock;

@GET
public Response getQuote(@QueryParam("ticker") final String ticker) {
return Response.ok(stock.getPrice(ticker)).build();
}

}

Application logic (CDI bean)

@Dependent
public class StockPriceRetriever {

@Inject
private StockDataParser parser;

public String getPrice(String ticker) {
String tick = null;

if (ticker == null) {
throw new InvalidTickerException();
}

Future<Response> promise = ClientBuilder.newClient().
target("https://www.google.com/finance/info?q=NASDAQ:" + ticker).
request().async().get();

Exception Mapper — yet another CDI bean

@Provider
@ApplicationScoped
public class InvalidTickerExceptionMapper implements ExceptionMapper<InvalidTickerException>{

public InvalidTickerExceptionMapper() {
}

@Override
public Response toResponse(InvalidTickerException e) {
return Response.status(400).entity(e.getMessage()).build(); //Bad Request
}

}

Dynamic port binding

When deployed to Oracle Application Container Cloud, the application dynamically binds to port exposed by the PORT environment variable

  • Wildfly Swarm exposes this as a system property — swarm.https.port
  • This in turn is used in the startup command within the manifest.json (which is a deployment descriptor)
{
"runtime":{"majorVersion":"8"},
"command":"java -jar -Dswarm.https.port=$PORT -Dswarm.context.path=/ accs-microprofile-swarm.jar",
"notes":"Sample Microprofile app on ACCS"
}

Setup

Oracle Developer Cloud

Let’s 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

Provide Oracle Application Container Cloud (configuration) descriptor

Deployment confirmation in Oracle Developer Cloud

Post-deployment status in Application Container Cloud

Test the application

Its super simple! Just pick a HTTP client — browser, Postman.. anything

Test the CI/CD flow

Make some code changes and push them to the Developer Cloud service Git repo. This should

  • Automatically trigger the build, which once successful will
  • Automatically trigger the deployment process, and
  • Redeploy the new application version to Oracle Application Container Cloud

Don’t forget to…

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Gupta

Abhishek Gupta

2K Followers

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