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