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