Class ThreadPerTaskSedaConsumer
java.lang.Object
org.apache.camel.support.service.BaseService
org.apache.camel.support.service.ServiceSupport
org.apache.camel.support.DefaultConsumer
org.apache.camel.component.seda.SedaConsumer
org.apache.camel.component.seda.ThreadPerTaskSedaConsumer
- All Implemented Interfaces:
AutoCloseable, Runnable, org.apache.camel.Consumer, org.apache.camel.EndpointAware, org.apache.camel.health.HealthCheckAware, 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.spi.ShutdownAware, org.apache.camel.spi.ShutdownPrepared, org.apache.camel.StatefulService, org.apache.camel.Suspendable, org.apache.camel.SuspendableService
A SEDA consumer that spawns a new thread/task for each message instead of using a fixed pool of long-running consumer
threads.
This consumer model is optimized for virtual threads (JDK 21+) where creating threads is very cheap, but it also
works with platform threads. The key differences from SedaConsumer are:
- Uses a cached thread pool instead of a fixed pool
- A single coordinator thread polls the queue
- Each message is processed in its own task/thread
- The concurrentConsumers setting becomes a concurrency limit (0 = unlimited)
When virtual threads are enabled via camel.threads.virtual.enabled=true, the cached thread pool will use
Executors.newThreadPerTaskExecutor(), providing optimal scaling for I/O-bound workloads.
-
Field Summary
Fields inherited from class SedaConsumer
pollTimeoutFields 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
ConstructorsConstructorDescriptionThreadPerTaskSedaConsumer(SedaEndpoint endpoint, org.apache.camel.Processor processor) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidHook called when poll returned no exchange.protected booleanHook called before polling the queue.protected ExecutorServicecreateExecutor(int poolSize) Creates the executor service used for consumer threads.longReturns the current number of active processing tasks.intReturns the maximum concurrent tasks allowed (0 means unlimited).voidprepareShutdown(boolean suspendOnly, boolean forced) protected voidprocessPolledExchange(org.apache.camel.Exchange exchange) Process a polled exchange.protected voidSetup the thread pool and ensures tasks gets executed (if needed)protected voidShuts down the executor service.Methods inherited from class SedaConsumer
deferShutdown, doResume, doRun, doShutdown, doStart, doStop, doSuspend, getEndpoint, getPendingExchangesSize, getPollTimeout, isRunAllowed, isShutdownPending, onProcessingDone, prepareExchange, run, sendToConsumers, setShutdownPendingMethods inherited from class org.apache.camel.support.DefaultConsumer
createExchange, createUoW, defaultConsumerCallback, doBuild, doInit, doneUoW, getAsyncProcessor, getExceptionHandler, getHealthCheck, getProcessor, getRoute, getRouteId, handleException, handleException, handleException, isHostedService, releaseExchange, setExceptionHandler, setHealthCheck, setRoute, setRouteId, toStringMethods inherited from class org.apache.camel.support.service.BaseService
build, doFail, doLifecycleChange, fail, getInternalLock, getStatus, init, isBuild, isInit, isNew, isShutdown, isStarted, isStarting, isStartingOrStarted, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspendMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.camel.Service
build, close, init, start, stopMethods inherited from interface org.apache.camel.ShutdownableService
shutdownMethods inherited from interface org.apache.camel.StatefulService
getStatus, isStarted, isStarting, isStopped, isStopping, isSuspendingMethods inherited from interface org.apache.camel.SuspendableService
isSuspended, resume, suspend
-
Constructor Details
-
ThreadPerTaskSedaConsumer
-
-
Method Details
-
createExecutor
Description copied from class:SedaConsumerCreates the executor service used for consumer threads. Subclasses can override this method to provide a different executor, such as one using virtual threads.- Overrides:
createExecutorin classSedaConsumer- Parameters:
poolSize- the number of concurrent consumers- Returns:
- the executor service
-
setupTasks
protected void setupTasks()Description copied from class:SedaConsumerSetup the thread pool and ensures tasks gets executed (if needed)- Overrides:
setupTasksin classSedaConsumer
-
shutdownExecutor
protected void shutdownExecutor()Description copied from class:SedaConsumerShuts down the executor service.- Overrides:
shutdownExecutorin classSedaConsumer
-
beforePoll
Description copied from class:SedaConsumerHook called before polling the queue. Subclasses can override to acquire resources (e.g., permits).- Overrides:
beforePollin classSedaConsumer- Returns:
- true to proceed with polling, false to skip this poll iteration
- Throws:
InterruptedException- if interrupted while acquiring resources
-
afterPollEmpty
protected void afterPollEmpty()Description copied from class:SedaConsumerHook called when poll returned no exchange. Subclasses can override to release resources (e.g., permits).- Overrides:
afterPollEmptyin classSedaConsumer
-
processPolledExchange
protected void processPolledExchange(org.apache.camel.Exchange exchange) Description copied from class:SedaConsumerProcess a polled exchange. Subclasses can override to dispatch to another thread.- Overrides:
processPolledExchangein classSedaConsumer- Parameters:
exchange- the exchange to process
-
prepareShutdown
public void prepareShutdown(boolean suspendOnly, boolean forced) - Specified by:
prepareShutdownin interfaceorg.apache.camel.spi.ShutdownPrepared- Overrides:
prepareShutdownin classSedaConsumer
-
getActiveTaskCount
public long getActiveTaskCount()Returns the current number of active processing tasks. -
getMaxConcurrentTasks
public int getMaxConcurrentTasks()Returns the maximum concurrent tasks allowed (0 means unlimited).
-