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 protected volatile int maxMessages = -1;
34
35
36
37 protected volatile int messageTTL = (60 * 5);
38
39
40
41 protected volatile int expirationInterval = 60;
42
43 public IdempotentReceiver()
44 {
45 super();
46 }
47
48 public int getMaxMessages()
49 {
50 return maxMessages;
51 }
52
53 public void setMaxMessages(int maxMessages)
54 {
55 this.maxMessages = maxMessages;
56 }
57
58 public int getMessageTTL()
59 {
60 return messageTTL;
61 }
62
63 public void setMessageTTL(int messageTTL)
64 {
65 this.messageTTL = messageTTL;
66 }
67
68 public int getExpirationInterval()
69 {
70 return expirationInterval;
71 }
72
73 public void setExpirationInterval(int expirationInterval)
74 {
75 if (expirationInterval <= 0)
76 {
77 throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("expirationInterval",
78 new Integer(expirationInterval)).toString());
79 }
80
81 this.expirationInterval = expirationInterval;
82 }
83
84 protected void initialize(UMOEvent event) throws RoutingException
85 {
86 if (assignedComponentName == null && idStore == null)
87 {
88 this.assignedComponentName = event.getComponent().getDescriptor().getName();
89 this.idStore = this.createMessageIdStore();
90 }
91 }
92
93 protected IdempotentMessageIdStore createMessageIdStore()
94 {
95 return new IdempotentInMemoryMessageIdStore(assignedComponentName, maxMessages, messageTTL,
96 expirationInterval);
97 }
98
99
100 public boolean isMatch(UMOEvent event) throws MessagingException
101 {
102 if (idStore == null)
103 {
104
105 synchronized (this)
106 {
107 this.initialize(event);
108 }
109 }
110
111 try
112 {
113 return !idStore.containsId(this.getIdForEvent(event));
114 }
115 catch (Exception ex)
116 {
117 throw new RoutingException(event.getMessage(), event.getEndpoint(), ex);
118 }
119 }
120
121
122 public UMOEvent[] process(UMOEvent event) throws MessagingException
123 {
124 String eventComponentName = event.getComponent().getDescriptor().getName();
125 if (!assignedComponentName.equals(eventComponentName))
126 {
127 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 throw new RoutingException(event.getMessage(), event.getEndpoint(), iex);
133 }
134
135 Object id = this.getIdForEvent(event);
136
137 try
138 {
139 if (idStore.storeId(id))
140 {
141 return new UMOEvent[]{event};
142 }
143 else
144 {
145 return null;
146 }
147 }
148 catch (Exception e)
149 {
150 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 return event.getMessage().getUniqueId();
158 }
159
160 }