Idempotent Consumer
来源:互联网 发布:windows 10重装系统 编辑:程序博客网 时间:2024/05/20 18:02
一、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 IdempotentRepository 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.
Camel will add the message id eagerly to the repository to detect duplication also for Exchanges currently in progress.
On completion Camel will remove the message id from the repository if the Exchange failed, otherwise it stays there.
Options
The Idempotent Consumer has the following options:
Using the Fluent Builders
The following example will use the header myMessageId to filter out duplicates
RouteBuilder builder = new
RouteBuilder() {
public
void configure() {
errorHandler(deadLetterChannel("mock:error"
));
from("seda:a"
) .idempotentConsumer(header("myMessageId"
),
MemoryIdempotentRepository.memoryIdempotentRepository(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
from("direct:start"
).idempotentConsumer(
header("messageId"
),
jpaMessageIdRepository(lookup(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.
For further examples of this pattern in use you could look at the junit test case
Spring XML example
The following example will use the header myMessageId to filter out duplicates
<camelContext xmlns="http://camel.apache.org/schema/spring"
>
<route>
<from uri="direct:start"
/>
<idempotentConsumer messageIdRepositoryRef="myRepo"
>
<!-- use the messageId header as key for identifying duplicate messages -->
<header>
messageId</header>
<!-- if not a duplicate send it to this mock endpoint -->
<to uri="mock:result"
/>
</idempotentConsumer>
</route>
</camelContext>
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.
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
二、
Contents of /camel/trunk/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerTest.java
Parent Directory| Revision Log
Fri Mar 26 08:03:00 2010 UTC(10 months ago)by davsclaus
File size: 6938 byte(s)
CAMEL-2576: Renamed redeliverDelay to redeliveryDelay.
二、
org.apache.camel.processor.idempotent
Class IdempotentConsumer
java.lang.Object
org.apache.camel.impl.ServiceSupport
org.apache.camel.processor.idempotent.IdempotentConsumer
- All Implemented Interfaces:
- Navigate<Processor>, Processor, Service
public class IdempotentConsumer
- extends ServiceSupport
- implements Processor
, Navigate
<Processor
>- extends ServiceSupport
An implementation of the Idempotent Consumer pattern.
- Version:
- $Revision: 835732 $
IdempotentConsumer(Expression messageIdExpression, IdempotentRepository<String> idempotentRepository, boolean eager, Processor processor)
protected void
doStart()
protected void
doStop()
IdempotentRepository<String>
getIdempotentRepository()
Expression
getMessageIdExpression()
Processor
getProcessor()
boolean
hasNext()
Are there more outputs?
List<Processor>
next()
Next group of outputs
protected void
onDuplicateMessage(Exchange exchange, String messageId)
A strategy method to allow derived classes to overload the behaviour of processing a duplicate message
void
process(Exchange exchange)
Processes the message exchange
String
toString()
addChildService, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, removeChildService, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
IdempotentConsumer
public IdempotentConsumer
(Expression
messageIdExpression,
IdempotentRepository
<String
> idempotentRepository,
boolean eager,
Processor
processor)
toString
public String
toString
()
- Overrides:
toString
in classObject
process
public void process
(Exchange
exchange)
throws Exception
- Description copied from interface:
Processor
- Processes the message exchange
- Specified by:
process
in interfaceProcessor
- Parameters:
exchange
- the message exchange- Throws:
Exception
- if an internal processing error has occurred.
next
public List
<Processor
> next
()
- Description copied from interface:
Navigate
- Next group of outputs
- Specified by:
next
in interfaceNavigate<Processor>
- Returns:
- next group or null if no more outputs
hasNext
public boolean hasNext
()
- Description copied from interface:
Navigate
- Are there more outputs?
- Specified by:
hasNext
in interfaceNavigate<Processor>
- Returns:
- true if more outputs
getMessageIdExpression
public Expression
getMessageIdExpression
()
getIdempotentRepository
public IdempotentRepository
<String
> getIdempotentRepository
()
getProcessor
public Processor
getProcessor
()
doStart
protected void doStart
()
throws Exception
- Specified by:
doStart
in classServiceSupport
- Throws:
Exception
doStop
protected void doStop
()
throws Exception
- Specified by:
doStop
in classServiceSupport
- Throws:
Exception
onDuplicateMessage
protected void onDuplicateMessage
(Exchange
exchange,
String
messageId)
- A strategy method to allow derived classes to overload the behaviour of processing a duplicate message
- Parameters:
exchange
- the exchangemessageId
- the message ID of this exchange
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------
三、rg.apache.camel.spi
Interface IdempotentRepository<E>
- All Known Implementing Classes:
- FileIdempotentRepository, MemoryIdempotentRepository
public interface IdempotentRepository<E>
Access to a repository of Message IDs to implement the Idempotent Consumer pattern.
The add and contains methods is operating according to the Set
contract.
- Version:
- $Revision: 782534 $
boolean
add(E key)
Adds the key to the repository.
boolean
confirm(E key)
Confirms the key, after the exchange has been processed sucesfully.
boolean
contains(E key)
Returns true if this repository contains the specified element.
boolean
remove(E key)
Removes the key from the repository.
add
boolean add
(E
key)
- Adds the key to the repository.
- Parameters:
key
- the key of the message for duplicate test- Returns:
- true if this repository did not already contain the specified element
contains
boolean contains
(E
key)
- Returns true if this repository contains the specified element.
- Parameters:
key
- the key of the message- Returns:
- true if this repository contains the specified element
remove
boolean remove
(E
key)
- Removes the key from the repository.
Is usually invoked if the exchange failed.
- Parameters:
key
- the key of the message for duplicate test- Returns:
- true if the key was removed
confirm
boolean confirm
(E
key) Confirms the key, after the exchange has been processed sucesfully.
Parameters:key
- the key of the message for duplicate test
Returns:
true
if the key was confirmed
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
四、Fluent Builders
Camel provides fluent builders for creating routing and mediation rules using a type-safe IDE friendly way which provides smart completion and is refactoring safe.
- Idempotent Consumer
- Idempotent Consumer 2
- Idempotent Consumer 3
- Consumer
- idempotent - 幂等
- ice 的 Nonmutating 和 Idempotent
- Ice 的 Nonmutating 和 Idempotent
- Codeforces 524C Idempotent functions
- Consumer与Consumer Group
- Using a JPA based idempotent repository
- interface/class 的 Nonmutating 和 Idempotent
- ICE编程中的 Nonmutating 和 Idempotent
- 【Codeforces】542C - Idempotent functions(思路)
- HDU Consumer
- Kombu Consumer
- producer consumer
- producer & consumer
- Consumer Prefetch
- DWZ富客户端框架小窥
- WINCE6.0+S3C2443下的activesync问题
- Hbase和Hive整合
- C#反射教程(3)
- C#反射教程(4)
- Idempotent Consumer
- VirtualBox NAT 端口映射(3.2.0版本以上)搭建web服务器
- 【喜报】老孙的博客获 ☆2010年度十大杰出IT博客☆ 殊荣
- Build Linux Kernel
- C# 去除文件和文件夹的只读属性
- 搭建flv可拖拽流媒体服务器方案之CentOS5.5+Lighttpd
- memcache 基于windw下安装
- 运行时库、标准库和系统api
- C#反射教程(5)