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