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

public class ThreadPerTaskSedaConsumer extends SedaConsumer
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

    pollTimeout

    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

    Constructors
    Constructor
    Description
    ThreadPerTaskSedaConsumer(SedaEndpoint endpoint, org.apache.camel.Processor processor)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Hook called when poll returned no exchange.
    protected boolean
    Hook called before polling the queue.
    protected ExecutorService
    createExecutor(int poolSize)
    Creates the executor service used for consumer threads.
    long
    Returns the current number of active processing tasks.
    int
    Returns the maximum concurrent tasks allowed (0 means unlimited).
    void
    prepareShutdown(boolean suspendOnly, boolean forced)
     
    protected void
    processPolledExchange(org.apache.camel.Exchange exchange)
    Process a polled exchange.
    protected void
    Setup the thread pool and ensures tasks gets executed (if needed)
    protected void
    Shuts down the executor service.

    Methods 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, toString

    Methods 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, suspend

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    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, isStarted, isStarting, isStopped, isStopping, isSuspending

    Methods inherited from interface org.apache.camel.SuspendableService

    isSuspended, resume, suspend
  • Constructor Details

    • ThreadPerTaskSedaConsumer

      public ThreadPerTaskSedaConsumer(SedaEndpoint endpoint, org.apache.camel.Processor processor)
  • Method Details

    • createExecutor

      protected ExecutorService createExecutor(int poolSize)
      Description copied from class: SedaConsumer
      Creates the executor service used for consumer threads.

      Subclasses can override this method to provide a different executor, such as one using virtual threads.

      Overrides:
      createExecutor in class SedaConsumer
      Parameters:
      poolSize - the number of concurrent consumers
      Returns:
      the executor service
    • setupTasks

      protected void setupTasks()
      Description copied from class: SedaConsumer
      Setup the thread pool and ensures tasks gets executed (if needed)
      Overrides:
      setupTasks in class SedaConsumer
    • shutdownExecutor

      protected void shutdownExecutor()
      Description copied from class: SedaConsumer
      Shuts down the executor service.
      Overrides:
      shutdownExecutor in class SedaConsumer
    • beforePoll

      protected boolean beforePoll() throws InterruptedException
      Description copied from class: SedaConsumer
      Hook called before polling the queue. Subclasses can override to acquire resources (e.g., permits).
      Overrides:
      beforePoll in class SedaConsumer
      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: SedaConsumer
      Hook called when poll returned no exchange. Subclasses can override to release resources (e.g., permits).
      Overrides:
      afterPollEmpty in class SedaConsumer
    • processPolledExchange

      protected void processPolledExchange(org.apache.camel.Exchange exchange)
      Description copied from class: SedaConsumer
      Process a polled exchange. Subclasses can override to dispatch to another thread.
      Overrides:
      processPolledExchange in class SedaConsumer
      Parameters:
      exchange - the exchange to process
    • prepareShutdown

      public void prepareShutdown(boolean suspendOnly, boolean forced)
      Specified by:
      prepareShutdown in interface org.apache.camel.spi.ShutdownPrepared
      Overrides:
      prepareShutdown in class SedaConsumer
    • 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).