Camel Components

Disruptor VM

Since Camel 2.12

Both producer and consumer are supported

The Disruptor component provides asynchronous SEDA behavior similarly to the standard SEDA component. However, it uses a Disruptor instead of a BlockingQueue used by the standard SEDA.

As with the SEDA component, buffers of the Disruptor endpoints are only visible within a single CamelContext and no support is provided for persistence or recovery. The buffers of the disruptor-vm: endpoints also provide support for communication across CamelContexts instances, so you can use this mechanism to communicate across web applications (as long as camel-disruptor.jar is on the system/boot classpath).

The main advantage of choosing to use the Disruptor component over the SEDA is performance in use cases where there is high contention between producer(s) and/or multicasted or concurrent consumers. In those cases, significant increases of throughput and reduction of latency has been observed. Performance in scenarios without contention is comparable to the SEDA component.

The Disruptor is implemented with the intention of mimicking the behavior and options of the SEDA component as much as possible. The main differences between them are the following:

  • The buffer used is always bounded in size (default 1024 exchanges).

  • As the buffer is always bounded, the default behaviour for the Disruptor is to block while the buffer is full instead of throwing an exception. This default behavior may be configured on the component (see options).

  • The Disruptor endpoints don’t implement the BrowsableEndpoint interface. As such, the exchanges currently in the Disruptor can’t be retrieved, only the number of exchanges.

  • The Disruptor requires its consumers (multicasted or otherwise) to be statically configured. Adding or removing consumers on the fly requires complete flushing of all pending exchanges in the Disruptor.

  • As a result of the reconfiguration: Data sent over a Disruptor is directly processed and 'gone' if there is at least one consumer, late joiners only get new exchanges published after they’ve joined.

  • The pollTimeout option is not supported by the Disruptor component.

  • When a producer blocks on a full Disruptor, it does not respond to thread interrupts.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-disruptor</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

 disruptor-vm:someId[?options]

Where someId can be any string that uniquely identifies the endpoint within the current CamelContext.

Options

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Disruptor VM component supports the following options which are listed below.

Name Description Default Type

bufferSize (common)

The number of exchanges the ring buffer of a queue holds when its endpoint does not set the size option, rounded up to the next power of two as the LMAX Disruptor requires. It applies to the ring buffers created after it is set.

1024

int

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

defaultConcurrentConsumers (consumer)

The default of the endpoint option concurrentConsumers: the number of threads that process the exchanges of each consumer.

1

int

defaultMultipleConsumers (consumer)

The default of the endpoint option multipleConsumers: whether more than one consumer may consume from the same endpoint, each one receiving every exchange (publish-subscribe).

false

boolean

defaultWaitStrategy (consumer)

The default of the endpoint option waitStrategy: the LMAX Disruptor wait strategy the consumer threads use to wait for new exchanges (Blocking, Sleeping, Yielding or BusySpin).

Enum values:

  • Blocking

  • Sleeping

  • BusySpin

  • Yielding

Blocking

DisruptorWaitStrategy

defaultBlockWhenFull (producer)

The default of the endpoint option blockWhenFull: whether a producer waits for a free slot when the ring buffer is full (true), or fails at once (false).

true

boolean

defaultProducerType (producer)

The default of the endpoint option producerType: the LMAX Disruptor producer type of the ring buffer, Multi (any number of threads publish) or Single (one thread at a time publishes).

Enum values:

  • Single

  • Multi

Multi

DisruptorProducerType

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The Disruptor VM endpoint is configured using URI syntax:

disruptor-vm:name

With the following path and query parameters:

Path Parameters

Name Description Default Type

name (common)

Required The name of the queue. All the endpoints with the same name share one ring buffer: within the CamelContext (disruptor), or between the CamelContexts of the JVM (disruptor-vm).

String

Query Parameters

Name Description Default Type

size (common)

The number of exchanges the ring buffer holds, rounded up to the next power of two as the LMAX Disruptor requires (for example 1000 becomes 1024). When not set, the bufferSize option of the component applies. All the endpoints with the same name share one ring buffer, created by the first of them: creating an endpoint whose size differs from the size of the existing ring buffer fails, and an endpoint without a size uses the existing one. To be sure of the size, set it on all the endpoints of the name.

1024

int

concurrentConsumers (consumer)

The number of threads that process the exchanges of each consumer of this endpoint. Each thread is an LMAX Disruptor event handler that sees every exchange and processes only those whose ring buffer sequence modulo concurrentConsumers is its index, so the exchanges are spread round-robin over the threads. With multipleConsumers, each consumer has this number of threads. A value greater than 500 is rejected.

1

int

multipleConsumers (consumer)

Whether more than one consumer (route) may consume from this endpoint, for publish-subscribe messaging: each consumer receives its own copy of every exchange. A producer that waits for the reply waits until all the consumers are done, and gets the failure of a consumer that failed (the results are not aggregated). When false, starting a second consumer on this endpoint fails.

false

boolean

waitStrategy (consumer)

The LMAX Disruptor wait strategy the consumer threads use to wait for new exchanges: Blocking (waits on a lock and condition, the least CPU), Sleeping (spins, then calls Thread.yield(), then parks for 100 nanoseconds at a time), Yielding (spins, then calls Thread.yield()) or BusySpin (spins without pause, the lowest latency, keeps a CPU core busy per thread). The ring buffer uses the wait strategy of the first endpoint created with its name; the value of the other endpoints is ignored.

Enum values:

  • Blocking

  • Sleeping

  • BusySpin

  • Yielding

Blocking

DisruptorWaitStrategy

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

blockWhenFull (producer)

Whether a producer that sends to a full ring buffer waits for a free slot (true), or fails the exchange at once with an IllegalStateException (false). The wait has no timeout and does not respond to thread interrupts. When not set, the defaultBlockWhenFull option of the component applies, which is true.

false

boolean

producerType (producer)

The LMAX Disruptor producer type of the ring buffer: Multi, which any number of threads can publish to at the same time, or Single, which is faster but only correct when one thread at a time publishes (Camel rejects a second producer on this endpoint, but does not stop several threads from using one producer). The ring buffer uses the producer type of the first endpoint created with its name; the value of the other endpoints is ignored.

Enum values:

  • Single

  • Multi

Multi

DisruptorProducerType

timeout (producer)

How long (in milliseconds) a producer that waits for the consumer (see waitForTaskToComplete) waits for the exchange to complete, before it fails the exchange with an ExchangeTimedOutException. The consumer then skips the exchange if it has not started it yet. The time a producer waits for a free slot of a full ring buffer does not count. Use 0 or a negative value to wait without limit.

30000

long

waitForTaskToComplete (producer)

Whether the producer waits for the consumer to complete the exchange before it continues: Always, Never, or IfReplyExpected, which waits only when the exchange expects a reply (InOut). A producer that waits (see the timeout option) gets the result of the consumer, or with multipleConsumers only the failure of a consumer that failed. The exchange property CamelAsyncWait overrides this option for one exchange.

Enum values:

  • Never

  • IfReplyExpected

  • Always

IfReplyExpected

WaitForTaskToComplete

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Body

The message body at each side of the Disruptor VM component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

unchanged

The body the producer sent.

Producer input

any

Producer output

unknown

Unchanged for a message that expects no reply (InOnly). A message that expects one (InOut), or waitForTaskToComplete=Always, waits for the body the disruptor route ends with; with more than one consumer route (multipleConsumers) the body stays unchanged.

  • With waitForTaskToComplete=Never: unchanged

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Example

The first route below sends a message to a disruptor-vm endpoint every second. The second route reads the messages with four concurrent consumers. Unlike a disruptor endpoint, a disruptor-vm endpoint can also be read by a route in another CamelContext of the same JVM.

- route:
    from:
      uri: timer:orders
      parameters:
        period: 1000
        includeMetadata: true
      steps:
        - setBody:
            expression:
              simple:
                expression: "Order ${exchangeProperty.CamelTimerCounter}"
        - to:
            uri: disruptor-vm:orders
- route:
    from:
      uri: disruptor-vm:orders
      parameters:
        concurrentConsumers: 4
      steps:
        - log:
            message: "Processing ${body}"

More Documentation

See the Disruptor component for more information.