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.impl; 018 019 import java.util.concurrent.ArrayBlockingQueue; 020 import java.util.concurrent.BlockingQueue; 021 import java.util.concurrent.TimeUnit; 022 023 import org.apache.camel.Consumer; 024 import org.apache.camel.Endpoint; 025 import org.apache.camel.Exchange; 026 import org.apache.camel.PollingConsumer; 027 import org.apache.camel.Processor; 028 import org.apache.camel.processor.Logger; 029 import org.apache.camel.spi.ExceptionHandler; 030 import org.apache.commons.logging.Log; 031 import org.apache.commons.logging.LogFactory; 032 033 /** 034 * A default implementation of the {@link PollingConsumer} which uses the normal 035 * asynchronous consumer mechanism along with a {@link BlockingQueue} to allow 036 * the caller to pull messages on demand. 037 * 038 * @version $Revision: 36321 $ 039 */ 040 public class EventDrivenPollingConsumer<E extends Exchange> extends PollingConsumerSupport<E> implements 041 Processor { 042 private static final transient Log LOG = LogFactory.getLog(EventDrivenPollingConsumer.class); 043 private BlockingQueue<E> queue; 044 private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(LOG)); 045 private Consumer<E> consumer; 046 047 public EventDrivenPollingConsumer(Endpoint<E> endpoint) { 048 this(endpoint, new ArrayBlockingQueue<E>(1000)); 049 } 050 051 public EventDrivenPollingConsumer(Endpoint<E> endpoint, BlockingQueue<E> queue) { 052 super(endpoint); 053 this.queue = queue; 054 } 055 056 public E receiveNoWait() { 057 return receive(0); 058 } 059 060 public E receive() { 061 while (isRunAllowed()) { 062 try { 063 return queue.take(); 064 } catch (InterruptedException e) { 065 handleInteruptedException(e); 066 } 067 } 068 return null; 069 } 070 071 public E receive(long timeout) { 072 try { 073 return queue.poll(timeout, TimeUnit.MILLISECONDS); 074 } catch (InterruptedException e) { 075 handleInteruptedException(e); 076 return null; 077 } 078 } 079 080 public void process(Exchange exchange) throws Exception { 081 queue.offer((E)exchange); 082 } 083 084 public ExceptionHandler getInteruptedExceptionHandler() { 085 return interuptedExceptionHandler; 086 } 087 088 public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) { 089 this.interuptedExceptionHandler = interuptedExceptionHandler; 090 } 091 092 protected void handleInteruptedException(InterruptedException e) { 093 getInteruptedExceptionHandler().handleException(e); 094 } 095 096 protected void doStart() throws Exception { 097 // lets add ourselves as a consumer 098 consumer = getEndpoint().createConsumer(this); 099 consumer.start(); 100 } 101 102 protected void doStop() throws Exception { 103 if (consumer != null) { 104 try { 105 consumer.stop(); 106 } finally { 107 consumer = null; 108 } 109 } 110 } 111 }