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;
026
027import org.apache.camel.model.LoadBalancerDefinition;
028import org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer;
029import org.apache.camel.processor.loadbalancer.LoadBalancer;
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.spi.RouteContext;
032import org.apache.camel.util.ObjectHelper;
033
034/**
035 * Circuit break load balancer
036 * <p/>
037 * The Circuit Breaker load balancer is a stateful pattern that monitors all calls for certain exceptions.
038 * Initially the Circuit Breaker is in closed state and passes all messages.
039 * If there are failures and the threshold is reached, it moves to open state and rejects all calls until halfOpenAfter
040 * timeout is reached. After this timeout is reached, if there is a new call, it will pass and if the result is
041 * success the Circuit Breaker will move to closed state, or to open state if there was an error.
042 */
043@Metadata(label = "configuration,loadbalance")
044@XmlRootElement(name = "circuitBreaker")
045@XmlAccessorType(XmlAccessType.FIELD)
046public class CircuitBreakerLoadBalancerDefinition extends LoadBalancerDefinition {
047    @XmlElement(name = "exception")
048    private List<String> exceptions = new ArrayList<String>();
049    @XmlAttribute
050    private Long halfOpenAfter;
051    @XmlAttribute
052    private Integer threshold;
053
054    public CircuitBreakerLoadBalancerDefinition() {
055    }
056
057    @Override
058    protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
059        CircuitBreakerLoadBalancer answer;
060
061        if (!exceptions.isEmpty()) {
062            List<Class<?>> classes = new ArrayList<Class<?>>();
063            for (String name : exceptions) {
064                Class<?> type = routeContext.getCamelContext().getClassResolver().resolveClass(name);
065                if (type == null) {
066                    throw new IllegalArgumentException("Cannot find class: " + name + " in the classpath");
067                }
068                if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) {
069                    throw new IllegalArgumentException("Class is not an instance of Throwable: " + type);
070                }
071                classes.add(type);
072            }
073            answer = new CircuitBreakerLoadBalancer(classes);
074        } else {
075            answer = new CircuitBreakerLoadBalancer();
076        }
077
078        if (getHalfOpenAfter() != null) {
079            answer.setHalfOpenAfter(getHalfOpenAfter());
080        }
081        if (getThreshold() != null) {
082            answer.setThreshold(getThreshold());
083        }
084        return answer;
085    }
086
087    public Long getHalfOpenAfter() {
088        return halfOpenAfter;
089    }
090
091    /**
092     * The timeout in millis to use as threshold to move state from closed to half-open or open state
093     */
094    public void setHalfOpenAfter(Long halfOpenAfter) {
095        this.halfOpenAfter = halfOpenAfter;
096    }
097
098    public Integer getThreshold() {
099        return threshold;
100    }
101
102    /**
103     * Number of previous failed messages to use as threshold to move state from closed to half-open or open state
104     */
105    public void setThreshold(Integer threshold) {
106        this.threshold = threshold;
107    }
108
109    public List<String> getExceptions() {
110        return exceptions;
111    }
112
113    /**
114     * A list of class names for specific exceptions to monitor.
115     * If no exceptions is configured then all exceptions is monitored
116     */
117    public void setExceptions(List<String> exceptions) {
118        this.exceptions = exceptions;
119    }
120
121    @Override
122    public String toString() {
123        return "CircuitBreakerLoadBalancer";
124    }
125}