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@Metadata(label = "eip,routing,loadbalance") 045@XmlRootElement(name = "circuitBreaker") 046@XmlAccessorType(XmlAccessType.FIELD) 047public class CircuitBreakerLoadBalancerDefinition extends LoadBalancerDefinition { 048 @XmlTransient 049 private List<Class<?>> exceptionTypes = new ArrayList<Class<?>>(); 050 @XmlElement(name = "exception") 051 private List<String> exceptions = new ArrayList<String>(); 052 @XmlAttribute 053 private Long halfOpenAfter; 054 @XmlAttribute 055 private Integer threshold; 056 057 public CircuitBreakerLoadBalancerDefinition() { 058 } 059 060 @Override 061 protected int getMaximumNumberOfOutputs() { 062 // we can only support 1 output 063 return 1; 064 } 065 066 @Override 067 protected LoadBalancer createLoadBalancer(RouteContext routeContext) { 068 CircuitBreakerLoadBalancer answer; 069 070 List<Class<?>> classes = new ArrayList<Class<?>>(); 071 if (!exceptionTypes.isEmpty()) { 072 classes.addAll(exceptionTypes); 073 } else if (!exceptions.isEmpty()) { 074 for (String name : exceptions) { 075 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name); 076 if (type == null) { 077 throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath"); 078 } 079 if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) { 080 throw new IllegalArgumentException("Class is not an instance of Throwable: " + type); 081 } 082 classes.add(type); 083 } 084 } 085 if (classes.isEmpty()) { 086 answer = new CircuitBreakerLoadBalancer(); 087 } else { 088 answer = new CircuitBreakerLoadBalancer(classes); 089 } 090 091 if (getHalfOpenAfter() != null) { 092 answer.setHalfOpenAfter(getHalfOpenAfter()); 093 } 094 if (getThreshold() != null) { 095 answer.setThreshold(getThreshold()); 096 } 097 return answer; 098 } 099 100 public Long getHalfOpenAfter() { 101 return halfOpenAfter; 102 } 103 104 /** 105 * The timeout in millis to use as threshold to move state from closed to half-open or open state 106 */ 107 public void setHalfOpenAfter(Long halfOpenAfter) { 108 this.halfOpenAfter = halfOpenAfter; 109 } 110 111 public Integer getThreshold() { 112 return threshold; 113 } 114 115 /** 116 * Number of previous failed messages to use as threshold to move state from closed to half-open or open state 117 */ 118 public void setThreshold(Integer threshold) { 119 this.threshold = threshold; 120 } 121 122 public List<String> getExceptions() { 123 return exceptions; 124 } 125 126 /** 127 * A list of class names for specific exceptions to monitor. 128 * If no exceptions is configured then all exceptions is monitored 129 */ 130 public void setExceptions(List<String> exceptions) { 131 this.exceptions = exceptions; 132 } 133 134 public List<Class<?>> getExceptionTypes() { 135 return exceptionTypes; 136 } 137 138 /** 139 * A list of specific exceptions to monitor. 140 * If no exceptions is configured then all exceptions is monitored 141 */ 142 public void setExceptionTypes(List<Class<?>> exceptionTypes) { 143 this.exceptionTypes = exceptionTypes; 144 } 145 146 @Override 147 public String toString() { 148 return "CircuitBreakerLoadBalancer"; 149 } 150}