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: 656947 $
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 if (LOG.isDebugEnabled()) {
062 LOG.debug("Starting to poll: " + this.getEndpoint());
063 }
064 try {
065 poll();
066 } catch (Exception e) {
067 // TODO: We should not swallow this but handle it better. See CAMEL-501
068 LOG.warn("An exception occured while polling: " + this.getEndpoint() + ": " + e.getMessage(), e);
069 }
070 }
071
072 // Properties
073 // -------------------------------------------------------------------------
074 public long getInitialDelay() {
075 return initialDelay;
076 }
077
078 public void setInitialDelay(long initialDelay) {
079 this.initialDelay = initialDelay;
080 }
081
082 public long getDelay() {
083 return delay;
084 }
085
086 public void setDelay(long delay) {
087 this.delay = delay;
088 }
089
090 public TimeUnit getTimeUnit() {
091 return timeUnit;
092 }
093
094 public void setTimeUnit(TimeUnit timeUnit) {
095 this.timeUnit = timeUnit;
096 }
097
098 public boolean isUseFixedDelay() {
099 return useFixedDelay;
100 }
101
102 public void setUseFixedDelay(boolean useFixedDelay) {
103 this.useFixedDelay = useFixedDelay;
104 }
105
106 // Implementation methods
107 // -------------------------------------------------------------------------
108
109 /**
110 * The polling method which is invoked periodically to poll this consumer
111 *
112 * @throws Exception can be thrown if an exception occured during polling
113 */
114 protected abstract void poll() throws Exception;
115
116 @Override
117 protected void doStart() throws Exception {
118 super.doStart();
119 if (isUseFixedDelay()) {
120 future = executor.scheduleWithFixedDelay(this, getInitialDelay(), getDelay(), getTimeUnit());
121 } else {
122 future = executor.scheduleAtFixedRate(this, getInitialDelay(), getDelay(), getTimeUnit());
123 }
124 }
125
126 @Override
127 protected void doStop() throws Exception {
128 if (future != null) {
129 future.cancel(false);
130 }
131 super.doStop();
132 }
133 }