001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.impl; 019 020 import org.apache.camel.Consumer; 021 import org.apache.camel.Endpoint; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Processor; 024 import org.apache.camel.PollingConsumer; 025 import org.apache.camel.processor.Logger; 026 import org.apache.camel.spi.ExceptionHandler; 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 030 import java.util.concurrent.ArrayBlockingQueue; 031 import java.util.concurrent.BlockingQueue; 032 import java.util.concurrent.TimeUnit; 033 034 /** 035 * A default implementation of the {@link PollingConsumer} which uses the normal asynchronous consumer mechanism 036 * along with a {@link BlockingQueue} to allow the caller to pull messages on demand. 037 * 038 * @version $Revision: 1.1 $ 039 */ 040 public class DefaultPollingConsumer<E extends Exchange> extends PollingConsumerSupport<E> implements Processor { 041 private static final transient Log log = LogFactory.getLog(DefaultPollingConsumer.class); 042 private BlockingQueue<E> queue; 043 private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(log)); 044 private Consumer<E> consumer; 045 046 public DefaultPollingConsumer(Endpoint<E> endpoint) { 047 this(endpoint, new ArrayBlockingQueue<E>(1000)); 048 } 049 050 public DefaultPollingConsumer(Endpoint<E> endpoint, BlockingQueue<E> queue) { 051 super(endpoint); 052 this.queue = queue; 053 } 054 055 public E receiveNoWait() { 056 return receive(0); 057 } 058 059 public E receive() { 060 while (!isStopping() && !isStopped()) { 061 try { 062 return queue.take(); 063 } 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 } 075 catch (InterruptedException e) { 076 handleInteruptedException(e); 077 return null; 078 } 079 } 080 081 public void process(Exchange exchange) throws Exception { 082 queue.offer((E) exchange); 083 } 084 085 public ExceptionHandler getInteruptedExceptionHandler() { 086 return interuptedExceptionHandler; 087 } 088 089 public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) { 090 this.interuptedExceptionHandler = interuptedExceptionHandler; 091 } 092 093 protected void handleInteruptedException(InterruptedException e) { 094 getInteruptedExceptionHandler().handleException(e); 095 } 096 097 protected void doStart() throws Exception { 098 // lets add ourselves as a consumer 099 consumer = getEndpoint().createConsumer(this); 100 consumer.start(); 101 } 102 103 protected void doStop() throws Exception { 104 if (consumer != null) { 105 try { 106 consumer.stop(); 107 } 108 finally { 109 consumer = null; 110 } 111 } 112 } 113 }