DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Microservices With Wildfly Swarm and Infinispan

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.

Sebastian Laskawiec user avatar by
Sebastian Laskawiec
·
Oct. 22, 16 · Integration Zone · Tutorial
Like (4)
Save
Tweet
4.27K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Infinispan microservice swarm WildFly

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Modify Java Command-Line Arguments
  • Revoking Access to JWTs With a Blacklist/Deny List
  • How to Build Security for Your SaaS User Communications
  • How to Make Git Forget a Tracked File Now in .gitignore

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo