Enterprise Java

WildFly Swarm: Building Microservices with Java EE

¨Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away¨ Antoine de Saint-Exupery

This quote by the French writer Antoine de Saint-Exupery was made to substantiate that often less is more. This is true for architect, artist, designer, writer, running, software developer, or in any other profession. Simplicity, minimalism, cutting down the cruft always goes a long way and has several advantages as opposed to something bloated.

What is WildFly Swarm?

WildFly is a light weight, flexible, feature rich, Java EE 7 compliant application server. WildFly 9 even introduced a 27MB Servlet-only distribution. These are very well suited for your enterprise and web applications.

WildFly Swarm takes the notch a bit higher. From the announcement:

WildFly Swarm is a new sidecar project supporting WildFly 9.x to enable deconstructing the WildFly AS and pasting just enough of it back together with your application to create a self-contained executable jar. WildFly Swarm

The typical application development model for a Java EE application is to create an EAR or WAR archive and deploy it in an application server. All the dependencies, such as Java EE implementations are packaged in the application server and provide the functionality required by the application classes. Multiple archives can be deployed and they all share the same libraries. This is a well understood model and have been used over the past several years.

WildFly Swarm turns the table where it creates a “fat jar” that has all the dependencies packaged in a JAR file. This includes a minimalist version of WildFly, any required dependencies, and of course, the application code itself. The application can simply be run using java -jar.

Each fat jar could possibly be a microservice which can then independently upgrade, replace, or scale. Each fat jar would typically follow single responsibility principle and thus will have only the required dependencies packaged. Each JAR can use polyglot persistence, and use only the persistence mechanism that is required.

Show me the code!

A Java EE application can be packaged as WildFly Swarm fat jar by adding a Maven dependency and a plugin. Complete source code for a simple JAX-RS sample is available at github.com/arun-gupta/wildfly-samples/tree/master/swarm.

WildFly Swarm Maven Dependency

Add the following Maven dependency in pom.xml:

<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>wildfly-swarm-jaxrs</artifactId>
    <version>${version.wildfly-swarm}</version>
    <scope>provided</scope>
</dependency>

WildFly Swarm Maven Plugin

Add the following Maven plugin in pom.xml:

<plugin>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>wildfly-swarm-plugin</artifactId>
    <version>${version.wildfly-swarm}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Create WildFly Swarm Fat Jar

The fat jar can be easily created by invoking the standard Maven target:

maven package

This generates a JAR file using the usual Maven conventions, and appends -swarm at the end. The generated WAR file name in our sample is swarm-1.0-SNAPSHOT-swarm.jar.

The generated WAR file is ~30MB, has 134 JARs (all in m2repo directory), and 211 classes. The application code is bundled in app/swarm-1.0-SNAPSHOT.war.

Run WildFly Swarm Fat Jar

This far jar can be run as:

swarm> java -jar target/swarm-1.0-SNAPSHOT-swarm.jar 
12:27:10,622 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.4.Final
12:27:10,739 INFO  [org.jboss.as] (MSC service thread 1-6) WFLYSRV0049: WildFly Core 1.0.0.CR1 "Kenny" starting
2015-05-06 12:27:11,185 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 11) WFLYSEC0002: Activating Security Subsystem
2015-05-06 12:27:11,189 INFO  [org.jboss.as.security] (MSC service thread 1-10) WFLYSEC0001: Current PicketBox version=4.9.0.Beta2
2015-05-06 12:27:11,194 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 13) WFLYIO001: Worker 'default' has auto-configured to 16 core threads with 128 task threads based on your 8 available processors
2015-05-06 12:27:11,199 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 12) WFLYNAM0001: Activating Naming Subsystem
2015-05-06 12:27:11,246 INFO  [org.jboss.as.naming] (MSC service thread 1-12) WFLYNAM0003: Starting Naming Service
2015-05-06 12:27:11,319 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0003: Undertow 1.2.4.Final starting
2015-05-06 12:27:11,319 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 14) WFLYUT0003: Undertow 1.2.4.Final starting
2015-05-06 12:27:11,337 INFO  [org.xnio] (MSC service thread 1-7) XNIO version 3.3.1.Final
2015-05-06 12:27:11,343 INFO  [org.xnio.nio] (MSC service thread 1-7) XNIO NIO Implementation Version 3.3.1.Final
2015-05-06 12:27:11,369 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-13) WFLYUT0012: Started server default-server.
2015-05-06 12:27:11,409 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTP listener default listening on /127.0.0.1:8080
2015-05-06 12:27:11,543 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Core 1.0.0.CR1 "Kenny" started in 855ms - Started 64 of 70 services (13 services are lazy, passive or on-demand)
2015-05-06 12:27:11,570 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0027: Starting deployment of "swarm-1.0-SNAPSHOT.war" (runtime-name: "swarm-1.0-SNAPSHOT.war")
2015-05-06 12:27:11,724 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) WFLYUT0018: Host default-host starting
2015-05-06 12:27:11,906 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-14) Deploying javax.ws.rs.core.Application: class org.wildfly.samples.swarm.MyApplication
2015-05-06 12:27:11,923 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-14) WFLYUT0021: Registered web context: /
2015-05-06 12:27:11,944 INFO  [org.jboss.as.server] (main) WFLYSRV0010: Deployed "swarm-1.0-SNAPSHOT.war" (runtime-name : "swarm-1.0-SNAPSHOT.war")

The response can be verified as:

target> curl -v http://127.0.0.1:8080/resources/resource
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /resources/resource HTTP/1.1
> User-Agent: curl/7.37.1
> Host: 127.0.0.1:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/octet-stream
< Content-Length: 12
< Date: Wed, 06 May 2015 19:29:10 GMT
< 
* Connection #0 to host 127.0.0.1 left intact
hello swarm!

WildFly Swarm Release blog refers to lots of blogs about Servlet, JAX-RS with ShrinkWrap, DataSource via Deployment, Messaging and JAX-RS, and much more.

WildFly Swarm Next Steps

This is only 1.0.0.Alpha1 release so feel free to try out samples and give us feedback by filing an issue.

You have the power of all WildFly subsystems, and can even create embeddable Java EE container as shown in the release blog:

public class Main {

    public static void main(String[] args) throws Exception {
        Container container = new Container();

        container.subsystem(new MessagingFraction()
                        .server(
                                new MessagingServer()
                                        .enableInVmConnector()
                                        .topic("my-topic")
                                        .queue("my-queue")
                        )
        );

        // Start the container
        container.start();

        JaxRsDeployment appDeployment = new JaxRsDeployment();
        appDeployment.addResource(MyResource.class);

        // Deploy your JAX-RS app
        container.deploy(appDeployment);

        // Create an MSC deployment
        ServiceDeployment deployment = new ServiceDeployment();
        deployment.addService(new MyService("/jms/topic/my-topic" ) );

        // Deploy the services
        container.deploy( deployment );
    }
}

Subsequent blogs will show how a microservice can be easily created using WildFly Swarm.

WildFly Swarm Stay Connected

You can keep up with the project through the WildFly HipChat room, @wildflyswarm on Twitter, or through GitHub Issues.

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button