Camel Components

Dataset

Since Camel 1.3

Both producer and consumer are supported

Testing of distributed and asynchronous processing is notoriously challenging. The Mock, DataSet, and DataSet Test endpoints work with the Camel Testing Framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel’s large range of Components together with the powerful Bean Integration.

The DataSet component provides a mechanism to easily perform load & soak testing of your system. It works by allowing you to create DataSet instances both as a source of messages and as a way to assert that the data set is received.

Camel will use the throughput logger when sending the dataset.

URI format

dataset:name[?options]

Where name is used to find the DataSet instance in the Registry

Camel ships with a support implementation of org.apache.camel.component.dataset.DataSet, the org.apache.camel.component.dataset.DataSetSupport class, that can be used as a base for implementing your own data set. Camel also ships with some implementations that can be used for testing: org.apache.camel.component.dataset.SimpleDataSet, org.apache.camel.component.dataset.ListDataSet and org.apache.camel.component.dataset.FileDataSet, all of which extend DataSetSupport.

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 Dataset component supports the following options which are listed below.

Name Description Default Type

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

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

log (producer)

Whether the endpoints of this component log each received message at INFO level, as one line formatted by exchangeFormatter. The log option of an endpoint overrides it.

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

exchangeFormatter (advanced)

Autowired The ExchangeFormatter that formats each received message for the log line of the log option. By default, a DefaultExchangeFormatter that shows the exchange id, the body type and the body (up to 10000 characters).

ExchangeFormatter

Endpoint Options

The Dataset endpoint is configured using URI syntax:

dataset:name

With the following path and query parameters:

Path Parameters

Name Description Default Type

name (common)

Required The name of the DataSet bean to look up in the registry. The DataSet creates the messages that the consumer sends, and the messages that the producer expects to receive.

DataSet

Query Parameters

Name Description Default Type

dataSetIndex (common)

How the CamelDataSetIndex header (the index of the message in the DataSet, starting at 0) is used. The consumer sets the header on each message it sends, unless off. The producer, with strict, requires the header on each message and checks that it equals the number of messages received before; with lenient, it checks the header when present and sets it when missing; with off, it neither checks nor sets the header.

Enum values:

  • strict

  • lenient

  • off

lenient

String

initialDelay (consumer)

The time in milliseconds that the consumer waits after it started before it sends the messages of the DataSet. The preloaded messages (preloadSize) are sent before, without this delay.

1000

long

minRate (consumer)

The minimum message rate, in messages per second, that keeps the assertions of the producer waiting: when not all the messages of the DataSet have arrived within resultWaitTime, assertIsSatisfied() keeps waiting in steps of 1 second as long as at least this number of messages arrived in the last second. 0 turns it off.

0

int

preloadSize (consumer)

The number of messages that the consumer sends while it starts, so the start of the route waits until they are processed. The other messages of the DataSet are sent afterwards from a background thread, after initialDelay.

0

long

produceDelay (consumer)

The time in milliseconds that the consumer waits after each message it sent (after the route has processed it), to set the rate of the messages. This also applies to the preloaded messages. 0 sends the messages without delay.

3

long

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

assertPeriod (producer)

The time in milliseconds that assertIsSatisfied() waits after the expectations are met, before it checks them all again. The first check passes as soon as the expected number of messages arrived, so a period is needed to detect messages that arrive afterwards, for example to assert that exactly 5 messages arrive, or with expectedCount 0 that none arrive. 0 checks only once.

long

consumeDelay (producer)

The time in milliseconds that the producer waits after it has received and checked each message, to simulate slow processing. The thread that sent the message waits as well, and the endpoint receives one message at a time. 0 means no delay.

0

long

expectedCount (producer)

The number of messages that this endpoint must receive. assertIsSatisfied() waits up to resultWaitTime for them, and then checks that exactly this number arrived; a message that arrives after the check is not seen, unless assertPeriod is set. With 0, the check runs at once, so set sleepForEmptyTest or assertPeriod to give messages time to arrive. -1 sets no expectation on the number of messages. Expectations that set the messages, such as expectedBodiesReceived(), replace this value, and a dataset endpoint replaces it with the size of its DataSet.

-1

int

copyOnExchange (producer (advanced))

Whether to keep a copy of each received Exchange, instead of the Exchange itself. The copy has its own message, headers and exchange properties (the body object is shared), so changes that the route makes after this endpoint do not affect the received Exchanges and the expectations. Set to false to save memory. A dataset endpoint turns it off unless set.

true

boolean

failFast (producer (advanced))

Whether to check the expectations on each message as it arrives (the expectations that check each message, such as on bodies and headers), so that assertIsSatisfied() fails at the first failed expectation without waiting for the remaining messages. When false, assertIsSatisfied() waits for all the expected messages (up to resultWaitTime) and then checks the expectations.

true

boolean

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

log (producer (advanced))

Whether to log each received message at INFO level, as one line formatted by the exchangeFormatter of the component.

false

boolean

reportGroup (producer (advanced))

The size of the groups for throughput logging: after each group of this many received messages, the endpoint logs at INFO level, with the logger org.apache.camel.component.mock:name, the number of messages received so far and the message rate. Must be a positive number. Only the mock component applies it; the components that extend it, such as dataset, ignore it.

int

resultMinimumWaitTime (producer (advanced))

The minimum time in milliseconds that assertIsSatisfied() must wait for the expected number of messages: the assertion fails when they arrive sooner. It is checked only when the messages had not all arrived yet when assertIsSatisfied() was called. 0 turns it off.

long

resultWaitTime (producer (advanced))

The maximum time in milliseconds that assertIsSatisfied() waits for the expected number of messages to arrive, before it checks the expectations. 0 waits 10 seconds. MockEndpoint.assertIsSatisfied(context, timeout, unit) sets this option on each mock endpoint.

long

retainFirst (producer (advanced))

The number of the first received Exchanges that this endpoint keeps, to save memory in tests with many messages. The endpoint still counts every message, so expectedCount works, but the expectations on the content of the messages (such as bodies and headers) look up the kept Exchanges by position, so they can only check the kept ones. Together with retainLast, the endpoint keeps the first and the last Exchanges; alone, it keeps none after the first ones. 0 keeps none. -1 sets no limit, and the endpoint keeps every Exchange when retainLast is -1 as well.

-1

int

retainLast (producer (advanced))

The number of the last received Exchanges that this endpoint keeps, to save memory in tests with many messages. The endpoint still counts every message, so expectedCount works, but the expectations on the content of the messages (such as bodies and headers) look up the kept Exchanges by position, so they can only check the kept ones. Together with retainFirst, the endpoint keeps the first and the last Exchanges; alone, it keeps only the last ones. 0 keeps none. -1 sets no limit, and the endpoint keeps every Exchange when retainFirst is -1 as well.

-1

int

sleepForEmptyTest (producer (advanced))

The time in milliseconds that assertIsSatisfied() waits, when expectedCount is 0, before it checks that no message arrived. 0 checks at once.

long

browseLimit (advanced)

Has no effect on this endpoint: it does not limit the Exchanges that it keeps by this option (use retainFirst and retainLast). The value is only reported as the browse limit, for example in JMX.

100

int

Message Headers

The Dataset component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelDataSetIndex (common)

Constant: DATASET_INDEX

The index of the message in the DataSet, starting at 0. The consumer sets it on each message it sends (unless dataSetIndex is off). The producer checks it against the number of messages received before, depending on dataSetIndex.

Long

Message Body

The message body at each side of the Dataset 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

unknown

The body the DataSet bean creates for each message: the defaultBody of a SimpleDataSet (the text <hello>world!</hello> unless set), the bodies of a ListDataSet in turn, or the text of a FileDataSet.

Producer input

any

Each message is compared with the body the DataSet creates for its index. A mismatch fails the assertions of the endpoint, not the message.

Producer output

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.

Usage

Configuring DataSet

Camel will look up in the Registry for a bean implementing the DataSet interface. So you can register your own data set as:

<bean id="myDataSet" class="com.mycompany.MyDataSet">
  <property name="size" value="100"/>
</bean>

DataSetSupport (abstract class)

The DataSetSupport abstract class is a nice starting point for new data set, and provides some useful features to derived classes.

Additional Properties on DataSetSupport

Property Type Default Description

defaultHeaders

Map<String,Object>

null

Specify the default message headers. They are the same for each message; though if you want to create custom headers per message, create your own derivation of DataSetSupport.

outputTransformer

org.apache.camel.Processor

null

size

long

10

Specify how many messages to send/consume.

reportCount

long

-1

Specify the number of messages to be received before reporting progress. Useful for showing the progress of a large load test. If smaller than zero (` < 0`), then size / 5, if is 0 then size, else set to reportCount value.

SimpleDataSet

The SimpleDataSet extends DataSetSupport, and adds a default body.

Additional Properties on SimpleDataSet

Property Type Default Description

defaultBody

Object

<hello>world!</hello>

Specify the default message body. By default, the SimpleDataSet produces the same constant payload for each exchange. If you want to customize the payload for each exchange, create a Camel Processor and configure the SimpleDataSet to use it by setting the outputTransformer property.

ListDataSet

The List`DataSet` extends DataSetSupport, and adds a list of default bodies.

Additional Properties on ListDataSet

Property Type Default Description

defaultBodies

List<Object>

empty LinkedList<Object>

Specify the default message body. By default, the ListDataSet selects a constant payload from the list of defaultBodies using the CamelDataSetIndex. If you want to customize the payload, create a Camel Processor and configure the ListDataSet to use it by setting the outputTransformer property.

size

long

the size of the defaultBodies list

Specify how many messages to send/consume. This value can be different from the size of the defaultBodies list. If the value is less than the size of the defaultBodies list, some of the list elements will not be used. If the value is greater than the size of the defaultBodies list, the payload for the exchange will be selected using the modulus of the CamelDataSetIndex and the size of the defaultBodies list (i.e., CamelDataSetIndex % defaultBodies.size() )

FileDataSet

The FileDataSet extends ListDataSet, and adds support for loading the bodies from a file.

Additional Properties on FileDataSet

Property Type Default Description

sourceFile

File

null

Specify the source file for payloads

delimiter

String

\z

Specify the delimiter pattern used by a java.util.Scanner to split the file into multiple payloads.

Example

For example, to test that a set of messages are sent to a queue and then consumed from the queue without losing any messages:

  • Java

  • XML

  • YAML

// send the dataset to a queue
from("dataset:foo").to("activemq:SomeQueue");

// now lets test that the messages are consumed correctly
from("activemq:SomeQueue").to("dataset:foo");
<!-- send the dataset to a queue -->
<route>
  <from uri="dataset:foo"/>
  <to uri="activemq:SomeQueue"/>
</route>
<!-- now lets test that the messages are consumed correctly -->
<route>
  <from uri="activemq:SomeQueue"/>
  <to uri="dataset:foo"/>
</route>
# send the dataset to a queue
- route:
    from:
      uri: dataset:foo
      steps:
        - to:
            uri: activemq:SomeQueue
# now lets test that the messages are consumed correctly
- route:
    from:
      uri: activemq:SomeQueue
      steps:
        - to:
            uri: dataset:foo

The above would look in the Registry to find the foo DataSet instance which is used to create the messages.

Then you create a DataSet implementation, such as using the SimpleDataSet as described below, configuring things like how big the data set is and what the messages look like etc.