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.Timer;
020    import java.util.TimerTask;
021    import java.util.concurrent.RejectedExecutionException;
022    
023    import org.apache.camel.AsyncCallback;
024    import org.apache.camel.AsyncProcessor;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Message;
027    import org.apache.camel.Predicate;
028    import org.apache.camel.Processor;
029    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
030    import org.apache.camel.model.ExceptionType;
031    import org.apache.camel.model.LoggingLevel;
032    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
033    import org.apache.camel.util.AsyncProcessorHelper;
034    import org.apache.camel.util.ServiceHelper;
035    import org.apache.commons.logging.Log;
036    import org.apache.commons.logging.LogFactory;
037    
038    /**
039     * Implements a <a
040     * href="http://activemq.apache.org/camel/dead-letter-channel.html">Dead Letter
041     * Channel</a> after attempting to redeliver the message using the
042     * {@link RedeliveryPolicy}
043     *
044     * @version $Revision: 63653 $
045     */
046    public class DeadLetterChannel extends ErrorHandlerSupport implements AsyncProcessor {
047        public static final String REDELIVERY_COUNTER = "org.apache.camel.RedeliveryCounter";
048        public static final String REDELIVERED = "org.apache.camel.Redelivered";
049        public static final String EXCEPTION_CAUSE_PROPERTY = "CamelCauseException";
050        public static final String CAUGHT_EXCEPTION_HEADER = "org.apache.camel.CamelCaughtException";    
051        
052        private static final transient Log LOG = LogFactory.getLog(DeadLetterChannel.class);
053        private static final String FAILURE_HANDLED_PROPERTY = DeadLetterChannel.class.getName() + ".FAILURE_HANDLED";
054        
055        private static Timer timer = new Timer();
056        private Processor output;
057        private Processor deadLetter;
058        private AsyncProcessor outputAsync;
059        private RedeliveryPolicy redeliveryPolicy;
060        private Logger logger;
061    
062        private class RedeliveryData {
063            int redeliveryCounter;
064            long redeliveryDelay;
065            boolean sync = true;
066            Predicate handledPredicate;
067    
068            // default behavior which can be overloaded on a per exception basis
069            RedeliveryPolicy currentRedeliveryPolicy = redeliveryPolicy;
070            Processor failureProcessor = deadLetter;
071        }
072        
073        private class RedeliverTimerTask extends TimerTask {
074            private final Exchange exchange;
075            private final AsyncCallback callback;
076            private final RedeliveryData data;
077            
078            public RedeliverTimerTask(Exchange exchange, AsyncCallback callback, RedeliveryData data) {
079                this.exchange = exchange;
080                this.callback = callback;
081                this.data = data;
082            }
083    
084            @Override
085            public void run() {
086                //only handle the real AsyncProcess the exchange 
087                outputAsync.process(exchange, new AsyncCallback() {
088                    public void done(boolean sync) {
089                        // Only handle the async case...
090                        if (sync) {
091                            return;
092                        }
093                        data.sync = false;
094                        // only process if the exchange hasn't failed
095                        // and it has not been handled by the error processor
096                        if (exchange.getException() != null && !isFailureHandled(exchange)) {
097                            // if we are redelivering then sleep before trying again
098                            asyncProcess(exchange, callback, data);
099                        } else {
100                            callback.done(sync);
101                        }
102                    }
103                });                
104            } 
105        }
106    
107        public DeadLetterChannel(Processor output, Processor deadLetter) {
108            this(output, deadLetter, new RedeliveryPolicy(), DeadLetterChannel.createDefaultLogger(),
109                ErrorHandlerSupport.createDefaultExceptionPolicyStrategy());
110        }
111    
112        public DeadLetterChannel(Processor output, Processor deadLetter, RedeliveryPolicy redeliveryPolicy, Logger logger, ExceptionPolicyStrategy exceptionPolicyStrategy) {
113            this.deadLetter = deadLetter;
114            this.output = output;
115            this.outputAsync = AsyncProcessorTypeConverter.convert(output);
116    
117            this.redeliveryPolicy = redeliveryPolicy;
118            this.logger = logger;
119            setExceptionPolicy(exceptionPolicyStrategy);
120        }
121    
122        public static <E extends Exchange> Logger createDefaultLogger() {
123            return new Logger(LOG, LoggingLevel.ERROR);
124        }
125    
126        @Override
127        public String toString() {
128            return "DeadLetterChannel[" + output + ", " + deadLetter + "]";
129        }
130    
131        public boolean process(Exchange exchange, final AsyncCallback callback) {
132            return process(exchange, callback, new RedeliveryData());
133        }
134    
135        public boolean process(final Exchange exchange, final AsyncCallback callback, final RedeliveryData data) {
136    
137            while (true) {
138                // we can't keep retrying if the route is being shutdown.
139                if (!isRunAllowed()) {
140                    if (exchange.getException() == null) {
141                        exchange.setException(new RejectedExecutionException());
142                    }
143                    callback.done(data.sync);
144                    return data.sync;
145                }
146    
147                // if the exchange is transacted then let the underlying system handle the redelivery etc.
148                // this DeadLetterChannel is only for non transacted exchanges
149                if (exchange.isTransacted() && exchange.getException() != null) {
150                    if (LOG.isDebugEnabled()) {
151                        LOG.debug("This is a transacted exchange, bypassing this DeadLetterChannel: " + this + " for exchange: " + exchange);
152                    }
153                    return data.sync;
154                }
155    
156                // did previous processing caused an exception?
157                if (exchange.getException() != null) {
158                    handleException(exchange, data);
159                }
160    
161                // should we redeliver or not?
162                if (!data.currentRedeliveryPolicy.shouldRedeliver(data.redeliveryCounter)) {
163                    return deliverToFaultProcessor(exchange, callback, data);
164                }
165    
166                // should we redeliver
167                if (data.redeliveryCounter > 0) {
168                    // okay we will give it another go so clear the exception so we can try again
169                    if (exchange.getException() != null) {
170                        exchange.setException(null);
171                    }
172    
173                    // wait until we should redeliver
174                    data.redeliveryDelay = data.currentRedeliveryPolicy.sleep(data.redeliveryDelay);
175                }
176    
177                // process the exchange
178                boolean sync = outputAsync.process(exchange, new AsyncCallback() {
179                    public void done(boolean sync) {
180                        // Only handle the async case...
181                        if (sync) {
182                            return;
183                        }
184                        data.sync = false;
185                        // only process if the exchange hasn't failed
186                        // and it has not been handled by the error processor
187                        if (exchange.getException() != null && !isFailureHandled(exchange)) {                        
188                            //TODO Call the Timer for the asyncProcessor
189                            asyncProcess(exchange, callback, data);
190                        } else {
191                            callback.done(sync);
192                        }
193                    }
194                });
195                if (!sync) {
196                    // It is going to be processed async..
197                    return false;
198                }
199                if (exchange.getException() == null || isFailureHandled(exchange)) {
200                    // If everything went well.. then we exit here..
201                    callback.done(true);
202                    return true;
203                }
204                // error occurred so loop back around.....
205            }
206    
207        }
208    
209        private void logFailedDelivery(String message, RedeliveryData data, Throwable e) {
210            LoggingLevel newLogLevel = null;
211            if (data.currentRedeliveryPolicy.shouldRedeliver(data.redeliveryCounter)) {
212                newLogLevel = data.currentRedeliveryPolicy.getRetryAttemptedLogLevel();
213            } else {
214                newLogLevel = data.currentRedeliveryPolicy.getRetriesExhaustedLogLevel();
215            }
216            if (e != null) {
217                logger.log(message, e, newLogLevel);
218            } else {
219                logger.log(message, newLogLevel);
220            }
221        }
222    
223        protected void asyncProcess(final Exchange exchange, final AsyncCallback callback, final RedeliveryData data) {
224            // set the timer here
225            if (!isRunAllowed()) {
226                if (exchange.getException() == null) {
227                    exchange.setException(new RejectedExecutionException());
228                }
229                callback.done(data.sync);
230                return;
231            }
232    
233            // if the exchange is transacted then let the underlying system handle the redelivery etc.
234            // this DeadLetterChannel is only for non transacted exchanges
235            if (exchange.isTransacted() && exchange.getException() != null) {
236                if (LOG.isDebugEnabled()) {
237                    LOG.debug("This is a transacted exchange, bypassing this DeadLetterChannel: " + this + " for exchange: " + exchange);
238                }
239                return;
240            }
241            
242            // did previous processing caused an exception?
243            if (exchange.getException() != null) {
244                handleException(exchange, data);
245            }        
246            
247            if (!data.currentRedeliveryPolicy.shouldRedeliver(data.redeliveryCounter)) {
248                deliverToFaultProcessor(exchange, callback, data);
249                return;
250            }
251            
252            // process the next try
253            // if we are redelivering then sleep before trying again
254            if (data.redeliveryCounter > 0) {
255                // okay we will give it another go so clear the exception so we can try again
256                if (exchange.getException() != null) {
257                    exchange.setException(null);
258                }
259                // wait until we should redeliver
260                data.redeliveryDelay = data.currentRedeliveryPolicy.getRedeliveryDelay(data.redeliveryDelay);
261                timer.schedule(new RedeliverTimerTask(exchange, callback, data), data.redeliveryDelay);
262            }        
263        }
264        
265        private void handleException(Exchange exchange, RedeliveryData data) {
266            Throwable e = exchange.getException();
267            // set the original caused exception
268            exchange.setProperty(EXCEPTION_CAUSE_PROPERTY, e);
269    
270            // find the error handler to use (if any)
271            ExceptionType exceptionPolicy = getExceptionPolicy(exchange, e);
272            if (exceptionPolicy != null) {
273                data.currentRedeliveryPolicy = exceptionPolicy.createRedeliveryPolicy(exchange.getContext(), data.currentRedeliveryPolicy);
274                data.handledPredicate = exceptionPolicy.getHandledPolicy();
275                Processor processor = exceptionPolicy.getErrorHandler();
276                if (processor != null) {
277                    data.failureProcessor = processor;
278                }                    
279            }
280            
281            logFailedDelivery("Failed delivery for exchangeId: " + exchange.getExchangeId() + ". On delivery attempt: " + data.redeliveryCounter + " caught: " + e, data, e);
282            data.redeliveryCounter = incrementRedeliveryCounter(exchange, e);
283            
284        }
285        
286        private boolean deliverToFaultProcessor(final Exchange exchange, final AsyncCallback callback,
287                                                final RedeliveryData data) {
288            // we did not success with the redelivery so now we let the failure processor handle it
289            setFailureHandled(exchange);
290            // must decrement the redelivery counter as we didn't process the redelivery but is
291            // handling by the failure handler. So we must -1 to not let the counter be out-of-sync
292            decrementRedeliveryCounter(exchange);
293    
294            AsyncProcessor afp = AsyncProcessorTypeConverter.convert(data.failureProcessor);
295            boolean sync = afp.process(exchange, new AsyncCallback() {
296                public void done(boolean sync) {
297                    restoreExceptionOnExchange(exchange, data.handledPredicate);
298                    callback.done(data.sync);
299                }
300            });
301    
302            // The line below shouldn't be needed, it is invoked by the AsyncCallback above
303            //restoreExceptionOnExchange(exchange, data.handledPredicate);
304            logFailedDelivery("Failed delivery for exchangeId: " + exchange.getExchangeId() + ". Handled by the failure processor: " + data.failureProcessor, data, null);
305            return sync;
306        }
307    
308        public static boolean isFailureHandled(Exchange exchange) {
309            return exchange.getProperty(FAILURE_HANDLED_PROPERTY) != null 
310                || exchange.getIn().getHeader(CAUGHT_EXCEPTION_HEADER) != null;
311        }
312    
313        public static void setFailureHandled(Exchange exchange) {
314            exchange.setProperty(FAILURE_HANDLED_PROPERTY, exchange.getException());
315            exchange.getIn().setHeader(CAUGHT_EXCEPTION_HEADER, exchange.getException());        
316            exchange.setException(null);
317        }
318    
319        protected static void restoreExceptionOnExchange(Exchange exchange, Predicate handledPredicate) {
320            if (handledPredicate == null || !handledPredicate.matches(exchange)) {
321                if (LOG.isDebugEnabled()) {
322                    LOG.debug("This exchange is not handled so its marked as failed: " + exchange);
323                }
324                // exception not handled, put exception back in the exchange
325                exchange.setException(exchange.getProperty(FAILURE_HANDLED_PROPERTY, Throwable.class));
326            } else {
327                if (LOG.isDebugEnabled()) {
328                    LOG.debug("This exchange is handled so its marked as not failed: " + exchange);
329                }
330                exchange.setProperty(Exchange.EXCEPTION_HANDLED_PROPERTY, Boolean.TRUE);
331            }
332        }
333    
334        public void process(Exchange exchange) throws Exception {
335            AsyncProcessorHelper.process(this, exchange);
336        }
337    
338        // Properties
339        // -------------------------------------------------------------------------
340    
341        /**
342         * Returns the output processor
343         */
344        public Processor getOutput() {
345            return output;
346        }
347    
348        /**
349         * Returns the dead letter that message exchanges will be sent to if the
350         * redelivery attempts fail
351         */
352        public Processor getDeadLetter() {
353            return deadLetter;
354        }
355    
356        public RedeliveryPolicy getRedeliveryPolicy() {
357            return redeliveryPolicy;
358        }
359    
360        /**
361         * Sets the redelivery policy
362         */
363        public void setRedeliveryPolicy(RedeliveryPolicy redeliveryPolicy) {
364            this.redeliveryPolicy = redeliveryPolicy;
365        }
366    
367        public Logger getLogger() {
368            return logger;
369        }
370    
371        /**
372         * Sets the logger strategy; which {@link Log} to use and which
373         * {@link LoggingLevel} to use
374         */
375        public void setLogger(Logger logger) {
376            this.logger = logger;
377        }
378    
379        // Implementation methods
380        // -------------------------------------------------------------------------
381    
382        /**
383         * Increments the redelivery counter and adds the redelivered flag if the
384         * message has been redelivered
385         */
386        protected int incrementRedeliveryCounter(Exchange exchange, Throwable e) {
387            Message in = exchange.getIn();
388            Integer counter = in.getHeader(REDELIVERY_COUNTER, Integer.class);
389            int next = 1;
390            if (counter != null) {
391                next = counter + 1;
392            }
393            in.setHeader(REDELIVERY_COUNTER, next);
394            in.setHeader(REDELIVERED, Boolean.TRUE);
395            return next;
396        }
397    
398        /**
399         * Prepares the redelivery counter and boolean flag for the failure handle processor
400         */
401        private void decrementRedeliveryCounter(Exchange exchange) {
402            Message in = exchange.getIn();
403            Integer counter = in.getHeader(REDELIVERY_COUNTER, Integer.class);
404            if (counter != null) {
405                int prev = counter - 1;
406                in.setHeader(REDELIVERY_COUNTER, prev);
407                // set boolean flag according to counter
408                in.setHeader(REDELIVERED, prev > 0 ? Boolean.TRUE : Boolean.FALSE);
409            } else {
410                // not redelivered
411                in.setHeader(REDELIVERY_COUNTER, 0);
412                in.setHeader(REDELIVERED, Boolean.FALSE);
413            }
414        }
415    
416        @Override
417        protected void doStart() throws Exception {
418            ServiceHelper.startServices(output, deadLetter);
419        }
420    
421        @Override
422        protected void doStop() throws Exception {
423            ServiceHelper.stopServices(deadLetter, output);
424        }    
425        
426    }