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.spring.spi;
018
019 import org.apache.camel.Processor;
020 import org.apache.camel.builder.ErrorHandlerBuilder;
021 import org.apache.camel.builder.ErrorHandlerBuilderSupport;
022 import org.apache.camel.processor.RedeliveryPolicy;
023 import org.apache.camel.spi.RouteContext;
024 import org.apache.camel.util.ObjectHelper;
025 import org.springframework.beans.factory.InitializingBean;
026 import org.springframework.transaction.support.TransactionTemplate;
027
028 /**
029 * An error handler which will roll the exception back if there is an error
030 * rather than using the dead letter channel and retry logic.
031 *
032 * A delay is also used after a rollback
033 *
034 * @version $Revision: 1.1 $
035 */
036 public class TransactionErrorHandlerBuilder extends ErrorHandlerBuilderSupport implements Cloneable, InitializingBean {
037
038 private TransactionTemplate transactionTemplate;
039 private RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
040
041 public TransactionErrorHandlerBuilder() {
042 }
043
044 public TransactionTemplate getTransactionTemplate() {
045 return transactionTemplate;
046 }
047
048 public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
049 this.transactionTemplate = transactionTemplate;
050 }
051
052 public RedeliveryPolicy getRedeliveryPolicy() {
053 return redeliveryPolicy;
054 }
055
056 public void setRedeliveryPolicy(RedeliveryPolicy redeliveryPolicy) {
057 this.redeliveryPolicy = redeliveryPolicy;
058 }
059
060 public ErrorHandlerBuilder copy() {
061 try {
062 return (ErrorHandlerBuilder) clone();
063 } catch (CloneNotSupportedException e) {
064 throw new Error("Clone should be supported: " + e, e);
065 }
066 }
067
068 public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
069 return new TransactionInterceptor(processor, transactionTemplate, redeliveryPolicy);
070 }
071
072 public void afterPropertiesSet() throws Exception {
073 ObjectHelper.notNull(transactionTemplate, "transactionTemplate");
074 }
075
076 // Builder methods
077 // -------------------------------------------------------------------------
078 public TransactionErrorHandlerBuilder backOffMultiplier(double backOffMultiplier) {
079 getRedeliveryPolicy().backOffMultiplier(backOffMultiplier);
080 return this;
081 }
082
083 public TransactionErrorHandlerBuilder collisionAvoidancePercent(short collisionAvoidancePercent) {
084 getRedeliveryPolicy().collisionAvoidancePercent(collisionAvoidancePercent);
085 return this;
086 }
087
088 public TransactionErrorHandlerBuilder initialRedeliveryDelay(long initialRedeliveryDelay) {
089 getRedeliveryPolicy().initialRedeliveryDelay(initialRedeliveryDelay);
090 return this;
091 }
092
093 public TransactionErrorHandlerBuilder maximumRedeliveries(int maximumRedeliveries) {
094 getRedeliveryPolicy().maximumRedeliveries(maximumRedeliveries);
095 return this;
096 }
097
098 public TransactionErrorHandlerBuilder maximumRedeliveryDelay(long maximumRedeliveryDelay) {
099 getRedeliveryPolicy().maximumRedeliveryDelay(maximumRedeliveryDelay);
100 return this;
101 }
102
103 public TransactionErrorHandlerBuilder useCollisionAvoidance() {
104 getRedeliveryPolicy().useCollisionAvoidance();
105 return this;
106 }
107
108 public TransactionErrorHandlerBuilder useExponentialBackOff() {
109 getRedeliveryPolicy().useExponentialBackOff();
110 return this;
111 }
112
113 }