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