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