About Bilgin Ibryam

Bilgin Ibryam is a senior software engineer based in London interested in service-oriented architecture, enterprise application integration and application development. He is also open source enthusiast, Apache Open for Business and Apache Camel committer.

Clustered Idempotent Consumer Pattern with Infinispan

I’ve created a small project that shows how to use JBoss Infinispan with Apache Camel and the Idempotent Consumer Pattern to guarantee a message will not be processed twice in a clustered environment.
Imagine you have an application that has to scale out easily by deploying it on multiple containers. But the application has to process each unique request only once across the cluster.
The solution is simple: use Idempotent Consumer Pattern in Camel with a repository that can scale out easily. This is where Infinispan comes into play. Infinispan is extremely scalable, highly available key/value store and data grid. If you use InfinispanIdempotentRepository with an idempotent consumer, it will create an in-memory cache to store the requests, and the moment you start another instance of the application, the cache instances will sync and the idempotent consumers in all applications will not process existing requests any longer.

IdempotentConsumer(2)
With this project (idempotent consumer demo5) you can start as many containers as you want, each container will start a rest endpoint on a new port starting from 8080 (http://localhost:8080/idempotent/KEY), and if you perform a GET request with a key, the subsequent requests with the same key to any other container will be rejected. Behind the scene Infinispan is replicating all the processed keys across the cluster of Camel applications and ensuring consistency.

The core of the application is the following route definition that finds a new free port number for each instance of the application:

public class IdempotentRoute extends RouteBuilder {
    private static final transient Logger LOGGER = LoggerFactory.getLogger(IdempotentRoute.class);

    private InfinispanIdempotentRepository infinispanRepo;
    private int port;

    @Override
    public void configure() throws Exception {
        from("restlet:http://localhost:" + port + "/idempotent/{key}?restletMethods=GET")

                .idempotentConsumer(header("key"), infinispanRepo)
                    .setBody(simple("UNIQUE REQUEST ACCEPTED: ${header.key}"))
                    .stop()
                 .end()

                .setBody(simple("REQUEST REJECTED: ${header.key}"));
    }

    public InfinispanIdempotentRepository getInfinispanRepo() {
        return infinispanRepo;
    }

    public void setInfinispanRepo(InfinispanIdempotentRepository infinispanRepo) {
        this.infinispanRepo = infinispanRepo;
    }

    public void start() {
         port = AvailablePortFinder.getNextAvailable(8080);
         LOGGER.info("Using port: " + port);
    }
}

Simple, isn’t it.

Reference: Clustered Idempotent Consumer Pattern with Infinispan from our JCG partner Bilgin Ibryam at the OFBIZian blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


nine + = 13



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close