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