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 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: 1.1 $
038     */
039    public class DefaultPollingConsumer<E extends Exchange> extends PollingConsumerSupport<E> implements
040        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                } catch (InterruptedException e) {
064                    handleInteruptedException(e);
065                }
066            }
067            return null;
068        }
069    
070        public E receive(long timeout) {
071            try {
072                return queue.poll(timeout, TimeUnit.MILLISECONDS);
073            } catch (InterruptedException e) {
074                handleInteruptedException(e);
075                return null;
076            }
077        }
078    
079        public void process(Exchange exchange) throws Exception {
080            queue.offer((E)exchange);
081        }
082    
083        public ExceptionHandler getInteruptedExceptionHandler() {
084            return interuptedExceptionHandler;
085        }
086    
087        public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) {
088            this.interuptedExceptionHandler = interuptedExceptionHandler;
089        }
090    
091        protected void handleInteruptedException(InterruptedException e) {
092            getInteruptedExceptionHandler().handleException(e);
093        }
094    
095        protected void doStart() throws Exception {
096            // lets add ourselves as a consumer
097            consumer = getEndpoint().createConsumer(this);
098            consumer.start();
099        }
100    
101        protected void doStop() throws Exception {
102            if (consumer != null) {
103                try {
104                    consumer.stop();
105                } finally {
106                    consumer = null;
107                }
108            }
109        }
110    }