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.TimeUnit;
020    
021    import org.apache.camel.AsyncCallback;
022    import org.apache.camel.AsyncProcessor;
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.impl.ServiceSupport;
027    import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * @version $Revision: 36321 $
033     */
034    public class SedaConsumer extends ServiceSupport implements Consumer, Runnable {
035        private static final transient Log LOG = LogFactory.getLog(SedaConsumer.class);
036    
037        private SedaEndpoint endpoint;
038        private AsyncProcessor processor;
039        private Thread thread;
040    
041        public SedaConsumer(SedaEndpoint endpoint, Processor processor) {
042            this.endpoint = endpoint;
043            this.processor = AsyncProcessorTypeConverter.convert(processor);
044        }
045    
046        @Override
047        public String toString() {
048            return "SedaConsumer: " + endpoint.getEndpointUri();
049        }
050    
051        public void run() {
052            while (isRunAllowed()) {
053                final Exchange exchange;
054                try {
055                    exchange = endpoint.getQueue().poll(1000, TimeUnit.MILLISECONDS);
056                } catch (InterruptedException e) {
057                    if (LOG.isDebugEnabled()) {
058                        LOG.debug("Interupted: " + e, e);
059                    }
060                    continue;
061                }
062                if (exchange != null && isRunAllowed()) {
063                    try {
064                        processor.process(exchange, new AsyncCallback() {
065                            public void done(boolean sync) {
066                            }
067                        });
068                    } catch (Exception e) {
069                        LOG.error("Seda queue caught: " + e, e);
070                    }
071                }
072            }
073        }
074    
075        protected void doStart() throws Exception {
076            thread = new Thread(this, getThreadName(endpoint.getEndpointUri()));
077            thread.setDaemon(true);
078            thread.start();
079        }
080    
081        protected void doStop() throws Exception {
082            thread.join();
083        }
084    
085    }