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.spi.Metadata;
031
032/**
033 * Failover load balancer The failover load balancer is capable of trying the
034 * next processor in case an Exchange failed with an exception during
035 * processing. You can constrain the failover to activate only when one
036 * exception of a list you specify occurs. If you do not specify a list any
037 * exception will cause fail over to occur. This balancer uses the same strategy
038 * for matching exceptions as the Exception Clause does for the onException.
039 */
040@Metadata(label = "eip,routing,loadbalance")
041@XmlRootElement(name = "failover")
042@XmlAccessorType(XmlAccessType.FIELD)
043public class FailoverLoadBalancerDefinition extends LoadBalancerDefinition {
044    @XmlTransient
045    private List<Class<?>> exceptionTypes = new ArrayList<>();
046    @XmlElement(name = "exception")
047    private List<String> exceptions = new ArrayList<>();
048    @XmlAttribute
049    private Boolean roundRobin;
050    @XmlAttribute
051    private Boolean sticky;
052    @XmlAttribute
053    @Metadata(defaultValue = "-1")
054    private Integer maximumFailoverAttempts;
055
056    public FailoverLoadBalancerDefinition() {
057    }
058
059    public List<String> getExceptions() {
060        return exceptions;
061    }
062
063    /**
064     * A list of class names for specific exceptions to monitor. If no
065     * exceptions is configured then all exceptions is monitored
066     */
067    public void setExceptions(List<String> exceptions) {
068        this.exceptions = exceptions;
069    }
070
071    public List<Class<?>> getExceptionTypes() {
072        return exceptionTypes;
073    }
074
075    /**
076     * A list of specific exceptions to monitor. If no exceptions is configured
077     * then all exceptions is monitored
078     */
079    public void setExceptionTypes(List<Class<?>> exceptionTypes) {
080        this.exceptionTypes = exceptionTypes;
081    }
082
083    public Boolean getRoundRobin() {
084        return roundRobin;
085    }
086
087    /**
088     * Whether or not the failover load balancer should operate in round robin
089     * mode or not. If not, then it will always start from the first endpoint
090     * when a new message is to be processed. In other words it restart from the
091     * top for every message. If round robin is enabled, then it keeps state and
092     * will continue with the next endpoint in a round robin fashion.
093     * <p/>
094     * You can also enable sticky mode together with round robin, if so then it
095     * will pick the last known good endpoint to use when starting the load
096     * balancing (instead of using the next when starting).
097     */
098    public void setRoundRobin(Boolean roundRobin) {
099        this.roundRobin = roundRobin;
100    }
101
102    public Boolean getSticky() {
103        return sticky;
104    }
105
106    /**
107     * Whether or not the failover load balancer should operate in sticky mode
108     * or not. If not, then it will always start from the first endpoint when a
109     * new message is to be processed. In other words it restart from the top
110     * for every message. If sticky is enabled, then it keeps state and will
111     * continue with the last known good endpoint.
112     * <p/>
113     * You can also enable sticky mode together with round robin, if so then it
114     * will pick the last known good endpoint to use when starting the load
115     * balancing (instead of using the next when starting).
116     */
117    public void setSticky(Boolean sticky) {
118        this.sticky = sticky;
119    }
120
121    public Integer getMaximumFailoverAttempts() {
122        return maximumFailoverAttempts;
123    }
124
125    /**
126     * A value to indicate after X failover attempts we should exhaust (give
127     * up). Use -1 to indicate never give up and continuously try to failover.
128     * Use 0 to never failover. And use e.g. 3 to failover at most 3 times
129     * before giving up. his option can be used whether or not roundRobin is
130     * enabled or not.
131     */
132    public void setMaximumFailoverAttempts(Integer maximumFailoverAttempts) {
133        this.maximumFailoverAttempts = maximumFailoverAttempts;
134    }
135
136    @Override
137    public String toString() {
138        return "FailoverLoadBalancer";
139    }
140}