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.ScheduledExecutorService; 020 import java.util.concurrent.ScheduledFuture; 021 import java.util.concurrent.TimeUnit; 022 023 import org.apache.camel.Endpoint; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.Processor; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * A useful base class for any consumer which is polling based 031 * 032 * @version $Revision: 35332 $ 033 */ 034 public abstract class ScheduledPollConsumer<E extends Exchange> extends DefaultConsumer<E> implements 035 Runnable { 036 private static final transient Log LOG = LogFactory.getLog(ScheduledPollConsumer.class); 037 038 private final ScheduledExecutorService executor; 039 private long initialDelay = 1000; 040 private long delay = 500; 041 private TimeUnit timeUnit = TimeUnit.MILLISECONDS; 042 private boolean useFixedDelay; 043 private ScheduledFuture<?> future; 044 045 public ScheduledPollConsumer(DefaultEndpoint<E> endpoint, Processor processor) { 046 this(endpoint, processor, endpoint.getExecutorService()); 047 } 048 049 public ScheduledPollConsumer(Endpoint<E> endpoint, Processor processor, ScheduledExecutorService executor) { 050 super(endpoint, processor); 051 this.executor = executor; 052 if (executor == null) { 053 throw new IllegalArgumentException("A non null ScheduledExecutorService must be provided."); 054 } 055 } 056 057 /** 058 * Invoked whenever we should be polled 059 */ 060 public void run() { 061 LOG.debug("Starting to poll"); 062 try { 063 poll(); 064 } catch (Exception e) { 065 LOG.warn("Caught: " + e, e); 066 } 067 } 068 069 // Properties 070 // ------------------------------------------------------------------------- 071 public long getInitialDelay() { 072 return initialDelay; 073 } 074 075 public void setInitialDelay(long initialDelay) { 076 this.initialDelay = initialDelay; 077 } 078 079 public long getDelay() { 080 return delay; 081 } 082 083 public void setDelay(long delay) { 084 this.delay = delay; 085 } 086 087 public TimeUnit getTimeUnit() { 088 return timeUnit; 089 } 090 091 public void setTimeUnit(TimeUnit timeUnit) { 092 this.timeUnit = timeUnit; 093 } 094 095 public boolean isUseFixedDelay() { 096 return useFixedDelay; 097 } 098 099 public void setUseFixedDelay(boolean useFixedDelay) { 100 this.useFixedDelay = useFixedDelay; 101 } 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 super.doStart(); 116 if (isUseFixedDelay()) { 117 future = executor.scheduleWithFixedDelay(this, getInitialDelay(), getDelay(), getTimeUnit()); 118 } else { 119 future = executor.scheduleAtFixedRate(this, getInitialDelay(), getDelay(), getTimeUnit()); 120 } 121 } 122 123 @Override 124 protected void doStop() throws Exception { 125 if (future != null) { 126 future.cancel(false); 127 } 128 super.doStop(); 129 } 130 }