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 = "configuration,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 LoadBalancer createLoadBalancer(RouteContext routeContext) { 062 CircuitBreakerLoadBalancer answer; 063 064 List<Class<?>> classes = new ArrayList<Class<?>>(); 065 if (!exceptionTypes.isEmpty()) { 066 classes.addAll(exceptionTypes); 067 } else if (!exceptions.isEmpty()) { 068 for (String name : exceptions) { 069 Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name); 070 if (type == null) { 071 throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath"); 072 } 073 if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) { 074 throw new IllegalArgumentException("Class is not an instance of Throwable: " + type); 075 } 076 classes.add(type); 077 } 078 } 079 if (classes.isEmpty()) { 080 answer = new CircuitBreakerLoadBalancer(); 081 } else { 082 answer = new CircuitBreakerLoadBalancer(classes); 083 } 084 085 if (getHalfOpenAfter() != null) { 086 answer.setHalfOpenAfter(getHalfOpenAfter()); 087 } 088 if (getThreshold() != null) { 089 answer.setThreshold(getThreshold()); 090 } 091 return answer; 092 } 093 094 public Long getHalfOpenAfter() { 095 return halfOpenAfter; 096 } 097 098 /** 099 * The timeout in millis to use as threshold to move state from closed to half-open or open state 100 */ 101 public void setHalfOpenAfter(Long halfOpenAfter) { 102 this.halfOpenAfter = halfOpenAfter; 103 } 104 105 public Integer getThreshold() { 106 return threshold; 107 } 108 109 /** 110 * Number of previous failed messages to use as threshold to move state from closed to half-open or open state 111 */ 112 public void setThreshold(Integer threshold) { 113 this.threshold = threshold; 114 } 115 116 public List<String> getExceptions() { 117 return exceptions; 118 } 119 120 /** 121 * A list of class names for specific exceptions to monitor. 122 * If no exceptions is configured then all exceptions is monitored 123 */ 124 public void setExceptions(List<String> exceptions) { 125 this.exceptions = exceptions; 126 } 127 128 public List<Class<?>> getExceptionTypes() { 129 return exceptionTypes; 130 } 131 132 /** 133 * A list of specific exceptions to monitor. 134 * If no exceptions is configured then all exceptions is monitored 135 */ 136 public void setExceptionTypes(List<Class<?>> exceptionTypes) { 137 this.exceptionTypes = exceptionTypes; 138 } 139 140 @Override 141 public String toString() { 142 return "CircuitBreakerLoadBalancer"; 143 } 144}