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">
        <!-- Java EE 7 dependency -->

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;
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;
public class InfinispanEndpoint {
  Cache<String, String> cache;
   public Response printContent() {
      return Response.ok(cache.keySet().toString()).build();
   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.

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.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}