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.HostedService
,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
-
Field Summary
Fields inherited from class org.apache.camel.support.service.BaseService
BUILT, FAILED, INITIALIZED, INITIALIZING, lock, NEW, SHUTDOWN, SHUTTING_DOWN, STARTED, STARTING, status, STOPPED, STOPPING, SUSPENDED, SUSPENDING
-
Constructor Summary
ConstructorsConstructorDescriptionScheduledPollConsumer
(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
Modifier and TypeMethodDescriptionprotected void
addLastErrorDetail
(String key, Object value) Adds a detail to the last caused error (exception) for the last poll that failed.protected void
afterConfigureScheduler
(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler, boolean newScheduler) After the scheduler has been configuredvoid
long
beforePoll
(long timeout) protected void
doBuild()
protected void
doInit()
protected void
protected void
doStart()
protected void
doStop()
protected void
protected void
Forces the consumer to be marked as ready.int
int
int
int
long
Gets the total number of polls run.long
getDelay()
long
Gets the error counter.long
protected Throwable
Gets the last caused error (exception) for the last poll that failed.Gets the last caused error (exception) details for the last poll that failed.org.apache.camel.spi.PollingConsumerPollStrategy
long
org.apache.camel.LoggingLevel
org.apache.camel.spi.ScheduledPollConsumerScheduler
long
Gets the success counter.protected org.apache.camel.health.HealthCheck.State
Used to allow special consumers to override the initial state of the health check (readiness check) during startup.boolean
Whether the consumer is ready and has established connection to its target system, or first poll has been completed successfully.boolean
Whether a first pool attempt has been done (also if the consumer has been restarted).boolean
isGreedy()
protected boolean
boolean
Whether polling is currently in progressboolean
boolean
boolean
boolean
void
onInit()
protected abstract int
poll()
The polling method which is invoked periodically to poll this consumerprotected int
poll
(org.apache.camel.Exchange dynamic) The polling method which is invoked periodically to poll this consumer, for components that supportDynamicPollingConsumer
such as camel-file.protected void
No messages to poll so send an empty message instead.void
run()
Invoked whenever we should be polledvoid
setBackoffErrorThreshold
(int backoffErrorThreshold) void
setBackoffIdleThreshold
(int backoffIdleThreshold) void
setBackoffMultiplier
(int backoffMultiplier) void
setDelay
(long delay) void
setGreedy
(boolean greedy) void
setInitialDelay
(long initialDelay) void
setPollStrategy
(org.apache.camel.spi.PollingConsumerPollStrategy pollStrategy) void
setRepeatCount
(long repeatCount) void
setRunLoggingLevel
(org.apache.camel.LoggingLevel runLoggingLevel) void
setScheduledExecutorService
(ScheduledExecutorService scheduledExecutorService) void
setScheduler
(org.apache.camel.spi.ScheduledPollConsumerScheduler scheduler) void
setSchedulerProperties
(Map<String, Object> schedulerProperties) void
setSendEmptyMessageWhenIdle
(boolean sendEmptyMessageWhenIdle) void
setStartScheduler
(boolean startScheduler) void
setTimeUnit
(TimeUnit timeUnit) void
setUseFixedDelay
(boolean useFixedDelay) void
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, handleException, isHostedService, releaseExchange, setExceptionHandler, setHealthCheck, setRoute, setRouteId, toString
Methods inherited from class org.apache.camel.support.service.BaseService
build, doFail, doLifecycleChange, doResume, fail, getInternalLock, 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
Methods inherited from interface org.apache.camel.Service
build, close, init, start, stop
Methods inherited from interface org.apache.camel.ShutdownableService
shutdown
Methods inherited from interface org.apache.camel.StatefulService
getStatus, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspending
Methods inherited from interface org.apache.camel.SuspendableService
isSuspended, resume, suspend
-
Constructor Details
-
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 Details
-
run
public void run()Invoked whenever we should be polled -
processEmptyMessage
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
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) -
getSchedulerProperties
-
setSchedulerProperties
-
getInitialDelay
public long getInitialDelay() -
setInitialDelay
public void setInitialDelay(long initialDelay) -
getDelay
public long getDelay() -
setDelay
public void setDelay(long delay) -
getTimeUnit
-
setTimeUnit
-
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
-
isSchedulerStarted
public boolean isSchedulerStarted() -
setScheduledExecutorService
-
getErrorCounter
public 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
public 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:
-
getCounter
public long getCounter()Gets the total number of polls run. -
isFirstPollDone
public boolean isFirstPollDone()Whether a first pool attempt has been done (also if the consumer has been restarted). -
isConsumerReady
public boolean isConsumerReady()Whether the consumer is ready and has established connection to its target system, or first poll has been completed successfully. The health-check is using this information to know when the consumer is ready for readiness checks. -
forceConsumerAsReady
protected void forceConsumerAsReady()Forces the consumer to be marked as ready. This can be used by components that need to mark this sooner than usual (default marked as ready after first poll is done). This allows health-checks to be ready before an entire poll is completed. This is for example needed by the FTP component as polling a large file can take long time, causing a health-check to not be ready within reasonable time. -
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
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_CODE
can be included.- Returns:
- error details, or null if no details exists.
-
addLastErrorDetail
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_CODE
can be included.- Parameters:
key
- the key (seeHealthCheck
)value
- the value
-
poll
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
-
poll
The polling method which is invoked periodically to poll this consumer, for components that supportDynamicPollingConsumer
such as camel-file.- Parameters:
dynamic
- the current exchange when being used from Poll and PollEnrich EIPs in dynamic mode,- 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
- Overrides:
doBuild
in 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
- Overrides:
doInit
in classDefaultConsumer
- Throws:
Exception
-
doStart
- Overrides:
doStart
in 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
- Overrides:
doStop
in classDefaultConsumer
- Throws:
Exception
-
doShutdown
- Overrides:
doShutdown
in classDefaultConsumer
- Throws:
Exception
-
doSuspend
- Overrides:
doSuspend
in classorg.apache.camel.support.service.BaseService
- Throws:
Exception
-
onInit
- Specified by:
onInit
in interfaceorg.apache.camel.PollingConsumerPollingStrategy
- Throws:
Exception
-
beforePoll
- Specified by:
beforePoll
in interfaceorg.apache.camel.PollingConsumerPollingStrategy
- Throws:
Exception
-
afterPoll
- Specified by:
afterPoll
in interfaceorg.apache.camel.PollingConsumerPollingStrategy
- Throws:
Exception
-