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.ArrayList;
020    import java.util.List;
021    import java.util.concurrent.CopyOnWriteArrayList;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Navigate;
025    import org.apache.camel.Processor;
026    import org.apache.camel.support.ServiceSupport;
027    import org.apache.camel.util.AsyncProcessorHelper;
028    import org.apache.camel.util.ServiceHelper;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * A default base class for a {@link LoadBalancer} implementation.
034     * <p/>
035     * This implementation is dedicated for asynchronous load balancers.
036     * <p/>
037     * Consider using the {@link SimpleLoadBalancerSupport} if your load balancer does not by nature
038     * support asynchronous routing.
039     *
040     * @version 
041     */
042    public abstract class LoadBalancerSupport extends ServiceSupport implements LoadBalancer, Navigate<Processor> {
043    
044        protected final Logger log = LoggerFactory.getLogger(getClass());
045        private final List<Processor> processors = new CopyOnWriteArrayList<Processor>();
046    
047        public void addProcessor(Processor processor) {
048            processors.add(processor);
049        }
050    
051        public void removeProcessor(Processor processor) {
052            processors.remove(processor);
053        }
054    
055        public List<Processor> getProcessors() {
056            return processors;
057        }
058    
059        public List<Processor> next() {
060            if (!hasNext()) {
061                return null;
062            }
063            return new ArrayList<Processor>(processors);
064        }
065    
066        public boolean hasNext() {
067            return processors.size() > 0;
068        }
069    
070        protected void doStart() throws Exception {
071            ServiceHelper.startServices(processors);
072        }
073    
074        protected void doStop() throws Exception {
075            ServiceHelper.stopServices(processors);
076        }
077    
078        @Override
079        protected void doShutdown() throws Exception {
080            ServiceHelper.stopAndShutdownServices(processors);
081            for (Processor processor : processors) {
082                removeProcessor(processor);
083            }
084        }
085    
086        public void process(Exchange exchange) throws Exception {
087            AsyncProcessorHelper.process(this, exchange);
088        }
089    }