1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.inbound; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.umo.MessagingException; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.routing.RoutingException; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class IdempotentReceiver extends SelectiveConsumer |
27 | |
{ |
28 | |
protected volatile IdempotentMessageIdStore idStore; |
29 | |
protected volatile String assignedComponentName; |
30 | |
|
31 | |
|
32 | |
|
33 | 2 | protected volatile int maxMessages = -1; |
34 | |
|
35 | |
|
36 | |
|
37 | 2 | protected volatile int messageTTL = (60 * 5); |
38 | |
|
39 | |
|
40 | |
|
41 | 2 | protected volatile int expirationInterval = 60; |
42 | |
|
43 | |
public IdempotentReceiver() |
44 | |
{ |
45 | 2 | super(); |
46 | 2 | } |
47 | |
|
48 | |
public int getMaxMessages() |
49 | |
{ |
50 | 0 | return maxMessages; |
51 | |
} |
52 | |
|
53 | |
public void setMaxMessages(int maxMessages) |
54 | |
{ |
55 | 0 | this.maxMessages = maxMessages; |
56 | 0 | } |
57 | |
|
58 | |
public int getMessageTTL() |
59 | |
{ |
60 | 0 | return messageTTL; |
61 | |
} |
62 | |
|
63 | |
public void setMessageTTL(int messageTTL) |
64 | |
{ |
65 | 0 | this.messageTTL = messageTTL; |
66 | 0 | } |
67 | |
|
68 | |
public int getExpirationInterval() |
69 | |
{ |
70 | 0 | return expirationInterval; |
71 | |
} |
72 | |
|
73 | |
public void setExpirationInterval(int expirationInterval) |
74 | |
{ |
75 | 0 | if (expirationInterval <= 0) |
76 | |
{ |
77 | 0 | throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("expirationInterval", |
78 | |
new Integer(expirationInterval)).toString()); |
79 | |
} |
80 | |
|
81 | 0 | this.expirationInterval = expirationInterval; |
82 | 0 | } |
83 | |
|
84 | |
protected void initialize(UMOEvent event) throws RoutingException |
85 | |
{ |
86 | 2 | if (assignedComponentName == null && idStore == null) |
87 | |
{ |
88 | 2 | this.assignedComponentName = event.getComponent().getDescriptor().getName(); |
89 | 2 | this.idStore = this.createMessageIdStore(); |
90 | |
} |
91 | 2 | } |
92 | |
|
93 | |
protected IdempotentMessageIdStore createMessageIdStore() |
94 | |
{ |
95 | 2 | return new IdempotentInMemoryMessageIdStore(assignedComponentName, maxMessages, messageTTL, |
96 | |
expirationInterval); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
public boolean isMatch(UMOEvent event) throws MessagingException |
101 | |
{ |
102 | 10 | if (idStore == null) |
103 | |
{ |
104 | |
|
105 | 2 | synchronized (this) |
106 | |
{ |
107 | 2 | this.initialize(event); |
108 | 2 | } |
109 | |
} |
110 | |
|
111 | |
try |
112 | |
{ |
113 | 10 | return !idStore.containsId(this.getIdForEvent(event)); |
114 | |
} |
115 | 0 | catch (Exception ex) |
116 | |
{ |
117 | 0 | throw new RoutingException(event.getMessage(), event.getEndpoint(), ex); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
|
122 | |
public UMOEvent[] process(UMOEvent event) throws MessagingException |
123 | |
{ |
124 | 4 | String eventComponentName = event.getComponent().getDescriptor().getName(); |
125 | 4 | if (!assignedComponentName.equals(eventComponentName)) |
126 | |
{ |
127 | 0 | IllegalArgumentException iex = new IllegalArgumentException( |
128 | |
"This receiver is assigned to component: " + assignedComponentName |
129 | |
+ " but has received an event for component: " + eventComponentName |
130 | |
+ ". Please check your config to make sure each component" |
131 | |
+ "has its own instance of IdempotentReceiver."); |
132 | 0 | throw new RoutingException(event.getMessage(), event.getEndpoint(), iex); |
133 | |
} |
134 | |
|
135 | 4 | Object id = this.getIdForEvent(event); |
136 | |
|
137 | |
try |
138 | |
{ |
139 | 4 | if (idStore.storeId(id)) |
140 | |
{ |
141 | 4 | return new UMOEvent[]{event}; |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | return null; |
146 | |
} |
147 | |
} |
148 | 0 | catch (Exception e) |
149 | |
{ |
150 | 0 | throw new RoutingException(CoreMessages.failedToWriteMessageToStore(id, assignedComponentName), |
151 | |
event.getMessage(), event.getEndpoint(), e); |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
protected Object getIdForEvent(UMOEvent event) throws MessagingException |
156 | |
{ |
157 | 14 | return event.getMessage().getUniqueId(); |
158 | |
} |
159 | |
|
160 | |
} |