Clustered Idempotent Consumer Pattern with Infinispan
Join the DZone community and get the full member experience.
Join For FreeI'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.
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.
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