Coverage Report - org.apache.camel.impl.ScheduledPollConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
ScheduledPollConsumer
67% 
100% 
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.ScheduledExecutorService;
 20  
 import java.util.concurrent.ScheduledFuture;
 21  
 import java.util.concurrent.TimeUnit;
 22  
 
 23  
 import org.apache.camel.Endpoint;
 24  
 import org.apache.camel.Exchange;
 25  
 import org.apache.camel.Processor;
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * A useful base class for any consumer which is polling based
 31  
  * 
 32  
  * @version $Revision: 563607 $
 33  
  */
 34  
 public abstract class ScheduledPollConsumer<E extends Exchange> extends DefaultConsumer<E> implements
 35  
     Runnable {
 36  3
     private static final transient Log LOG = LogFactory.getLog(ScheduledPollConsumer.class);
 37  
 
 38  
     private final ScheduledExecutorService executor;
 39  18
     private long initialDelay = 1000;
 40  18
     private long delay = 500;
 41  18
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
 42  
     private boolean useFixedDelay;
 43  
     private ScheduledFuture<?> future;
 44  
 
 45  
     public ScheduledPollConsumer(DefaultEndpoint<E> endpoint, Processor processor) {
 46  18
         this(endpoint, processor, endpoint.getExecutorService());
 47  18
     }
 48  
 
 49  
     public ScheduledPollConsumer(Endpoint<E> endpoint, Processor processor, ScheduledExecutorService executor) {
 50  18
         super(endpoint, processor);
 51  18
         this.executor = executor;
 52  18
         if (executor == null) {
 53  0
             throw new IllegalArgumentException("A non null ScheduledExecutorService must be provided.");
 54  
         }
 55  18
     }
 56  
 
 57  
     /**
 58  
      * Invoked whenever we should be polled
 59  
      */
 60  
     public void run() {
 61  20
         LOG.debug("Starting to poll");
 62  
         try {
 63  20
             poll();
 64  0
         } catch (Exception e) {
 65  0
             LOG.warn("Caught: " + e, e);
 66  20
         }
 67  20
     }
 68  
 
 69  
     // Properties
 70  
     // -------------------------------------------------------------------------
 71  
     public long getInitialDelay() {
 72  18
         return initialDelay;
 73  
     }
 74  
 
 75  
     public void setInitialDelay(long initialDelay) {
 76  0
         this.initialDelay = initialDelay;
 77  0
     }
 78  
 
 79  
     public long getDelay() {
 80  18
         return delay;
 81  
     }
 82  
 
 83  
     public void setDelay(long delay) {
 84  0
         this.delay = delay;
 85  0
     }
 86  
 
 87  
     public TimeUnit getTimeUnit() {
 88  18
         return timeUnit;
 89  
     }
 90  
 
 91  
     public void setTimeUnit(TimeUnit timeUnit) {
 92  0
         this.timeUnit = timeUnit;
 93  0
     }
 94  
 
 95  
     public boolean isUseFixedDelay() {
 96  18
         return useFixedDelay;
 97  
     }
 98  
 
 99  
     public void setUseFixedDelay(boolean useFixedDelay) {
 100  0
         this.useFixedDelay = useFixedDelay;
 101  0
     }
 102  
 
 103  
     // Implementation methods
 104  
     // -------------------------------------------------------------------------
 105  
 
 106  
     /**
 107  
      * The polling method which is invoked periodically to poll this consumer
 108  
      * 
 109  
      * @throws Exception
 110  
      */
 111  
     protected abstract void poll() throws Exception;
 112  
 
 113  
     @Override
 114  
     protected void doStart() throws Exception {
 115  18
         super.doStart();
 116  18
         if (isUseFixedDelay()) {
 117  0
             future = executor.scheduleWithFixedDelay(this, getInitialDelay(), getDelay(), getTimeUnit());
 118  0
         } else {
 119  18
             future = executor.scheduleAtFixedRate(this, getInitialDelay(), getDelay(), getTimeUnit());
 120  
         }
 121  18
     }
 122  
 
 123  
     @Override
 124  
     protected void doStop() throws Exception {
 125  18
         if (future != null) {
 126  18
             future.cancel(false);
 127  
         }
 128  18
         super.doStop();
 129  18
     }
 130  
 }