DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. 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 · Tutorial
Like (4)
Save
Tweet
Share
4.39K 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

  • Top Five Tools for AI-based Test Automation
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Distributed SQL: An Alternative to Database Sharding
  • Using QuestDB to Collect Infrastructure Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: