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