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