C2B2 logo icon

How to Create Microservices with WildFly Swarm

Andy Overton describes how to get two basic applications with Swarm functionality up-and-running

My colleague, Matt Brasier has written a couple of articles exploring Microservices and the theory behind them. In SOA vs Microservices, he looks at the principles involved and the differences in technologies and products, and in the second, Microservices – SOA and the Application Service Minibus, he continues his thoughts in an article that was originally written for Voxxed

His work on microservices made me think that there was room in this blog for a short technical demonstration that will get you up-and-running with microservices in no time at all. So for this walk-through, I'm going take a look at building microservices from scratch using WildFly Swarm - the open source product from Red Hat.

Note: Because I wrote this post earlier in the year, and Swarm has moved quickly, I'd advise that you take a look at the latest documentation to see current functionality. The version I used for this demo is 1.0.0 Beta7.

What is WildFly Swarm?

Typically, when developing Java EE applications, you create your application, package it as an EAR or WAR ,and then deploy it to an application server. Application Servers are designed to support all Java EE functionality, whether you use that functionality or not. 

JBoss WildFly (and its commercial counterpart, JBoss EAP) can be stripped down to remove some of the functionality, but Swarm turns things around. Instead of starting with all the functionality and removing parts, you start with a stripped down version of WildFly and add in the parts you require.

You then add your code on top and create a runnable ‘uber-jar’ containing WildFly (plus the dependencies you’ve added) and your compiled code.

Developing the application

So, enough about the theory, let’s see it in practice. WildFly Swarm requires JDK1.8 and Maven 3.2.1 or later, and I used the following:

  • JDK 1.8.0_73
  • Maven 3.3.9
  • Netbeans 8.1
  • Ubuntu 14.04

If you’re using Ubuntu you can get Maven 3 by following the instructions on this link.

I’m going to build two small applications, one will be a simple servlet, and the other will be a simple REST service that returns the Date and Time. The servlet will call the REST service and display the result.

Creating the servlet

First of all we will create the servlet. As I mentioned previously, I will be using NetBeans so things may be different if you’re using a different IDE but the code and Maven POM files will be the same.

Create a new project (Maven - Web Application) and give it a Project Name - DateTimeServlet

Now, in order to add in the Swarm functionality we need to edit the generated pom.xml, and to do this, we need a web server, so add in a dependency for Undertow:

<dependency>
	<groupId>org.wildfly.swarm</groupId>
	<artifactId>undertow</artifactId>
	<version>1.0.0.Beta5</version>
</dependency>

We will also need the Swarm plugin for packaging our uber-jar:

<plugin>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>wildfly-swarm-plugin</artifactId>
    <version>1.0.0.Beta6</version>
    <executions>
        <execution>
            <goals>
                <goal>package</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The latest versions of these can be found here, and as far as WildFly is concerned that’s all we require.

Create a new servlet under Source Packages. No need to alter the generated code at the moment. We’re done!

Build your project, and you should find that two archive files are generated:

  • DateTimeServlet-1.0-SNAPSHOT.war
  • DateTimeServlet-1.0-SNAPSHOT-swarm.jar

The first of these is a standard war file that can be deployed into any app server. The second is the swarm generated uber-jar containing WildFly with Undertow and our servlet code.

This can be run the same as any other standard runnable jar file:

java –jar DateTimeServlet-1.0-SNAPSHOT-swarm.jar

You should then see WildFly start up, with Undertow and the DateTimeServlet war file deployed.

Then, point your browser at http://localhost:8080/DateTimeServlet and you should see your servlet.

Creating the REST service

Create a new project (Maven - Web Application)
Project Name – DateTimeService

As before add the Swarm plugin to the pom:

<plugin>
	<groupId>org.wildfly.swarm</groupId>
	<artifactId>wildfly-swarm-plugin</artifactId>
	<version>1.0.0.Beta6</version>
	<executions>
		<execution>
			<goals>
				<goal>package</goal>
			</goals>
		</execution>
	</executions>
</plugin>

This time we will need support for REST services and this will be provided by JAX-RS. Add the JAX-RS dependency:

<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>jaxrs</artifactId>
    <version>1.0.0.Beta5</version>
</dependency>

Next we will create our application and service.
For our application we simply need to provide an implementation of javax.ws.rs.core.Application:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("resources")
public class DateTimeApplication extends Application 
{
    
}

For our service we will just create a very basic REST service that returns the current date.

import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/datetime")
public class DateTimeService
{
    @GET
    @Produces("text/plain")
    public String get() 
    {
        return "" + new Date();
    }
}

Build it and run the jar:

java –jar DateTimeService-1.0-SNAPSHOT-swarm.jar

Point your browser at http://localhost:8080/resources/datetime

You should see the result of calling your service i.e. the current date and time.

Linking the two

In order to call our REST service from our servlet we will need a REST API. In this case I’m going to use Jersey, the reference implementation of JAX-RS.

To do so, simply add in the dependency:

<dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-client</artifactId>
	<version>1.19.1</version>
</dependency>

Now, within our servlet we need to add a couple of imports:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

Then we alter our servlet code to call out to our service:

response.setContentType("text/html;charset=UTF-8");
        
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8081/resources/datetime");
        
String dateTime = resource.get(String.class);
        
try (PrintWriter out = response.getWriter())
{
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Current date and time: ");
    out.println(dateTime);
    out.println("</h1>");
    out.println("</body>");
    out.println("</html>");
}

Rebuild the code and we’re all done.

Now, run the Servlet jar in the same way as before:

java –jar DateTimeServlet-1.0-SNAPSHOT-swarm.jar

In order to also run our REST service on the same machine, we will need to run it on a different port from the default (8080). To do so, we can simply add a Java parameter (swarm.http.port) when we run it:

java -Dswarm.http.port=8081 –jar DateTimeService-1.0-SNAPSHOT-swarm.jar

Now, if we go to our servlet in a browser, we should see the results of our servlet calling out to the service:

Current date and time: Fri Apr 15 12:48:49 BST 2016

Conclusion

Hopefully that’s given you a flavour of what you can do with WildFly Swarm. It’s pretty straightforward to create basic applications and get them up and running in very little time. Swarm certainly looks promising, and I’ll be keeping a watchful eye to see where the project goes in the future.