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.component.seda;
018    
019    import java.util.concurrent.BlockingQueue;
020    import java.util.concurrent.TimeUnit;
021    
022    import org.apache.camel.AsyncCallback;
023    import org.apache.camel.AsyncProcessor;
024    import org.apache.camel.Consumer;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Processor;
027    import org.apache.camel.impl.ServiceSupport;
028    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * A Consumer for the SEDA component.
034     *
035     * @version $Revision: 56884 $
036     */
037    public class SedaConsumer extends ServiceSupport implements Consumer, Runnable {
038        private static final transient Log LOG = LogFactory.getLog(SedaConsumer.class);
039    
040        private SedaEndpoint endpoint;
041        private AsyncProcessor processor;
042        private Thread thread;
043    
044        public SedaConsumer(SedaEndpoint endpoint, Processor processor) {
045            this.endpoint = endpoint;
046            this.processor = AsyncProcessorTypeConverter.convert(processor);
047        }
048    
049        @Override
050        public String toString() {
051            return "SedaConsumer: " + endpoint.getEndpointUri();
052        }
053    
054        public void run() {
055            BlockingQueue<Exchange> queue = endpoint.getQueue();
056            while (queue != null && isRunAllowed()) {
057                final Exchange exchange;
058                try {
059                    exchange = queue.poll(1000, TimeUnit.MILLISECONDS);
060                } catch (InterruptedException e) {
061                    if (LOG.isDebugEnabled()) {
062                        LOG.debug("Interupted: " + e, e);
063                    }
064                    continue;
065                }
066                if (exchange != null) {
067                    if (isRunAllowed()) {
068                        try {
069                            processor.process(exchange, new AsyncCallback() {
070                                public void done(boolean sync) {
071                                }
072                            });
073                        } catch (Exception e) {
074                            LOG.error("Seda queue caught: " + e, e);
075                        }
076                    } else {
077                        LOG.warn("This consumer is stopped during polling an exchange, so putting it back on the seda queue: " + exchange);
078                        try {
079                            queue.put(exchange);
080                        } catch (InterruptedException e) {
081                            if (LOG.isDebugEnabled()) {
082                                LOG.debug("Interupted: " + e, e);
083                            }
084                        }
085                    }
086                }
087            }
088        }
089    
090        protected void doStart() throws Exception {
091            thread = new Thread(this, getThreadName(endpoint.getEndpointUri()));
092            thread.setDaemon(true);
093            thread.start();
094        }
095    
096        protected void doStop() throws Exception {
097            thread.join();
098            thread = null;
099        }
100    
101    }