Over a million developers have joined DZone.

Microservices With Wildfly Swarm and Infinispan

Sebastian Laskawiec provides quick and easy guide to writing microservices in Java with the Wildfly Swarm server and Infinispan.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and dPaaS could cost you down the road. Brought to you in partnership with Liaison Technologies.

Everybody loves microservices, right? Today, all of us has a slightly different understanding of what microservices are. However, among all of those different definitions and attributes, there's probably one thing that fits them all, and that is that they need to be simple.

So, let's have a look at some practical examples of creating a REST service with Infinispan as a storage wired together using CDI. We will be using Wildfly Swarm as a running platform.

Bootstrapping a New Project

A good way to start a new Wildfly Swarm project is to generate it. The only requirement here is that you add "JAX-RS with CDI" and "JPA" as dependencies.

The next step is to add the infinispan-embedded artifact. The final pom.xml should look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.infinispan</groupId>
    <artifactId>wildfly-swarm-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <version.infinispan>8.2.4.Final</version.infinispan>
        <version.wildfly.swarm>2016.9</version.wildfly.swarm>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.wildfly.swarm}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
              <groupId>org.infinispan</groupId>
                <artifactId>infinispan-bom</artifactId>
                <version>${version.infinispan}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>wildfly-swarm-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>wildfly-swarm-plugin</artifactId>
                <version>${version.wildfly.swarm}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Java EE 7 dependency -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.infinispan</groupId>
            <artifactId>infinispan-embedded</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jaxrs-cdi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jpa</artifactId>
        </dependency>
    </dependencies>
</project>

Next, the Infinispan CDI Extension will take care of bootstrapping Infinispan. This means that we can dive directly into JAX-RS code. It should look like the following:

package com.example.configuration;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class RESTConfiguration extends Application {
}
package com.example.rest;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.infinispan.Cache;
@Path("/infinispan")
public class InfinispanEndpoint {
   @Inject
  Cache<String, String> cache;
   @GET
   @Path("cache")
   public Response printContent() {
      return Response.ok(cache.keySet().toString()).build();
   }
   @POST
   @Path("cache")
   public Response addSomethingToTheCache(String text) {
      cache.put(text, text);
      return Response.created(null).build();
   }
}

And that's it!

What's Next?

If you would like to have a look at the complete example, then you should check out my repository. The code is based on a fresh build from Infinispan master branch that contains a lot of improvements for CDI. You might build it yourself or just wait for 9.0.0.Beta1.

Discover the unprecedented possibilities and challenges, created by today’s fast paced data climate and why your current integration solution is not enough, brought to you in partnership with Liaison Technologies.

Topics:
infinispan ,wildfly swarm ,integration ,microservices

Published at DZone with permission of Sebastian Laskawiec. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}