Coverage Report - org.apache.camel.impl.DefaultPollingConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultPollingConsumer
55% 
50% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.impl;
 18  
 
 19  
 import java.util.concurrent.ArrayBlockingQueue;
 20  
 import java.util.concurrent.BlockingQueue;
 21  
 import java.util.concurrent.TimeUnit;
 22  
 
 23  
 import org.apache.camel.Consumer;
 24  
 import org.apache.camel.Endpoint;
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Processor;
 27  
 import org.apache.camel.processor.Logger;
 28  
 import org.apache.camel.spi.ExceptionHandler;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 /**
 33  
  * A default implementation of the {@link PollingConsumer} which uses the normal
 34  
  * asynchronous consumer mechanism along with a {@link BlockingQueue} to allow
 35  
  * the caller to pull messages on demand.
 36  
  * 
 37  
  * @version $Revision: 1.1 $
 38  
  */
 39  
 public class DefaultPollingConsumer<E extends Exchange> extends PollingConsumerSupport<E> implements
 40  
     Processor {
 41  3
     private static final transient Log LOG = LogFactory.getLog(DefaultPollingConsumer.class);
 42  
     private BlockingQueue<E> queue;
 43  6
     private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(LOG));
 44  
     private Consumer<E> consumer;
 45  
 
 46  
     public DefaultPollingConsumer(Endpoint<E> endpoint) {
 47  6
         this(endpoint, new ArrayBlockingQueue<E>(1000));
 48  6
     }
 49  
 
 50  
     public DefaultPollingConsumer(Endpoint<E> endpoint, BlockingQueue<E> queue) {
 51  6
         super(endpoint);
 52  6
         this.queue = queue;
 53  6
     }
 54  
 
 55  
     public E receiveNoWait() {
 56  0
         return receive(0);
 57  
     }
 58  
 
 59  
     public E receive() {
 60  0
         while (!isStopping() && !isStopped()) {
 61  
             try {
 62  0
                 return queue.take();
 63  0
             } catch (InterruptedException e) {
 64  0
                 handleInteruptedException(e);
 65  
             }
 66  0
         }
 67  0
         return null;
 68  
     }
 69  
 
 70  
     public E receive(long timeout) {
 71  
         try {
 72  318
             return queue.poll(timeout, TimeUnit.MILLISECONDS);
 73  0
         } catch (InterruptedException e) {
 74  0
             handleInteruptedException(e);
 75  0
             return null;
 76  
         }
 77  
     }
 78  
 
 79  
     public void process(Exchange exchange) throws Exception {
 80  312
         queue.offer((E)exchange);
 81  312
     }
 82  
 
 83  
     public ExceptionHandler getInteruptedExceptionHandler() {
 84  0
         return interuptedExceptionHandler;
 85  
     }
 86  
 
 87  
     public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) {
 88  0
         this.interuptedExceptionHandler = interuptedExceptionHandler;
 89  0
     }
 90  
 
 91  
     protected void handleInteruptedException(InterruptedException e) {
 92  0
         getInteruptedExceptionHandler().handleException(e);
 93  0
     }
 94  
 
 95  
     protected void doStart() throws Exception {
 96  
         // lets add ourselves as a consumer
 97  6
         consumer = getEndpoint().createConsumer(this);
 98  6
         consumer.start();
 99  6
     }
 100  
 
 101  
     protected void doStop() throws Exception {
 102  6
         if (consumer != null) {
 103  
             try {
 104  6
                 consumer.stop();
 105  
             } finally {
 106  6
                 consumer = null;
 107  6
             }
 108  
         }
 109  6
     }
 110  
 }