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.builder;
018
019 import org.apache.camel.Endpoint;
020 import org.apache.camel.ExchangePattern;
021 import org.apache.camel.LoggingLevel;
022 import org.apache.camel.NoSuchEndpointException;
023 import org.apache.camel.Processor;
024 import org.apache.camel.processor.DeadLetterChannel;
025 import org.apache.camel.processor.RedeliveryPolicy;
026 import org.apache.camel.processor.SendProcessor;
027 import org.apache.camel.spi.RouteContext;
028 import org.apache.camel.util.CamelLogger;
029 import org.apache.camel.util.ObjectHelper;
030 import org.slf4j.LoggerFactory;
031
032 /**
033 * A builder of a <a
034 * href="http://camel.apache.org/dead-letter-channel.html">Dead Letter
035 * Channel</a>
036 *
037 * @version
038 */
039 public class DeadLetterChannelBuilder extends DefaultErrorHandlerBuilder {
040
041 public DeadLetterChannelBuilder() {
042 // no-arg constructor used by Spring DSL
043 }
044
045 public DeadLetterChannelBuilder(Endpoint deadLetter) {
046 setDeadLetter(deadLetter);
047 }
048
049 public DeadLetterChannelBuilder(String uri) {
050 setDeadLetterUri(uri);
051 }
052
053 public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
054 validateDeadLetterUri(routeContext);
055
056 DeadLetterChannel answer = new DeadLetterChannel(routeContext.getCamelContext(), processor, getLogger(), getOnRedelivery(),
057 getRedeliveryPolicy(), getExceptionPolicyStrategy(), getFailureProcessor(), getDeadLetterUri(), isUseOriginalMessage(),
058 getRetryWhilePolicy(routeContext.getCamelContext()), getExecutorService(routeContext.getCamelContext()));
059 // configure error handler before we can use it
060 configure(routeContext, answer);
061 return answer;
062 }
063
064 public boolean supportTransacted() {
065 return false;
066 }
067
068 // Properties
069 // -------------------------------------------------------------------------
070
071 public Processor getFailureProcessor() {
072 if (failureProcessor == null) {
073 // force MEP to be InOnly so when sending to DLQ we would not expect a reply if the MEP was InOut
074 failureProcessor = new SendProcessor(deadLetter, ExchangePattern.InOnly);
075 }
076 return failureProcessor;
077 }
078
079 protected void validateDeadLetterUri(RouteContext routeContext) {
080 if (deadLetter == null) {
081 ObjectHelper.notEmpty(deadLetterUri, "deadLetterUri", this);
082 deadLetter = routeContext.getCamelContext().getEndpoint(deadLetterUri);
083 if (deadLetter == null) {
084 throw new NoSuchEndpointException(deadLetterUri);
085 }
086 }
087 }
088
089 @Override
090 protected RedeliveryPolicy createRedeliveryPolicy() {
091 return new RedeliveryPolicy();
092 }
093
094 protected CamelLogger createLogger() {
095 return new CamelLogger(LoggerFactory.getLogger(DeadLetterChannel.class), LoggingLevel.ERROR);
096 }
097
098 @Override
099 public String toString() {
100 return "DeadLetterChannelBuilder(" + deadLetterUri + ")";
101 }
102 }