org.apache.camel.impl
Class ScheduledPollConsumer

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.impl.DefaultConsumer
          extended by org.apache.camel.impl.ScheduledPollConsumer
All Implemented Interfaces:
Runnable, Consumer, PollingConsumerPollingStrategy, Service, ShutdownableService, StatefulService, SuspendableService
Direct Known Subclasses:
DefaultScheduledPollConsumer, ScheduledBatchPollingConsumer

public abstract class ScheduledPollConsumer
extends DefaultConsumer
implements Runnable, SuspendableService, PollingConsumerPollingStrategy

A useful base class for any consumer which is polling based

Version:

Field Summary
 
Fields inherited from class org.apache.camel.impl.DefaultConsumer
log
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
ScheduledPollConsumer(Endpoint endpoint, Processor processor)
           
ScheduledPollConsumer(Endpoint endpoint, Processor processor, ScheduledExecutorService scheduledExecutorService)
           
 
Method Summary
 void afterPoll()
          Callback invoked after the poll.
 long beforePoll(long timeout)
          Callback invoked before the poll.
protected  void doShutdown()
          Implementations override this method to perform customized shutdown.
protected  void doStart()
          Implementations override this method to support customized start/stop.
protected  void doStop()
          Implementations override this method to support customized start/stop.
protected  void doSuspend()
          Implementations override this method to support customized suspend/resume.
 long getDelay()
           
 long getInitialDelay()
           
 PollingConsumerPollStrategy getPollStrategy()
           
 LoggingLevel getRunLoggingLevel()
           
 ScheduledExecutorService getScheduledExecutorService()
           
 TimeUnit getTimeUnit()
           
 boolean isGreedy()
           
protected  boolean isPollAllowed()
           
protected  boolean isPolling()
          Whether polling is currently in progress
 boolean isSchedulerStarted()
          Whether the scheduler has been started.
 boolean isSendEmptyMessageWhenIdle()
           
 boolean isStartScheduler()
           
 boolean isUseFixedDelay()
           
 void onInit()
          Callback invoked when the consumer is initialized such as when the PollingConsumer starts.
protected abstract  int poll()
          The polling method which is invoked periodically to poll this consumer
protected  void processEmptyMessage()
          No messages to poll so send an empty message instead.
 void run()
          Invoked whenever we should be polled
 void setDelay(long delay)
           
 void setGreedy(boolean greedy)
          If greedy then a poll is executed immediate after a previous poll that polled 1 or more messages.
 void setInitialDelay(long initialDelay)
           
 void setPollStrategy(PollingConsumerPollStrategy pollStrategy)
           
 void setRunLoggingLevel(LoggingLevel runLoggingLevel)
           
 void setScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
          Sets a custom shared ScheduledExecutorService to use as thread pool

Notice: When using a custom thread pool, then the lifecycle of this thread pool is not controlled by this consumer (eg this consumer will not start/stop the thread pool when the consumer is started/stopped etc.)

 void setSendEmptyMessageWhenIdle(boolean sendEmptyMessageWhenIdle)
           
 void setStartScheduler(boolean startScheduler)
          Sets whether the scheduler should be started when this consumer starts.
 void setTimeUnit(TimeUnit timeUnit)
          Sets the time unit to use.
 void setUseFixedDelay(boolean useFixedDelay)
           
 void startScheduler()
          Starts the scheduler.
 
Methods inherited from class org.apache.camel.impl.DefaultConsumer
getAsyncProcessor, getEndpoint, getExceptionHandler, getProcessor, handleException, handleException, setExceptionHandler, toString
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.camel.SuspendableService
isSuspended, resume, suspend
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Constructor Detail

ScheduledPollConsumer

public ScheduledPollConsumer(Endpoint endpoint,
                             Processor processor)

ScheduledPollConsumer

public ScheduledPollConsumer(Endpoint endpoint,
                             Processor processor,
                             ScheduledExecutorService scheduledExecutorService)
Method Detail

run

public void run()
Invoked whenever we should be polled

Specified by:
run in interface Runnable

processEmptyMessage

protected void processEmptyMessage()
                            throws Exception
No messages to poll so send an empty message instead.

Throws:
Exception - is thrown if error processing the empty message.

isPollAllowed

protected boolean isPollAllowed()

isPolling

protected boolean isPolling()
Whether polling is currently in progress


getInitialDelay

public long getInitialDelay()

setInitialDelay

public void setInitialDelay(long initialDelay)

getDelay

public long getDelay()

setDelay

public void setDelay(long delay)

getTimeUnit

public TimeUnit getTimeUnit()

setTimeUnit

public void setTimeUnit(TimeUnit timeUnit)
Sets the time unit to use.

Notice that both getDelay() and getInitialDelay() are using the same time unit. So if you change this value, then take into account that the default value of getInitialDelay() is 1000. So you may to adjust this value accordingly.

Parameters:
timeUnit - the time unit.

isUseFixedDelay

public boolean isUseFixedDelay()

setUseFixedDelay

public void setUseFixedDelay(boolean useFixedDelay)

getRunLoggingLevel

public LoggingLevel getRunLoggingLevel()

setRunLoggingLevel

public void setRunLoggingLevel(LoggingLevel runLoggingLevel)

getPollStrategy

public PollingConsumerPollStrategy getPollStrategy()

setPollStrategy

public void setPollStrategy(PollingConsumerPollStrategy pollStrategy)

isStartScheduler

public boolean isStartScheduler()

setStartScheduler

public void setStartScheduler(boolean startScheduler)
Sets whether the scheduler should be started when this consumer starts.

This option is default true.

Parameters:
startScheduler - whether to start scheduler

setSendEmptyMessageWhenIdle

public void setSendEmptyMessageWhenIdle(boolean sendEmptyMessageWhenIdle)

isSendEmptyMessageWhenIdle

public boolean isSendEmptyMessageWhenIdle()

isGreedy

public boolean isGreedy()

setGreedy

public void setGreedy(boolean greedy)
If greedy then a poll is executed immediate after a previous poll that polled 1 or more messages.


getScheduledExecutorService

public ScheduledExecutorService getScheduledExecutorService()

isSchedulerStarted

public boolean isSchedulerStarted()
Whether the scheduler has been started.

The scheduler can be started with the startScheduler() method.

Returns:
true if started, false if not.

setScheduledExecutorService

public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
Sets a custom shared ScheduledExecutorService to use as thread pool

Notice: When using a custom thread pool, then the lifecycle of this thread pool is not controlled by this consumer (eg this consumer will not start/stop the thread pool when the consumer is started/stopped etc.)

Parameters:
scheduledExecutorService - the custom thread pool to use

poll

protected abstract int poll()
                     throws Exception
The polling method which is invoked periodically to poll this consumer

Returns:
number of messages polled, will be 0 if no message was polled at all.
Throws:
Exception - can be thrown if an exception occurred during polling

doStart

protected void doStart()
                throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: See ServiceSupport.doStop() for more details.

Overrides:
doStart in class DefaultConsumer
Throws:
Exception
See Also:
ServiceSupport.doStop()

startScheduler

public void startScheduler()
Starts the scheduler.

If the scheduler is already started, then this is a noop method call.


doStop

protected void doStop()
               throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: Camel will invoke this ServiceSupport.doStop() method when the service is being stopped. This method will also be invoked if the service is still in uninitialized state (eg has not been started). The method is always called to allow the service to do custom logic when the service is being stopped, such as when CamelContext is shutting down.

Overrides:
doStop in class DefaultConsumer
Throws:
Exception
See Also:
ServiceSupport.doStart()

doShutdown

protected void doShutdown()
                   throws Exception
Description copied from class: ServiceSupport
Implementations override this method to perform customized shutdown.

Overrides:
doShutdown in class ServiceSupport
Throws:
Exception

doSuspend

protected void doSuspend()
                  throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized suspend/resume.

Overrides:
doSuspend in class ServiceSupport
Throws:
Exception

onInit

public void onInit()
            throws Exception
Description copied from interface: PollingConsumerPollingStrategy
Callback invoked when the consumer is initialized such as when the PollingConsumer starts.

Specified by:
onInit in interface PollingConsumerPollingStrategy
Throws:
Exception - can be thrown if error initializing.

beforePoll

public long beforePoll(long timeout)
                throws Exception
Description copied from interface: PollingConsumerPollingStrategy
Callback invoked before the poll.

Specified by:
beforePoll in interface PollingConsumerPollingStrategy
Parameters:
timeout - the timeout
Returns:
timeout to be used, this allows returning a higher timeout value to ensure at least one poll is being performed
Throws:
Exception - can be thrown if error occurred

afterPoll

public void afterPoll()
               throws Exception
Description copied from interface: PollingConsumerPollingStrategy
Callback invoked after the poll.

Specified by:
afterPoll in interface PollingConsumerPollingStrategy
Throws:
Exception - can be thrown if error occurred


Apache CAMEL