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.PollingConsumerAware;
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 org.apache.camel.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: 761579 $
039     */
040    public class EventDrivenPollingConsumer extends PollingConsumerSupport implements Processor {
041        private static final transient Log LOG = LogFactory.getLog(EventDrivenPollingConsumer.class);
042        private BlockingQueue<Exchange> queue;
043        private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(LOG));
044        private Consumer consumer;
045    
046        public EventDrivenPollingConsumer(Endpoint endpoint) {
047            this(endpoint, new ArrayBlockingQueue<Exchange>(1000));
048        }
049    
050        public EventDrivenPollingConsumer(Endpoint endpoint, BlockingQueue<Exchange> queue) {
051            super(endpoint);
052            this.queue = queue;
053        }
054    
055        public Exchange receiveNoWait() {
056            return receive(0);
057        }
058    
059        public Exchange receive() {
060            while (isRunAllowed()) {
061                try {
062                    return queue.take();
063                } catch (InterruptedException e) {
064                    handleInteruptedException(e);
065                }
066            }
067            if (LOG.isTraceEnabled()) {
068                LOG.trace("Consumer is not running, so returning null");
069            }
070            return null;
071        }
072    
073        public Exchange receive(long timeout) {
074            try {
075                return queue.poll(timeout, TimeUnit.MILLISECONDS);
076            } catch (InterruptedException e) {
077                handleInteruptedException(e);
078                return null;
079            }
080        }
081    
082        public void process(Exchange exchange) throws Exception {
083            if (exchange instanceof PollingConsumerAware) {
084                // callback that this exchange has been polled
085                ((PollingConsumerAware)exchange).exchangePolled(exchange);
086            }
087            queue.offer(exchange);
088        }
089    
090        public ExceptionHandler getInteruptedExceptionHandler() {
091            return interuptedExceptionHandler;
092        }
093    
094        public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) {
095            this.interuptedExceptionHandler = interuptedExceptionHandler;
096        }
097    
098        protected void handleInteruptedException(InterruptedException e) {
099            getInteruptedExceptionHandler().handleException(e);
100        }
101    
102        protected void doStart() throws Exception {
103            // lets add ourselves as a consumer
104            consumer = getEndpoint().createConsumer(this);
105            consumer.start();
106        }
107    
108        protected void doStop() throws Exception {
109            if (consumer != null) {
110                try {
111                    consumer.stop();
112                } finally {
113                    consumer = null;
114                }
115            }
116        }
117    }