1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.processor; |
19 |
|
|
20 |
|
import org.apache.camel.Endpoint; |
21 |
|
import org.apache.camel.Exchange; |
22 |
|
import org.apache.camel.Expression; |
23 |
|
import org.apache.camel.PollingConsumer; |
24 |
|
import org.apache.camel.Processor; |
25 |
|
import org.apache.camel.impl.LoggingExceptionHandler; |
26 |
|
import org.apache.camel.impl.ServiceSupport; |
27 |
|
import org.apache.camel.spi.ExceptionHandler; |
28 |
|
import org.apache.camel.util.ExpressionComparator; |
29 |
|
import org.apache.camel.util.ExpressionListComparator; |
30 |
|
import org.apache.camel.util.ServiceHelper; |
31 |
|
import org.apache.commons.logging.Log; |
32 |
|
import org.apache.commons.logging.LogFactory; |
33 |
|
|
34 |
|
import java.util.Comparator; |
35 |
|
import java.util.Iterator; |
36 |
|
import java.util.List; |
37 |
|
import java.util.Set; |
38 |
|
import java.util.TreeSet; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public class Resequencer extends ServiceSupport implements Runnable { |
46 |
1 |
private static final transient Log log = LogFactory.getLog(Resequencer.class); |
47 |
|
private Endpoint endpoint; |
48 |
|
private Processor processor; |
49 |
|
private Set<Exchange> set; |
50 |
1 |
private long batchTimeout = 1000L; |
51 |
1 |
private int batchSize = 100; |
52 |
|
private PollingConsumer consumer; |
53 |
|
private ExceptionHandler exceptionHandler; |
54 |
|
|
55 |
|
public Resequencer(Endpoint endpoint, Processor processor, Expression<Exchange> expression) { |
56 |
0 |
this(endpoint, processor, createSet(expression)); |
57 |
0 |
} |
58 |
|
|
59 |
|
public Resequencer(Endpoint endpoint, Processor processor, List<Expression<Exchange>> expressions) { |
60 |
1 |
this(endpoint, processor, createSet(expressions)); |
61 |
1 |
} |
62 |
|
|
63 |
1 |
public Resequencer(Endpoint endpoint, Processor processor, Set<Exchange> set) { |
64 |
1 |
this.endpoint = endpoint; |
65 |
1 |
this.processor = processor; |
66 |
1 |
this.set = set; |
67 |
1 |
} |
68 |
|
|
69 |
|
@Override |
70 |
|
public String toString() { |
71 |
2 |
return "Resequencer[to: " + processor + "]"; |
72 |
|
} |
73 |
|
|
74 |
|
public void run() { |
75 |
1 |
log.debug("Starting thread for " + this); |
76 |
3 |
while (!isStopped() && !isStopping()) { |
77 |
|
try { |
78 |
2 |
processBatch(); |
79 |
|
} |
80 |
0 |
catch (Exception e) { |
81 |
0 |
getExceptionHandler().handleException(e); |
82 |
2 |
} |
83 |
0 |
} |
84 |
1 |
set.clear(); |
85 |
1 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
public ExceptionHandler getExceptionHandler() { |
90 |
0 |
if (exceptionHandler == null) { |
91 |
0 |
exceptionHandler = new LoggingExceptionHandler(getClass()); |
92 |
|
} |
93 |
0 |
return exceptionHandler; |
94 |
|
} |
95 |
|
|
96 |
|
public void setExceptionHandler(ExceptionHandler exceptionHandler) { |
97 |
0 |
this.exceptionHandler = exceptionHandler; |
98 |
0 |
} |
99 |
|
|
100 |
|
public int getBatchSize() { |
101 |
0 |
return batchSize; |
102 |
|
} |
103 |
|
|
104 |
|
public void setBatchSize(int batchSize) { |
105 |
0 |
this.batchSize = batchSize; |
106 |
0 |
} |
107 |
|
|
108 |
|
public long getBatchTimeout() { |
109 |
0 |
return batchTimeout; |
110 |
|
} |
111 |
|
|
112 |
|
public void setBatchTimeout(long batchTimeout) { |
113 |
0 |
this.batchTimeout = batchTimeout; |
114 |
0 |
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
protected synchronized void processBatch() throws Exception { |
124 |
2 |
long start = System.currentTimeMillis(); |
125 |
2 |
long end = start + batchTimeout; |
126 |
6 |
for (int i = 0; i < batchSize; i++) { |
127 |
6 |
long timeout = end - System.currentTimeMillis(); |
128 |
|
|
129 |
6 |
Exchange exchange = consumer.receive(timeout); |
130 |
6 |
if (exchange == null) { |
131 |
2 |
break; |
132 |
|
} |
133 |
4 |
set.add(exchange); |
134 |
|
} |
135 |
|
|
136 |
2 |
if (log.isDebugEnabled()) { |
137 |
0 |
log.debug("Finsihed batch size: " + batchSize + " timeout: " + batchTimeout + " so sending set: " + set); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
2 |
Iterator<Exchange> iter = set.iterator(); |
142 |
6 |
while (iter.hasNext()) { |
143 |
4 |
Exchange exchange = iter.next(); |
144 |
4 |
iter.remove(); |
145 |
4 |
processor.process(exchange); |
146 |
4 |
} |
147 |
2 |
} |
148 |
|
|
149 |
|
protected void doStart() throws Exception { |
150 |
1 |
consumer = endpoint.createPollingConsumer(); |
151 |
|
|
152 |
1 |
ServiceHelper.startServices(processor, consumer); |
153 |
|
|
154 |
1 |
Thread thread = new Thread(this, this + " Polling Thread"); |
155 |
1 |
thread.start(); |
156 |
1 |
} |
157 |
|
|
158 |
|
protected void doStop() throws Exception { |
159 |
1 |
ServiceHelper.stopServices(consumer, processor); |
160 |
1 |
consumer = null; |
161 |
1 |
} |
162 |
|
|
163 |
|
protected static Set<Exchange> createSet(Expression<Exchange> expression) { |
164 |
1 |
return createSet(new ExpressionComparator<Exchange>(expression)); |
165 |
|
} |
166 |
|
|
167 |
|
protected static Set<Exchange> createSet(List<Expression<Exchange>> expressions) { |
168 |
1 |
if (expressions.size() == 1) { |
169 |
1 |
return createSet(expressions.get(0)); |
170 |
|
} |
171 |
0 |
return createSet(new ExpressionListComparator<Exchange>(expressions)); |
172 |
|
} |
173 |
|
|
174 |
|
protected static Set<Exchange> createSet(Comparator<? super Exchange> comparator) { |
175 |
1 |
return new TreeSet<Exchange>(comparator); |
176 |
|
} |
177 |
|
} |