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     */
017    package org.apache.camel.processor.loadbalancer;
018    
019    import java.util.List;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.util.ObjectHelper;
024    
025    /**
026     * This FailOverLoadBalancer will failover to use next processor when an exception occured
027     */
028    public class FailOverLoadBalancer extends LoadBalancerSupport {
029    
030        private final List<Class<?>> exceptions;
031    
032        public FailOverLoadBalancer() {
033            this.exceptions = null;
034        }
035    
036        public FailOverLoadBalancer(List<Class<?>> exceptions) {
037            this.exceptions = exceptions;
038            for (Class<?> type : exceptions) {
039                if (!ObjectHelper.isAssignableFrom(Throwable.class, type)) {
040                    throw new IllegalArgumentException("Class is not an instance of Throwable: " + type);
041                }
042            }
043        }
044    
045        public List<Class<?>> getExceptions() {
046            return exceptions;
047        }
048    
049        /**
050         * Should the given failed Exchange failover?
051         *
052         * @param exchange the exchange that failed
053         * @return <tt>true</tt> to failover
054         */
055        protected boolean shouldFailOver(Exchange exchange) {
056            if (exchange.getException() != null) {
057    
058                if (exceptions == null || exceptions.isEmpty()) {
059                    // always failover if no exceptions defined
060                    return true;
061                }
062    
063                for (Class<?> exception : exceptions) {
064                    // will look in exception hierarchy 
065                    if (exchange.getException(exception) != null) {
066                        return true;
067                    }
068                }
069            }
070    
071            return false;
072        }
073    
074        public void process(Exchange exchange) throws Exception {
075            List<Processor> list = getProcessors();
076            if (list.isEmpty()) {
077                throw new IllegalStateException("No processors available to process " + exchange);
078            }
079    
080            int index = 0;
081            Processor processor = list.get(index);
082    
083            // process the first time
084            processExchange(processor, exchange);
085    
086            // loop while we should fail over
087            while (shouldFailOver(exchange)) {
088                index++;
089                if (index < list.size()) {
090                    // try again but prepare exchange before we failover
091                    prepareExchangeForFailover(exchange);
092                    processor = list.get(index);
093                    processExchange(processor, exchange);
094                } else {
095                    // no more processors to try
096                    break;
097                }
098            }
099        }
100    
101        /**
102         * Prepares the exchange for failover
103         *
104         * @param exchange the exchange
105         */
106        protected void prepareExchangeForFailover(Exchange exchange) {
107            exchange.setException(null);
108    
109            exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, null);
110            exchange.setProperty(Exchange.FAILURE_HANDLED, null);
111            exchange.setProperty(Exchange.EXCEPTION_CAUGHT, null);
112            exchange.getIn().removeHeader(Exchange.REDELIVERED);
113            exchange.getIn().removeHeader(Exchange.REDELIVERY_COUNTER);
114        }
115    
116        private void processExchange(Processor processor, Exchange exchange) {
117            if (processor == null) {
118                throw new IllegalStateException("No processors could be chosen to process " + exchange);
119            }
120            try {
121                processor.process(exchange);
122            } catch (Exception e) {
123                exchange.setException(e);
124            }
125        }
126    
127        public String toString() {
128            return "FailoverLoadBalancer";
129        }
130    
131    }