Package org.apache.camel.support
Class ScheduledPollConsumer
- java.lang.Object
-
- org.apache.camel.support.service.BaseService
-
- org.apache.camel.support.service.ServiceSupport
-
- org.apache.camel.support.DefaultConsumer
-
- org.apache.camel.support.ScheduledPollConsumer
-
- All Implemented Interfaces:
AutoCloseable,Runnable,org.apache.camel.Consumer,org.apache.camel.EndpointAware,org.apache.camel.health.HealthCheckAware,org.apache.camel.PollingConsumerPollingStrategy,org.apache.camel.RouteAware,org.apache.camel.Service,org.apache.camel.ShutdownableService,org.apache.camel.spi.RouteIdAware,org.apache.camel.StatefulService,org.apache.camel.Suspendable,org.apache.camel.SuspendableService
- Direct Known Subclasses:
AbstractApiConsumer,DefaultScheduledPollConsumer,ScheduledBatchPollingConsumer
public abstract class ScheduledPollConsumer extends DefaultConsumer implements Runnable, org.apache.camel.Suspendable, org.apache.camel.PollingConsumerPollingStrategy, org.apache.camel.health.HealthCheckAware
A useful base class for any consumer which is polling based
-
-
Constructor Summary
Constructors Constructor Description ScheduledPollConsumer(org.apache.camel.Endpoint endpoint, org.apache.camel.Processor processor)ScheduledPollConsumer(org.apache.camel.Endpoint endpoint, org.apache.camel.Processor processor, ScheduledExecutorService scheduledExecutorService)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddLastErrorDetail(String key, Object value)Adds a detail to the last caused error (exception) for the last poll that failed.protected voidafterConfigureScheduler(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler, boolean newScheduler)After the scheduler has been configuredvoidafterPoll()longbeforePoll(long timeout)protected voiddoBuild()protected voiddoInit()protected voiddoShutdown()protected voiddoStart()protected voiddoStop()protected voiddoSuspend()intgetBackoffCounter()intgetBackoffErrorThreshold()intgetBackoffIdleThreshold()intgetBackoffMultiplier()protected longgetCounter()Gets the total number of polls run.longgetDelay()protected longgetErrorCounter()Gets the error counter.longgetInitialDelay()protected ThrowablegetLastError()Gets the last caused error (exception) for the last poll that failed.protected Map<String,Object>getLastErrorDetails()Gets the last caused error (exception) details for the last poll that failed.org.apache.camel.spi.PollingConsumerPollStrategygetPollStrategy()longgetRepeatCount()org.apache.camel.LoggingLevelgetRunLoggingLevel()ScheduledExecutorServicegetScheduledExecutorService()org.apache.camel.spi.ScheduledPollConsumerSchedulergetScheduler()Map<String,Object>getSchedulerProperties()protected longgetSuccessCounter()Gets the success counter.TimeUnitgetTimeUnit()protected org.apache.camel.health.HealthCheck.StateinitialHealthCheckState()Used to allow special consumers to override the initial state of the health check (readiness check) during startup.protected booleanisFirstPollDone()Whether a first pool attempt has been done (also if the consumer has been restarted)booleanisGreedy()protected booleanisPollAllowed()booleanisPolling()Whether polling is currently in progressbooleanisSchedulerStarted()booleanisSendEmptyMessageWhenIdle()booleanisStartScheduler()booleanisUseFixedDelay()voidonInit()protected abstract intpoll()The polling method which is invoked periodically to poll this consumerprotected voidprocessEmptyMessage()No messages to poll so send an empty message instead.voidrun()Invoked whenever we should be polledvoidsetBackoffErrorThreshold(int backoffErrorThreshold)voidsetBackoffIdleThreshold(int backoffIdleThreshold)voidsetBackoffMultiplier(int backoffMultiplier)voidsetDelay(long delay)voidsetGreedy(boolean greedy)voidsetInitialDelay(long initialDelay)voidsetPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy pollStrategy)voidsetRepeatCount(long repeatCount)voidsetRunLoggingLevel(org.apache.camel.LoggingLevel runLoggingLevel)voidsetScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)voidsetScheduler(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler)voidsetSchedulerProperties(Map<String,Object> schedulerProperties)voidsetSendEmptyMessageWhenIdle(boolean sendEmptyMessageWhenIdle)voidsetStartScheduler(boolean startScheduler)voidsetTimeUnit(TimeUnit timeUnit)voidsetUseFixedDelay(boolean useFixedDelay)voidstartScheduler()Starts the scheduler.-
Methods inherited from class org.apache.camel.support.DefaultConsumer
createExchange, createUoW, defaultConsumerCallback, doneUoW, getAsyncProcessor, getEndpoint, getExceptionHandler, getHealthCheck, getProcessor, getRoute, getRouteId, handleException, handleException, releaseExchange, setExceptionHandler, setHealthCheck, setRoute, setRouteId, toString
-
Methods inherited from class org.apache.camel.support.service.BaseService
build, doFail, doLifecycleChange, doResume, fail, getStatus, init, isBuild, isInit, isNew, isRunAllowed, isShutdown, isStarted, isStarting, isStartingOrStarted, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, 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.health.HealthCheckAware
getHealthCheck, setHealthCheck
-
-
-
-
Constructor Detail
-
ScheduledPollConsumer
public ScheduledPollConsumer(org.apache.camel.Endpoint endpoint, org.apache.camel.Processor processor)
-
ScheduledPollConsumer
public ScheduledPollConsumer(org.apache.camel.Endpoint endpoint, org.apache.camel.Processor processor, ScheduledExecutorService scheduledExecutorService)
-
-
Method Detail
-
processEmptyMessage
protected void processEmptyMessage() throws ExceptionNo messages to poll so send an empty message instead.- Throws:
Exception- is thrown if error processing the empty message.
-
isPollAllowed
protected boolean isPollAllowed()
-
isPolling
public boolean isPolling()
Whether polling is currently in progress
-
getScheduler
public org.apache.camel.spi.ScheduledPollConsumerScheduler getScheduler()
-
setScheduler
public void setScheduler(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler)
-
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)
-
isUseFixedDelay
public boolean isUseFixedDelay()
-
setUseFixedDelay
public void setUseFixedDelay(boolean useFixedDelay)
-
getRunLoggingLevel
public org.apache.camel.LoggingLevel getRunLoggingLevel()
-
setRunLoggingLevel
public void setRunLoggingLevel(org.apache.camel.LoggingLevel runLoggingLevel)
-
getPollStrategy
public org.apache.camel.spi.PollingConsumerPollStrategy getPollStrategy()
-
setPollStrategy
public void setPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy pollStrategy)
-
isStartScheduler
public boolean isStartScheduler()
-
setStartScheduler
public void setStartScheduler(boolean startScheduler)
-
setSendEmptyMessageWhenIdle
public void setSendEmptyMessageWhenIdle(boolean sendEmptyMessageWhenIdle)
-
isSendEmptyMessageWhenIdle
public boolean isSendEmptyMessageWhenIdle()
-
isGreedy
public boolean isGreedy()
-
setGreedy
public void setGreedy(boolean greedy)
-
getBackoffCounter
public int getBackoffCounter()
-
getBackoffMultiplier
public int getBackoffMultiplier()
-
setBackoffMultiplier
public void setBackoffMultiplier(int backoffMultiplier)
-
getBackoffIdleThreshold
public int getBackoffIdleThreshold()
-
setBackoffIdleThreshold
public void setBackoffIdleThreshold(int backoffIdleThreshold)
-
getBackoffErrorThreshold
public int getBackoffErrorThreshold()
-
setBackoffErrorThreshold
public void setBackoffErrorThreshold(int backoffErrorThreshold)
-
getRepeatCount
public long getRepeatCount()
-
setRepeatCount
public void setRepeatCount(long repeatCount)
-
getScheduledExecutorService
public ScheduledExecutorService getScheduledExecutorService()
-
isSchedulerStarted
public boolean isSchedulerStarted()
-
setScheduledExecutorService
public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
-
getErrorCounter
protected long getErrorCounter()
Gets the error counter. If the counter is > 0 that means the consumer failed polling for the last N number of times. When the consumer is successfully again, then the error counter resets to zero.- See Also:
getSuccessCounter()
-
getSuccessCounter
protected long getSuccessCounter()
Gets the success counter. If the success is > 0 that means the consumer succeeded polling for the last N number of times. When the consumer is failing again, then the success counter resets to zero.- See Also:
getErrorCounter()
-
getCounter
protected long getCounter()
Gets the total number of polls run.
-
isFirstPollDone
protected boolean isFirstPollDone()
Whether a first pool attempt has been done (also if the consumer has been restarted)
-
getLastError
protected Throwable getLastError()
Gets the last caused error (exception) for the last poll that failed. When the consumer is successfully again, then the error resets to null.
-
getLastErrorDetails
protected Map<String,Object> getLastErrorDetails()
Gets the last caused error (exception) details for the last poll that failed. When the consumer is successfully again, then the error resets to null. Some consumers can provide additional error details here, besides the caused exception. For example if the consumer uses HTTP then theHealthCheck.HTTP_RESPONSE_CODEcan be included.- Returns:
- error details, or null if no details exists.
-
addLastErrorDetail
protected void addLastErrorDetail(String key, Object value)
Adds a detail to the last caused error (exception) for the last poll that failed. When the consumer is successfully again, then the error resets to null. Some consumers can provide additional error details here, besides the caused exception. For example if the consumer uses HTTP then theHealthCheck.HTTP_RESPONSE_CODEcan be included.- Parameters:
key- the key (seeHealthCheck)value- the value
-
poll
protected abstract int poll() throws ExceptionThe 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
-
doBuild
protected void doBuild() throws Exception- Overrides:
doBuildin classDefaultConsumer- Throws:
Exception
-
initialHealthCheckState
protected org.apache.camel.health.HealthCheck.State initialHealthCheckState()
Used to allow special consumers to override the initial state of the health check (readiness check) during startup. Consumers that are internal only such as camel-scheduler uses UP as initial state because the scheduler may be configured to run only very in-frequently and therefore the overall health-check state would be affected and seen as DOWN.- Returns:
- null to use the initial state configured, otherwise force using the returned state.
-
doInit
protected void doInit() throws Exception- Overrides:
doInitin classDefaultConsumer- Throws:
Exception
-
doStart
protected void doStart() throws Exception- Overrides:
doStartin classDefaultConsumer- Throws:
Exception
-
afterConfigureScheduler
protected void afterConfigureScheduler(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler, boolean newScheduler)After the scheduler has been configured- Parameters:
scheduler- the schedulernewScheduler- true if this consumer created a new scheduler, or false if an existing (shared) scheduler is being used
-
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- Overrides:
doStopin classDefaultConsumer- Throws:
Exception
-
doShutdown
protected void doShutdown() throws Exception- Overrides:
doShutdownin classDefaultConsumer- Throws:
Exception
-
doSuspend
protected void doSuspend() throws Exception- Overrides:
doSuspendin classorg.apache.camel.support.service.BaseService- Throws:
Exception
-
onInit
public void onInit() throws Exception- Specified by:
onInitin interfaceorg.apache.camel.PollingConsumerPollingStrategy- Throws:
Exception
-
beforePoll
public long beforePoll(long timeout) throws Exception- Specified by:
beforePollin interfaceorg.apache.camel.PollingConsumerPollingStrategy- Throws:
Exception
-
-