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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI

Clustered Idempotent Consumer Pattern with Infinispan

Bilgin Ibryam user avatar by
Bilgin Ibryam
·
Aug. 25, 14 · Interview
Like (0)
Save
Tweet
Share
4.93K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.
consumer Infinispan application clustered

Published at DZone with permission of Bilgin Ibryam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • Building a Robust Data Engineering Pipeline in the Streaming Media Industry: An Insider’s Perspective
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI

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

Let's be friends: