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.Processor;
027 import org.apache.camel.processor.Logger;
028 import org.apache.camel.spi.ExceptionHandler;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * A default implementation of the {@link org.apache.camel.PollingConsumer} which uses the normal
034 * asynchronous consumer mechanism along with a {@link BlockingQueue} to allow
035 * the caller to pull messages on demand.
036 *
037 * @version $Revision: 751655 $
038 */
039 public class EventDrivenPollingConsumer extends PollingConsumerSupport implements Processor {
040 private static final transient Log LOG = LogFactory.getLog(EventDrivenPollingConsumer.class);
041 private BlockingQueue<Exchange> queue;
042 private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(LOG));
043 private Consumer consumer;
044
045 public EventDrivenPollingConsumer(Endpoint endpoint) {
046 this(endpoint, new ArrayBlockingQueue<Exchange>(1000));
047 }
048
049 public EventDrivenPollingConsumer(Endpoint endpoint, BlockingQueue<Exchange> queue) {
050 super(endpoint);
051 this.queue = queue;
052 }
053
054 public Exchange receiveNoWait() {
055 return receive(0);
056 }
057
058 public Exchange receive() {
059 while (isRunAllowed()) {
060 try {
061 return queue.take();
062 } catch (InterruptedException e) {
063 handleInteruptedException(e);
064 }
065 }
066 if (LOG.isTraceEnabled()) {
067 LOG.trace("Consumer is not running, so returning null");
068 }
069 return null;
070 }
071
072 public Exchange receive(long timeout) {
073 try {
074 return queue.poll(timeout, TimeUnit.MILLISECONDS);
075 } catch (InterruptedException e) {
076 handleInteruptedException(e);
077 return null;
078 }
079 }
080
081 public void process(Exchange exchange) throws Exception {
082 queue.offer(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 } finally {
108 consumer = null;
109 }
110 }
111 }
112 }