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.model.loadbalancer; 018 019import java.util.ArrayList; 020import java.util.List; 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAttribute; 024import javax.xml.bind.annotation.XmlElement; 025import javax.xml.bind.annotation.XmlRootElement; 026import javax.xml.bind.annotation.XmlTransient; 027 028import org.apache.camel.model.LoadBalancerDefinition; 029import org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer; 030import org.apache.camel.processor.loadbalancer.LoadBalancer; 031import org.apache.camel.spi.Metadata; 032import org.apache.camel.spi.RouteContext; 033import org.apache.camel.util.ObjectHelper; 034 035/** 036 * Circuit break load balancer 037 * <p/> 038 * The Circuit Breaker load balancer is a stateful pattern that monitors all calls for certain exceptions. 039 * Initially the Circuit Breaker is in closed state and passes all messages. 040 * If there are failures and the threshold is reached, it moves to open state and rejects all calls until halfOpenAfter 041 * timeout is reached. After this timeout is reached, if there is a new call, it will pass and if the result is 042 * success the Circuit Breaker will move to closed state, or to open state if there was an error. 043 * 044 * @deprecated use Hystrix EIP instead which is the popular Netflix implementation of circuit breaker 045 */ 046@Metadata(label = "eip,routing,loadbalance,circuitbreaker") 047@XmlRootElement(name = "circuitBreaker") 048@XmlAccessorType(XmlAccessType.FIELD) 049@Deprecated 050public class CircuitBreakerLoadBalancerDefinition extends LoadBalancerDefinition { 051 @XmlTransient 052 private List<Class<?>> exceptionTypes = new ArrayList<Class<?>>(); 053 @XmlElement(name = "exception") 054 private List<String> exceptions = new ArrayList<String>(); 055 @XmlAttribute 056 private Long halfOpenAfter; 057 @XmlAttribute 058 private Integer threshold; 059 060 public CircuitBreakerLoadBalancerDefinition() { 061 } 062 063 @Override 064 protected int getMaximumNumberOfOutputs() { 065 // we can only support 1 output 066 return 1; 067 } 068 069 @Override 070 protected LoadBalancer createLoadBalancer(RouteContext routeContext) { 071 CircuitBreakerLoadBalancer answer; 072 073 List<Class<?>> classes = new ArrayList<Class<?>>(); 074 if (!exceptionTypes.isEmpty()) { 075 classes.addAll(exceptionTypes); 076 } else if (!exceptions.isEmpty()) { 077 for (String name : exceptions) { 078 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name); 079 if (type == null) { 080 throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath"); 081 } 082 if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) { 083 throw new IllegalArgumentException("Class is not an instance of Throwable: " + type); 084 } 085 classes.add(type); 086 } 087 } 088 if (classes.isEmpty()) { 089 answer = new CircuitBreakerLoadBalancer(); 090 } else { 091 answer = new CircuitBreakerLoadBalancer(classes); 092 } 093 094 if (getHalfOpenAfter() != null) { 095 answer.setHalfOpenAfter(getHalfOpenAfter()); 096 } 097 if (getThreshold() != null) { 098 answer.setThreshold(getThreshold()); 099 } 100 return answer; 101 } 102 103 public Long getHalfOpenAfter() { 104 return halfOpenAfter; 105 } 106 107 /** 108 * The timeout in millis to use as threshold to move state from closed to half-open or open state 109 */ 110 public void setHalfOpenAfter(Long halfOpenAfter) { 111 this.halfOpenAfter = halfOpenAfter; 112 } 113 114 public Integer getThreshold() { 115 return threshold; 116 } 117 118 /** 119 * Number of previous failed messages to use as threshold to move state from closed to half-open or open state 120 */ 121 public void setThreshold(Integer threshold) { 122 this.threshold = threshold; 123 } 124 125 public List<String> getExceptions() { 126 return exceptions; 127 } 128 129 /** 130 * A list of class names for specific exceptions to monitor. 131 * If no exceptions is configured then all exceptions is monitored 132 */ 133 public void setExceptions(List<String> exceptions) { 134 this.exceptions = exceptions; 135 } 136 137 public List<Class<?>> getExceptionTypes() { 138 return exceptionTypes; 139 } 140 141 /** 142 * A list of specific exceptions to monitor. 143 * If no exceptions is configured then all exceptions is monitored 144 */ 145 public void setExceptionTypes(List<Class<?>> exceptionTypes) { 146 this.exceptionTypes = exceptionTypes; 147 } 148 149 @Override 150 public String toString() { 151 return "CircuitBreakerLoadBalancer"; 152 } 153}