Containers, Microservices, and NoSQL provide an awesome threesome for building your modern applications. These applications need to be agile, meet constantly evolving customer demands, be pervasive, and should work across mobile, web, and IoT platforms.

WildFly Swarm allows to package and run JavaEE applications by packaging them with just enough of the server runtime to java -jar your application. With built-in service discovery, single sign-on using Keycloak, monitoring using Hawkular, and many more features, WildFly Swarm provides all the necessary components to develop your microservice.

Docker for Mac provides native support for running Docker containers on Mac OSX. It relies upon Hypervisor.framework in OSX. Docker engine runs in an Alpine Linux distribution on top of an xhyve Virtual Machine, and even the VM is managed by Docker. There is no need for Docker Machine or VirtualBox, and it integrates with OSX security sandbox model. DockerCon 2016 removed the private beta restriction from Docker for Mac, and so it's available for everybody now.

It uses standard JAX-RS annotation to convert a POJO into a REST endpoint. The Couchbase Java API provides a fluent API and uses a N1QL statement to query the documents and return the results. The N1QL statement returns the first 10 elements from the query result. Learn more about N1QL syntax in this interactive tutorial. Database abstraction is defined as:

This is a singleton EJB that is eagerly initialized. It uses the Couchbase Java SDK to connect to Couchbase. Database endpoint can be specified using the COUCHBASE_URI environment variable. Next up is pom.xml for configuring the WildFly Swarm and Couchbase Java Client:

It uses a WildFly Swarm "bill of materials" to pull in all the dependencies. Only the specific dependencies needed for the build are specified in <dependencies>. These are then packaged in the "fat jar". WildFly Swarm Maven plugin is used to package and run the application:

This output shows that result of creating index was successful. One of the advantages of running Docker for Mac is that all the containers are accessible at localhost. This means Couchbase Web Console can be accessed at localhost:8091.

This screen ensures that Couchbase is configured correctly.

Run WildFly Swarm Microservice

Package and run the self-contained microservice as:

mvn wildfly-swarm:run

If Couchbase is running on a different host, then the command will change to:

So you built a simple microservice using WildFly Swarm accessing a Couchbase database running as a Docker container. Now, ideally this WildFly Swarm service should be packaged as a Docker image and then that Docker image would serve as the service. A Maven profile with the name docker is already added to pom.xml but issue #3 is making that scenario fail.