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.concurrent.ScheduledExecutorService;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Predicate;
024    import org.apache.camel.Processor;
025    import org.apache.camel.builder.ExpressionBuilder;
026    import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
027    import org.apache.camel.util.CamelLogger;
028    import org.apache.camel.util.ExpressionToPredicateAdapter;
029    
030    /**
031     * Implements a <a
032     * href="http://camel.apache.org/dead-letter-channel.html">Dead Letter
033     * Channel</a> after attempting to redeliver the message using the
034     * {@link RedeliveryPolicy}
035     *
036     * @version 
037     */
038    public class DeadLetterChannel extends RedeliveryErrorHandler {
039    
040        /**
041         * Creates the dead letter channel.
042         *
043         * @param camelContext              the camel context
044         * @param output                    outer processor that should use this dead letter channel
045         * @param logger                    logger to use for logging failures and redelivery attempts
046         * @param redeliveryProcessor       an optional processor to run before redelivery attempt
047         * @param redeliveryPolicy          policy for redelivery
048         * @param exceptionPolicyStrategy   strategy for onException handling
049         * @param deadLetter                the failure processor to send failed exchanges to
050         * @param deadLetterUri             an optional uri for logging purpose
051         * @param useOriginalBodyPolicy     should the original IN body be moved to the dead letter queue or the current exchange IN body?
052         * @param retryWhile                retry while
053         * @param executorService           the {@link java.util.concurrent.ScheduledExecutorService} to be used for redelivery thread pool. Can be <tt>null</tt>.
054         */
055        public DeadLetterChannel(CamelContext camelContext, Processor output, CamelLogger logger, Processor redeliveryProcessor, RedeliveryPolicy redeliveryPolicy,
056                ExceptionPolicyStrategy exceptionPolicyStrategy, Processor deadLetter, String deadLetterUri, boolean useOriginalBodyPolicy, Predicate retryWhile,
057                ScheduledExecutorService executorService) {
058    
059            super(camelContext, output, logger, redeliveryProcessor, redeliveryPolicy, deadLetter, deadLetterUri, useOriginalBodyPolicy, retryWhile, executorService);
060            setExceptionPolicy(exceptionPolicyStrategy);
061        }
062    
063        public void process(Exchange exchange) throws Exception {
064            // just to let the stack trace reveal that this is a dead letter channel
065            super.process(exchange);
066        }
067    
068        @Override
069        public String toString() {
070            if (output == null) {
071                // if no output then don't do any description
072                return "";
073            }
074            return "DeadLetterChannel[" + output + ", " + (deadLetterUri != null ? deadLetterUri : deadLetter) + "]";
075        }
076    
077        @Override
078        protected Predicate getDefaultHandledPredicate() {
079            // DeadLetterChannel handles errors before sending to DLQ
080            return ExpressionToPredicateAdapter.toPredicate(ExpressionBuilder.constantExpression(true));
081        }
082    
083        @Override
084        protected boolean isRunAllowedOnPreparingShutdown() {
085            // allow tu run as we want to move the message eto DLC, instead of rejecting the message
086            return true;
087        }
088    
089        @Override
090        public boolean isDeadLetterChannel() {
091            return true;
092        }
093    }