1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.inbound; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.umo.MessagingException; |
16 | |
import org.mule.umo.UMOEvent; |
17 | |
import org.mule.umo.routing.RoutingException; |
18 | |
import org.mule.util.FileUtils; |
19 | |
|
20 | |
import java.io.BufferedReader; |
21 | |
import java.io.File; |
22 | |
import java.io.FileReader; |
23 | |
import java.io.IOException; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class IdempotentReceiver extends SelectiveConsumer |
38 | |
{ |
39 | |
private static final String DEFAULT_STORE_PATH = "./idempotent"; |
40 | |
|
41 | |
private Set messageIds; |
42 | |
private File idStore; |
43 | |
private String componentName; |
44 | 0 | private boolean disablePersistence = false; |
45 | |
private String storePath; |
46 | |
|
47 | |
public IdempotentReceiver() |
48 | 0 | { |
49 | 0 | messageIds = new HashSet(); |
50 | 0 | String path = MuleManager.getConfiguration().getWorkingDirectory() + "/idempotent"; |
51 | 0 | setStorePath(path); |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
public boolean isMatch(UMOEvent event) throws MessagingException |
56 | |
{ |
57 | 0 | if (idStore == null) |
58 | |
{ |
59 | |
|
60 | |
|
61 | 0 | this.load(event); |
62 | |
} |
63 | 0 | return !messageIds.contains(this.getIdForEvent(event)); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
public UMOEvent[] process(UMOEvent event) throws MessagingException |
68 | |
{ |
69 | 0 | if (isMatch(event)) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 0 | checkComponentName(event.getComponent().getDescriptor().getName()); |
74 | |
} |
75 | 0 | catch (IllegalArgumentException e) |
76 | |
{ |
77 | 0 | throw new RoutingException(event.getMessage(), event.getEndpoint(), e); |
78 | 0 | } |
79 | |
|
80 | 0 | Object id = this.getIdForEvent(event); |
81 | |
try |
82 | |
{ |
83 | 0 | this.storeId(id); |
84 | 0 | return new UMOEvent[]{event}; |
85 | |
} |
86 | 0 | catch (IOException e) |
87 | |
{ |
88 | 0 | throw new RoutingException( |
89 | |
CoreMessages.failedToWriteMessageToStore(id, idStore.getAbsolutePath()), |
90 | |
event.getMessage(), event.getEndpoint(), e); |
91 | |
} |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | return null; |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
protected Object getIdForEvent(UMOEvent event) throws MessagingException |
100 | |
{ |
101 | 0 | return event.getMessage().getUniqueId(); |
102 | |
} |
103 | |
|
104 | |
private void checkComponentName(String name) throws IllegalArgumentException |
105 | |
{ |
106 | 0 | if (!componentName.equals(name)) |
107 | |
{ |
108 | 0 | throw new IllegalArgumentException("This receiver is assigned to component: " + componentName |
109 | |
+ " but has received an event for component: " + name |
110 | |
+ ". Please check your config to make sure each component" |
111 | |
+ "has its own instance of IdempotentReceiver"); |
112 | |
} |
113 | 0 | } |
114 | |
|
115 | |
protected synchronized void load(UMOEvent event) throws RoutingException |
116 | |
{ |
117 | 0 | this.componentName = event.getComponent().getDescriptor().getName(); |
118 | |
|
119 | 0 | if (idStore == null) |
120 | |
{ |
121 | 0 | idStore = FileUtils.newFile(storePath + "/muleComponent_" + componentName + ".store"); |
122 | |
} |
123 | |
|
124 | 0 | if (disablePersistence) |
125 | |
{ |
126 | 0 | return; |
127 | |
} |
128 | |
|
129 | |
try |
130 | |
{ |
131 | 0 | if (idStore.exists()) |
132 | |
{ |
133 | 0 | BufferedReader reader = null; |
134 | |
try |
135 | |
{ |
136 | 0 | reader = new BufferedReader(new FileReader(idStore)); |
137 | |
String id; |
138 | 0 | while ((id = reader.readLine()) != null) |
139 | |
{ |
140 | 0 | messageIds.add(id); |
141 | |
} |
142 | |
} |
143 | |
finally |
144 | |
{ |
145 | 0 | if (reader != null) |
146 | |
{ |
147 | 0 | reader.close(); |
148 | |
} |
149 | |
} |
150 | |
} |
151 | |
else |
152 | |
{ |
153 | 0 | idStore = FileUtils.createFile(idStore.getAbsolutePath()); |
154 | |
} |
155 | |
} |
156 | 0 | catch (IOException e) |
157 | |
{ |
158 | 0 | throw new RoutingException( |
159 | |
CoreMessages.failedToReadFromStore(idStore.getAbsolutePath()), |
160 | |
event.getMessage(), event.getEndpoint(), e); |
161 | 0 | } |
162 | 0 | } |
163 | |
|
164 | |
protected synchronized void storeId(Object id) throws IOException |
165 | |
{ |
166 | 0 | messageIds.add(id); |
167 | 0 | if (disablePersistence) |
168 | |
{ |
169 | 0 | return; |
170 | |
} |
171 | 0 | FileUtils.stringToFile(idStore.getAbsolutePath(), id.toString(), true, true); |
172 | 0 | } |
173 | |
|
174 | |
public boolean isDisablePersistence() |
175 | |
{ |
176 | 0 | return disablePersistence; |
177 | |
} |
178 | |
|
179 | |
public void setDisablePersistence(boolean disablePersistence) |
180 | |
{ |
181 | 0 | this.disablePersistence = disablePersistence; |
182 | 0 | } |
183 | |
|
184 | |
public String getStorePath() |
185 | |
{ |
186 | 0 | return storePath; |
187 | |
} |
188 | |
|
189 | |
public void setStorePath(String storePath) |
190 | |
{ |
191 | 0 | if (storePath == null) |
192 | |
{ |
193 | 0 | this.storePath = DEFAULT_STORE_PATH; |
194 | |
} |
195 | 0 | else if (storePath.endsWith("/")) |
196 | |
{ |
197 | 0 | storePath = storePath.substring(0, storePath.length() - 1); |
198 | 0 | this.storePath = storePath; |
199 | |
} |
200 | |
else |
201 | |
{ |
202 | 0 | this.storePath = storePath; |
203 | |
} |
204 | 0 | } |
205 | |
|
206 | |
} |