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