001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.processor;
018
019 import java.util.ArrayList;
020 import java.util.List;
021 import java.util.concurrent.TimeUnit;
022 import java.util.concurrent.locks.Condition;
023 import java.util.concurrent.locks.Lock;
024 import java.util.concurrent.locks.ReentrantLock;
025
026 import org.apache.camel.CamelContext;
027 import org.apache.camel.Exchange;
028 import org.apache.camel.Navigate;
029 import org.apache.camel.Processor;
030 import org.apache.camel.impl.LoggingExceptionHandler;
031 import org.apache.camel.impl.ServiceSupport;
032 import org.apache.camel.processor.resequencer.ResequencerEngine;
033 import org.apache.camel.processor.resequencer.SequenceElementComparator;
034 import org.apache.camel.processor.resequencer.SequenceSender;
035 import org.apache.camel.spi.ExceptionHandler;
036 import org.apache.camel.util.ObjectHelper;
037 import org.apache.camel.util.ServiceHelper;
038
039 /**
040 * A resequencer that re-orders a (continuous) stream of {@link Exchange}s. The
041 * algorithm implemented by {@link ResequencerEngine} is based on the detection
042 * of gaps in a message stream rather than on a fixed batch size. Gap detection
043 * in combination with timeouts removes the constraint of having to know the
044 * number of messages of a sequence (i.e. the batch size) in advance.
045 * <p>
046 * Messages must contain a unique sequence number for which a predecessor and a
047 * successor is known. For example a message with the sequence number 3 has a
048 * predecessor message with the sequence number 2 and a successor message with
049 * the sequence number 4. The message sequence 2,3,5 has a gap because the
050 * sucessor of 3 is missing. The resequencer therefore has to retain message 5
051 * until message 4 arrives (or a timeout occurs).
052 * <p>
053 * Instances of this class poll for {@link Exchange}s from a given
054 * <code>endpoint</code>. Resequencing work and the delivery of messages to
055 * the next <code>processor</code> is done within the single polling thread.
056 *
057 * @version $Revision: 18209 $
058 *
059 * @see ResequencerEngine
060 */
061 public class StreamResequencer extends ServiceSupport implements SequenceSender<Exchange>, Processor, Navigate<Processor>, Traceable {
062
063 private static final long DELIVERY_ATTEMPT_INTERVAL = 1000L;
064
065 private final CamelContext camelContext;
066 private final ExceptionHandler exceptionHandler;
067 private final ResequencerEngine<Exchange> engine;
068 private final Processor processor;
069 private Delivery delivery;
070 private int capacity;
071
072 /**
073 * Creates a new {@link StreamResequencer} instance.
074 *
075 * @param processor next processor that processes re-ordered exchanges.
076 * @param comparator a sequence element comparator for exchanges.
077 */
078 public StreamResequencer(CamelContext camelContext, Processor processor, SequenceElementComparator<Exchange> comparator) {
079 ObjectHelper.notNull(camelContext, "CamelContext");
080 this.camelContext = camelContext;
081 this.exceptionHandler = new LoggingExceptionHandler(getClass());
082 this.engine = new ResequencerEngine<Exchange>(comparator);
083 this.engine.setSequenceSender(this);
084 this.processor = processor;
085 }
086
087 /**
088 * Returns this resequencer's exception handler.
089 */
090 public ExceptionHandler getExceptionHandler() {
091 return exceptionHandler;
092 }
093
094 /**
095 * Returns the next processor.
096 */
097 public Processor getProcessor() {
098 return processor;
099 }
100
101 /**
102 * Returns this resequencer's capacity. The capacity is the maximum number
103 * of exchanges that can be managed by this resequencer at a given point in
104 * time. If the capacity if reached, polling from the endpoint will be
105 * skipped for <code>timeout</code> milliseconds giving exchanges the
106 * possibility to time out and to be delivered after the waiting period.
107 *
108 * @return this resequencer's capacity.
109 */
110 public int getCapacity() {
111 return capacity;
112 }
113
114 /**
115 * Returns this resequencer's timeout. This sets the resequencer engine's
116 * timeout via {@link ResequencerEngine#setTimeout(long)}. This value is
117 * also used to define the polling timeout from the endpoint.
118 *
119 * @return this resequencer's timeout. (Processor)
120 * @see ResequencerEngine#setTimeout(long)
121 */
122 public long getTimeout() {
123 return engine.getTimeout();
124 }
125
126 public void setCapacity(int capacity) {
127 this.capacity = capacity;
128 }
129
130 public void setTimeout(long timeout) {
131 engine.setTimeout(timeout);
132 }
133
134 @Override
135 public String toString() {
136 return "StreamResequencer[to: " + processor + "]";
137 }
138
139 public String getTraceLabel() {
140 return "streamResequence";
141 }
142
143 @Override
144 protected void doStart() throws Exception {
145 ServiceHelper.startServices(processor);
146 delivery = new Delivery();
147 engine.start();
148 delivery.start();
149 }
150
151 @Override
152 protected void doStop() throws Exception {
153 // let's stop everything in the reverse order
154 // no need to stop the worker thread -- it will stop automatically when this service is stopped
155 engine.stop();
156 ServiceHelper.stopServices(processor);
157 }
158
159 /**
160 * Sends the <code>exchange</code> to the next <code>processor</code>.
161 *
162 * @param exchange exchange to send.
163 */
164 public void sendElement(Exchange exchange) throws Exception {
165 processor.process(exchange);
166 }
167
168 public void process(Exchange exchange) throws Exception {
169 while (engine.size() >= capacity) {
170 Thread.sleep(getTimeout());
171 }
172 engine.insert(exchange);
173 delivery.request();
174 }
175
176 public boolean hasNext() {
177 return processor != null;
178 }
179
180 public List<Processor> next() {
181 if (!hasNext()) {
182 return null;
183 }
184 List<Processor> answer = new ArrayList<Processor>(1);
185 answer.add(processor);
186 return answer;
187 }
188
189 private class Delivery extends Thread {
190
191 private Lock deliveryRequestLock = new ReentrantLock();
192 private Condition deliveryRequestCondition = deliveryRequestLock.newCondition();
193
194 public Delivery() {
195 super(camelContext.getExecutorServiceStrategy().getThreadName("Resequencer Delivery"));
196 }
197
198 @Override
199 public void run() {
200 while (isRunAllowed()) {
201 try {
202 deliveryRequestLock.lock();
203 try {
204 deliveryRequestCondition.await(DELIVERY_ATTEMPT_INTERVAL, TimeUnit.MILLISECONDS);
205 } finally {
206 deliveryRequestLock.unlock();
207 }
208 } catch (InterruptedException e) {
209 break;
210 }
211 try {
212 engine.deliver();
213 } catch (Throwable t) {
214 // a fail safe to handle all exceptions being thrown
215 getExceptionHandler().handleException(t);
216 }
217 }
218 }
219
220 public void cancel() {
221 interrupt();
222 }
223
224 public void request() {
225 deliveryRequestLock.lock();
226 try {
227 deliveryRequestCondition.signal();
228 } finally {
229 deliveryRequestLock.unlock();
230 }
231 }
232
233 }
234
235 }