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