org.apache.camel.impl
Class ScheduledBatchPollingConsumer

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

public abstract class ScheduledBatchPollingConsumer
extends ScheduledPollConsumer
implements BatchConsumer, ShutdownAware

A useful base class for any consumer which is polling batch based


Field Summary
protected  int maxMessagesPerPoll
           
protected  int pendingExchanges
           
protected  ShutdownRunningTask shutdownRunningTask
           
 
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
ScheduledBatchPollingConsumer(Endpoint endpoint, Processor processor)
           
ScheduledBatchPollingConsumer(Endpoint endpoint, Processor processor, ScheduledExecutorService executor)
           
 
Method Summary
 boolean deferShutdown(ShutdownRunningTask shutdownRunningTask)
          To defer shutdown during first phase of shutdown.
 int getMaxMessagesPerPoll()
          Gets the maximum number of messages as a limit to poll at each polling.
 int getPendingExchangesSize()
          Gets the number of pending exchanges.
 boolean isBatchAllowed()
          Whether processing the batch is still allowed.
 void prepareShutdown(boolean forced)
          Prepares for shutdown.
protected  void processEmptyMessage()
          No messages to poll so send an empty message instead.
 void setMaxMessagesPerPoll(int maxMessagesPerPoll)
          Sets a maximum number of messages as a limit to poll at each polling.
 
Methods inherited from class org.apache.camel.impl.ScheduledPollConsumer
afterPoll, beforePoll, doShutdown, doStart, doStop, doSuspend, getDelay, getInitialDelay, getPollStrategy, getRunLoggingLevel, getScheduledExecutorService, getTimeUnit, isGreedy, isPollAllowed, isPolling, isSchedulerStarted, isSendEmptyMessageWhenIdle, isStartScheduler, isUseFixedDelay, onInit, poll, run, setDelay, setGreedy, setInitialDelay, setPollStrategy, setRunLoggingLevel, setScheduledExecutorService, setSendEmptyMessageWhenIdle, setStartScheduler, setTimeUnit, setUseFixedDelay, startScheduler
 
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.BatchConsumer
processBatch
 
Methods inherited from interface org.apache.camel.Consumer
getEndpoint
 
Methods inherited from interface org.apache.camel.Service
start, stop
 
Methods inherited from interface org.apache.camel.SuspendableService
isSuspended, resume, suspend
 

Field Detail

shutdownRunningTask

protected volatile ShutdownRunningTask shutdownRunningTask

pendingExchanges

protected volatile int pendingExchanges

maxMessagesPerPoll

protected int maxMessagesPerPoll
Constructor Detail

ScheduledBatchPollingConsumer

public ScheduledBatchPollingConsumer(Endpoint endpoint,
                                     Processor processor)

ScheduledBatchPollingConsumer

public ScheduledBatchPollingConsumer(Endpoint endpoint,
                                     Processor processor,
                                     ScheduledExecutorService executor)
Method Detail

deferShutdown

public boolean deferShutdown(ShutdownRunningTask shutdownRunningTask)
Description copied from interface: ShutdownAware
To defer shutdown during first phase of shutdown. This allows any pending exchanges to be completed and therefore ensure a graceful shutdown without loosing messages. At the very end when there are no more inflight and pending messages the consumer could then safely be shutdown.

This is needed by SedaConsumer.

Specified by:
deferShutdown in interface ShutdownAware
Parameters:
shutdownRunningTask - the configured option for how to act when shutting down running tasks.
Returns:
true to defer shutdown to very last.

getPendingExchangesSize

public int getPendingExchangesSize()
Description copied from interface: ShutdownAware
Gets the number of pending exchanges.

Some consumers has internal queues with Exchange which are pending. For example the SedaConsumer.

Return zero to indicate no pending exchanges and therefore ready to shutdown.

Specified by:
getPendingExchangesSize in interface ShutdownAware
Returns:
number of pending exchanges

prepareShutdown

public void prepareShutdown(boolean forced)
Description copied from interface: ShutdownPrepared
Prepares for shutdown.

The ShutdownStrategy supports preparing for shutdown using two steps. First a regular preparation, where the given forced parameter will be false. And if the shutdown times out, then the ShutdownStrategy performs a more aggressive shutdown, calling this method a second time with true for the given forced parameter.

For example by graceful stopping any threads or the likes.

For forced shutdown, then the service is expected to aggressively shutdown any child services, such as thread pools etc. This is the last chance it has to perform such duties.

Specified by:
prepareShutdown in interface ShutdownPrepared
Parameters:
forced - true is forcing a more aggressive shutdown, false is for preparing to shutdown.

setMaxMessagesPerPoll

public void setMaxMessagesPerPoll(int maxMessagesPerPoll)
Description copied from interface: BatchConsumer
Sets a maximum number of messages as a limit to poll at each polling.

Can be used to limit e.g. to 100 to avoid reading thousands or more messages within the first polling at startup.

Is default unlimited, but use 0 or negative number to disable it as unlimited.

Specified by:
setMaxMessagesPerPoll in interface BatchConsumer
Parameters:
maxMessagesPerPoll - maximum messages to poll.

getMaxMessagesPerPoll

public int getMaxMessagesPerPoll()
Gets the maximum number of messages as a limit to poll at each polling.

Is default unlimited, but use 0 or negative number to disable it as unlimited.

Returns:
max messages to poll

isBatchAllowed

public boolean isBatchAllowed()
Description copied from interface: BatchConsumer
Whether processing the batch is still allowed.

This is used during shutdown to indicate whether to complete the pending exchanges or stop after the current exchange has been processed.

Specified by:
isBatchAllowed in interface BatchConsumer
Returns:
true to continue processing from the batch, or false to stop.
See Also:
ShutdownRunningTask

processEmptyMessage

protected void processEmptyMessage()
                            throws Exception
Description copied from class: ScheduledPollConsumer
No messages to poll so send an empty message instead.

Overrides:
processEmptyMessage in class ScheduledPollConsumer
Throws:
Exception - is thrown if error processing the empty message.


Apache CAMEL