1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.processor.idempotent; |
19 |
|
|
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.Expression; |
22 |
|
import org.apache.camel.Processor; |
23 |
|
import org.apache.camel.impl.ServiceSupport; |
24 |
|
import org.apache.camel.util.ExpressionHelper; |
25 |
|
import org.apache.camel.util.ServiceHelper; |
26 |
|
import org.apache.commons.logging.Log; |
27 |
|
import org.apache.commons.logging.LogFactory; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class IdempotentConsumer extends ServiceSupport implements Processor { |
36 |
1 |
private static final transient Log log = LogFactory.getLog(IdempotentConsumer.class); |
37 |
|
private Expression<Exchange> messageIdExpression; |
38 |
|
private Processor nextProcessor; |
39 |
|
private MessageIdRepository messageIdRepository; |
40 |
|
|
41 |
2 |
public IdempotentConsumer(Expression<Exchange> messageIdExpression, MessageIdRepository messageIdRepository, Processor nextProcessor) { |
42 |
2 |
this.messageIdExpression = messageIdExpression; |
43 |
2 |
this.messageIdRepository = messageIdRepository; |
44 |
2 |
this.nextProcessor = nextProcessor; |
45 |
2 |
} |
46 |
|
|
47 |
|
@Override |
48 |
|
public String toString() { |
49 |
5 |
return "IdempotentConsumer[expression=" + messageIdExpression + ", repository=" + messageIdRepository + ", processor=" + nextProcessor + "]"; |
50 |
|
} |
51 |
|
|
52 |
|
public void process(Exchange exchange) throws Exception { |
53 |
6 |
String messageId = ExpressionHelper.evaluateAsString(messageIdExpression, exchange); |
54 |
6 |
if (messageId == null) { |
55 |
0 |
throw new NoMessageIdException(exchange, messageIdExpression); |
56 |
|
} |
57 |
6 |
if (!messageIdRepository.contains(messageId)) { |
58 |
3 |
nextProcessor.process(exchange); |
59 |
3 |
} |
60 |
|
else { |
61 |
3 |
onDuplicateMessage(exchange, messageId); |
62 |
|
} |
63 |
6 |
} |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public Expression<Exchange> getMessageIdExpression() { |
68 |
1 |
return messageIdExpression; |
69 |
|
} |
70 |
|
|
71 |
|
public MessageIdRepository getMessageIdRepository() { |
72 |
1 |
return messageIdRepository; |
73 |
|
} |
74 |
|
|
75 |
|
public Processor getNextProcessor() { |
76 |
1 |
return nextProcessor; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
protected void doStart() throws Exception { |
84 |
1 |
ServiceHelper.startServices(nextProcessor); |
85 |
1 |
} |
86 |
|
|
87 |
|
protected void doStop() throws Exception { |
88 |
1 |
ServiceHelper.stopServices(nextProcessor); |
89 |
1 |
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
protected void onDuplicateMessage(Exchange exchange, String messageId) { |
98 |
3 |
if (log.isDebugEnabled()) { |
99 |
0 |
log.debug("Ignoring duplicate message with id: " + messageId + " for exchange: " + exchange); |
100 |
|
} |
101 |
3 |
} |
102 |
|
} |