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: 751655 $ 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 LOG.debug("Sleep interrupted, are we stopping? " + (isStopping() || isStopped())); 065 continue; 066 } 067 if (exchange != null) { 068 if (isRunAllowed()) { 069 try { 070 processor.process(exchange, new AsyncCallback() { 071 public void done(boolean sync) { 072 } 073 }); 074 } catch (Exception e) { 075 LOG.error("Seda queue caught: " + e, e); 076 } 077 } else { 078 LOG.warn("This consumer is stopped during polling an exchange, so putting it back on the seda queue: " + exchange); 079 try { 080 queue.put(exchange); 081 } catch (InterruptedException e) { 082 LOG.debug("Sleep interrupted, are we stopping? " + (isStopping() || isStopped())); 083 } 084 } 085 } 086 } 087 } 088 089 protected void doStart() throws Exception { 090 int concurrentConsumers = endpoint.getConcurrentConsumers(); 091 executor = Executors.newFixedThreadPool(concurrentConsumers, new ThreadFactory() { 092 093 public Thread newThread(Runnable runnable) { 094 Thread thread = new Thread(runnable, getThreadName(endpoint.getEndpointUri())); 095 thread.setDaemon(true); 096 return thread; 097 } 098 }); 099 for (int i = 0; i < concurrentConsumers; i++) { 100 executor.execute(this); 101 } 102 endpoint.onStarted(this); 103 } 104 105 protected void doStop() throws Exception { 106 endpoint.onStopped(this); 107 108 executor.shutdownNow(); 109 executor = null; 110 } 111 112 }