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.Expression; 020 import org.apache.camel.Processor; 021 import org.apache.camel.processor.DeadLetterChannel; 022 import org.apache.camel.processor.ErrorHandlerSupport; 023 import org.apache.camel.processor.Logger; 024 import org.apache.camel.processor.LoggingLevel; 025 import org.apache.camel.processor.RecipientList; 026 import org.apache.camel.processor.RedeliveryPolicy; 027 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; 028 import org.apache.camel.spi.RouteContext; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 /** 033 * A builder of a <a 034 * href="http://activemq.apache.org/camel/dead-letter-channel.html">Dead Letter 035 * Channel</a> 036 * 037 * @version $Revision: 47490 $ 038 */ 039 public class DeadLetterChannelBuilder extends ErrorHandlerBuilderSupport { 040 private RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy(); 041 private ExceptionPolicyStrategy exceptionPolicyStrategy = ErrorHandlerSupport.createDefaultExceptionPolicyStrategy(); 042 private ProcessorFactory deadLetterFactory; 043 private Processor defaultDeadLetterEndpoint; 044 private Expression defaultDeadLetterEndpointExpression; 045 private String defaultDeadLetterEndpointUri = "log:org.apache.camel.DeadLetterChannel?level=error"; 046 private Logger logger = DeadLetterChannel.createDefaultLogger(); 047 048 public DeadLetterChannelBuilder() { 049 } 050 051 public DeadLetterChannelBuilder(Processor processor) { 052 this(new ConstantProcessorBuilder(processor)); 053 } 054 055 public DeadLetterChannelBuilder(ProcessorFactory deadLetterFactory) { 056 this.deadLetterFactory = deadLetterFactory; 057 } 058 059 public ErrorHandlerBuilder copy() { 060 DeadLetterChannelBuilder answer = new DeadLetterChannelBuilder(deadLetterFactory); 061 answer.setRedeliveryPolicy(getRedeliveryPolicy().copy()); 062 return answer; 063 } 064 065 public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception { 066 Processor deadLetter = getDeadLetterFactory().createProcessor(); 067 DeadLetterChannel answer = new DeadLetterChannel(processor, deadLetter, getRedeliveryPolicy(), getLogger(), getExceptionPolicyStrategy()); 068 configure(answer); 069 return answer; 070 } 071 072 // Builder methods 073 // ------------------------------------------------------------------------- 074 public DeadLetterChannelBuilder backOffMultiplier(double backOffMultiplier) { 075 getRedeliveryPolicy().backOffMultiplier(backOffMultiplier); 076 return this; 077 } 078 079 public DeadLetterChannelBuilder collisionAvoidancePercent(short collisionAvoidancePercent) { 080 getRedeliveryPolicy().collisionAvoidancePercent(collisionAvoidancePercent); 081 return this; 082 } 083 084 public DeadLetterChannelBuilder initialRedeliveryDelay(long initialRedeliveryDelay) { 085 getRedeliveryPolicy().initialRedeliveryDelay(initialRedeliveryDelay); 086 return this; 087 } 088 089 public DeadLetterChannelBuilder maximumRedeliveries(int maximumRedeliveries) { 090 getRedeliveryPolicy().maximumRedeliveries(maximumRedeliveries); 091 return this; 092 } 093 094 public DeadLetterChannelBuilder maximumRedeliveryDelay(long maximumRedeliveryDelay) { 095 getRedeliveryPolicy().maximumRedeliveryDelay(maximumRedeliveryDelay); 096 return this; 097 } 098 099 public DeadLetterChannelBuilder useCollisionAvoidance() { 100 getRedeliveryPolicy().useCollisionAvoidance(); 101 return this; 102 } 103 104 public DeadLetterChannelBuilder useExponentialBackOff() { 105 getRedeliveryPolicy().useExponentialBackOff(); 106 return this; 107 } 108 109 /** 110 * Sets the logger used for caught exceptions 111 */ 112 public DeadLetterChannelBuilder logger(Logger logger) { 113 setLogger(logger); 114 return this; 115 } 116 117 /** 118 * Sets the logging level of exceptions caught 119 */ 120 public DeadLetterChannelBuilder loggingLevel(LoggingLevel level) { 121 getLogger().setLevel(level); 122 return this; 123 } 124 125 /** 126 * Sets the log used for caught exceptions 127 */ 128 public DeadLetterChannelBuilder log(Log log) { 129 getLogger().setLog(log); 130 return this; 131 } 132 133 /** 134 * Sets the log used for caught exceptions 135 */ 136 public DeadLetterChannelBuilder log(String log) { 137 return log(LogFactory.getLog(log)); 138 } 139 140 /** 141 * Sets the log used for caught exceptions 142 */ 143 public DeadLetterChannelBuilder log(Class log) { 144 return log(LogFactory.getLog(log)); 145 } 146 147 /** 148 * Sets the exception policy to use 149 */ 150 public ErrorHandlerBuilderSupport exceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 151 setExceptionPolicyStrategy(exceptionPolicyStrategy); 152 return this; 153 } 154 155 // Properties 156 // ------------------------------------------------------------------------- 157 public RedeliveryPolicy getRedeliveryPolicy() { 158 return redeliveryPolicy; 159 } 160 161 /** 162 * Sets the redelivery policy 163 */ 164 public void setRedeliveryPolicy(RedeliveryPolicy redeliveryPolicy) { 165 this.redeliveryPolicy = redeliveryPolicy; 166 } 167 168 public ProcessorFactory getDeadLetterFactory() { 169 if (deadLetterFactory == null) { 170 deadLetterFactory = new ProcessorFactory() { 171 public Processor createProcessor() { 172 return getDefaultDeadLetterEndpoint(); 173 } 174 }; 175 } 176 return deadLetterFactory; 177 } 178 179 /** 180 * Sets the default dead letter queue factory 181 */ 182 public void setDeadLetterFactory(ProcessorFactory deadLetterFactory) { 183 this.deadLetterFactory = deadLetterFactory; 184 } 185 186 public Processor getDefaultDeadLetterEndpoint() { 187 if (defaultDeadLetterEndpoint == null) { 188 defaultDeadLetterEndpoint = new RecipientList(getDefaultDeadLetterEndpointExpression()); 189 } 190 return defaultDeadLetterEndpoint; 191 } 192 193 /** 194 * Sets the default dead letter endpoint used 195 */ 196 public void setDefaultDeadLetterEndpoint(Processor defaultDeadLetterEndpoint) { 197 this.defaultDeadLetterEndpoint = defaultDeadLetterEndpoint; 198 } 199 200 public Expression getDefaultDeadLetterEndpointExpression() { 201 if (defaultDeadLetterEndpointExpression == null) { 202 defaultDeadLetterEndpointExpression = ExpressionBuilder 203 .constantExpression(getDefaultDeadLetterEndpointUri()); 204 } 205 return defaultDeadLetterEndpointExpression; 206 } 207 208 /** 209 * Sets the expression used to decide the dead letter channel endpoint for 210 * an exchange if no factory is provided via 211 * {@link #setDeadLetterFactory(ProcessorFactory)} 212 */ 213 public void setDefaultDeadLetterEndpointExpression(Expression defaultDeadLetterEndpointExpression) { 214 this.defaultDeadLetterEndpointExpression = defaultDeadLetterEndpointExpression; 215 } 216 217 public String getDefaultDeadLetterEndpointUri() { 218 return defaultDeadLetterEndpointUri; 219 } 220 221 /** 222 * Sets the default dead letter endpoint URI used if no factory is provided 223 * via {@link #setDeadLetterFactory(ProcessorFactory)} and no expression is 224 * provided via {@link #setDefaultDeadLetterEndpointExpression(Expression)} 225 * 226 * @param defaultDeadLetterEndpointUri the default URI if no deadletter 227 * factory or expression is provided 228 */ 229 public void setDefaultDeadLetterEndpointUri(String defaultDeadLetterEndpointUri) { 230 this.defaultDeadLetterEndpointUri = defaultDeadLetterEndpointUri; 231 } 232 233 public Logger getLogger() { 234 return logger; 235 } 236 237 public void setLogger(Logger logger) { 238 this.logger = logger; 239 } 240 241 /** 242 * Sets the exception policy strategy to use for resolving the {@link org.apache.camel.model.ExceptionType} 243 * to use for a given thrown exception 244 */ 245 public ExceptionPolicyStrategy getExceptionPolicyStrategy() { 246 return exceptionPolicyStrategy; 247 } 248 249 public void setExceptionPolicyStrategy(ExceptionPolicyStrategy exceptionPolicyStrategy) { 250 this.exceptionPolicyStrategy = exceptionPolicyStrategy; 251 } 252 253 }