Fixed the example linke error
Idempotent Consumer
The Idempotent Consumer
from the EIP patterns is used to filter out duplicate messages.
This pattern is implemented using the IdempotentConsumer
class. This uses an _expression_ to calculate a unique message ID string for a given message exchange; this ID can then be looked up in the MessageIdRepository
to see if it has been seen before; if it has the message is consumed; if its not then the message is processed and the ID is added to the repository.
The Idempotent Consumer essentially acts like a Message Filter to filter out duplicates.
Using the Fluent Builders
The following example will use the header myMessageId to filter out duplicates
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("seda:a").idempotentConsumer(header("myMessageId"), memoryMessageIdRepository(200))
.to("seda:b");
}
};
The above example
will use an in-memory based MessageIdRepository
which can easily run out of memory and doesn't work in a clustered environment. So you might prefer to use the JPA based implementation which uses a database to store the message IDs which have been processed
return new SpringRouteBuilder() {
public void configure() {
from("direct:start").idempotentConsumer(
header("messageId"),
jpaMessageIdRepository(bean(JpaTemplate.class), PROCESSOR_NAME)
).to("mock:result");
}
};
In the above example
we are using the header messageId to filter out duplicates and using the collection myProcessorName to indicate the Message ID Repository to use. This name is important as you could process the same message by many different processors; so each may require its own logical Message ID Repository.
Using the Spring XML Extensions
<bean id="myProcessor" class="org.apache.camel.builder.MyProcessor"/>
<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="seda:a"/>
<filter>
<xpath>$foo = 'bar'</xpath>
<process ref="myProcessor"/>
</filter>
</route>
</camelContext>
For further examples of this pattern in use you could look at the junit test case![]()
Using This Pattern
If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.