Difficulty: intermediate
Estimated Time: 20-30 minutes

What you will learn

In this scenario you will learn more about WildFly Swarm, one of the runtimes included in Red Hat OpenShift Application Runtimes.

You will take an existing sample WildFly Swarm application and modify it to address microservice concerns, understand its structure, deploy it to OpenShift and exercise the interfaces between WildFly Swarm apps, microservices, and OpenShift/Kubernetes.

What is WildFly Swarm?

Logo

Java EE applications are traditionally created as an ear or war archive including all dependencies and deployed in an application server. Multiple Java EE applications can and were typically deployed in the same application server. This model is well understood in the development teams and has been used over the past several years.

WildFly Swarm offers an innovative approach to packaging and running Java EE applications by packaging them with just enough of the Java EE server runtime to be able to run them directly on the JVM using java -jar For more details on various approaches to packaging Java applications, read this blog post.

WildFly Swarm is based on WildFly and it also implements Eclipse MicroProfile, which is a community-driven open source specification that optimizes Enterprise Java for a microservices architecture and delivers application portability across multiple MicroProfile runtimes.

Since WildFly Swarm is based on Java EE standards, it significantly simplifies refactoring existing Java EE application to microservices and allows much of existing code-base to be reused in the new services.

In this scenario you learned a bit more about what WildFly Swarm is, and how it can be used to create modern Java microservice-oriented applications.

You took an existing application and developed a new health check for it using WildFly Swarm, and deployed the application to OpenShift and tested it.

WildFly Swarm brings in a number of concepts and APIs from the Java EE community, so your existing Java EE skills can be re-used to bring your applications into the modern world of containers, microservices and cloud deployments.

WildFly Swarm is one of many components of Red Hat OpenShift Application Runtimes - check out other scenarios (for example the Vert.x scenario) to learn about other runtimes you can use.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Getting Started with WildFly Swarm on OpenShift

Step 1 of 7

Examine the sample project

The sample project shows the components of a WildFly Swarm project laid out in different subdirectories according to Maven best practices.

1. Run the following command to examine the Maven project structure.

Click on the tree command below to automatically copy it into the terminal and execute it

tree

This is a minimal Java EE project which implements a simple RESTful microservice which implements a greeting service (that simply returns a Hello greeting). Click the links below to open each file and inspect its contents:

  • pom.xml - Maven project file describing how to build the project
  • src/main/java/com/example/ApplicationConfig.java - Declares the RESTful resource base path and a simple switch to toggle the application's ability to serve requests
  • src/main/java/com/example/Greeting.java - POJO containing a message to send as a greeting
  • src/main/java/com/example/GreetingResource.java - Definition of the /greeting and /stop RESTful endpoints.

Review the content a bit and notice that there are some comments in the code. Do not remove them! The comments are used as marker and without them you will not be able finish the scenario.

The /greeting API returns a simple message. The /stop API will simulate the application failing. In a real application this could happen if the application was overloaded, or a bug in the code causes the application to hang or otherwise not be able to service requests. For our sample app, we use a simple boolean flag to simulate failure and will toggle this on and off later to simulate failure.

2. Compile and run the application

Before we add code to the project you should build and test that current application starts as it should.

Since this is already a working application you can already without any code changes run the application locally directly from maven using wildfly-swarm:run as the goal

mvn wildfly-swarm:run

Since this is the first time we run maven may have to pull down some dependencies (we have tried to prepopulate most for you), but subsequent runs should go really fast.

At this stage the application doesn't really do anything but after a while you will see:

INFO  [org.wildfly.swarm] (main) WFSWARM99999: WildFly Swarm is Ready

3. Test the application

To begin, click on the Local Web Browser tab in the console frame of this browser window. This will open another tab or window of your browser pointing to port 8080 on your client.

Local Web Browser Tab

or use this link.

You should now see a html page that looks like this

App

4. Stop the application

Before moving on, click in the terminal window and then press CTRL-C to stop the running application!

You should see this:

INFO  [org.jboss.as] (MSC service thread 1-3) WFLYSRV0050: WildFly Swarm 2017.10.0 (WildFly Core 2.2.1.Final) stopped in 51ms

This indicates the application is stopped.

Congratulations

You have now successfully executed the first step in this scenario.

Now you've seen how you with a few lines of code one can create a simple RESTful HTTP Server capable of serving static content using WildFly Swarm.

In next step of this scenario we will deploy our application to OpenShift Container Platform.

Terminal
Local Web Browser
OpenShift Console