Stub
Since Camel 2.10
Both producer and consumer are supported
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,*.yamlfiles, 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 Stub component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
The default of the concurrentConsumers option of the endpoints: the number of threads that poll the queue and process the messages. | 1 | int | |
The default of the pollTimeout option of the endpoints: the time in milliseconds a consumer thread waits for a message before it checks whether it should keep running. | 1000 | int | |
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 | |
The default of the blockWhenFull option of the endpoints: whether a producer that sends to a full queue waits until there is free space, instead of failing. | false | boolean | |
The default of the discardWhenFull option of the endpoints: whether a producer that sends to a full queue drops the message and continues, instead of failing. | false | boolean | |
The default of the offerTimeout option of the endpoints: the time in milliseconds a producer waits for free space in a full queue, when blockWhenFull is enabled. 0 waits without a time limit. | long | ||
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 | |
The BlockingQueueFactory that creates the queues, unless an endpoint gives its own queue or queueFactory. The default creates a LinkedBlockingQueue. | BlockingQueueFactory | ||
The default capacity of the queues: the maximum number of messages a queue can hold, used when an endpoint does not set size. 0 or less creates the queues with the default capacity of the queue factory, which is unbounded (Integer.MAX_VALUE) for the default LinkedBlockingQueue. | 1000 | int | |
Whether to also register each stub endpoint in the endpoint registry under its URI without the stub: prefix, so that looking up kafka:cheese returns the endpoint of stub:kafka:cheese. | false | boolean | |
The pattern that selects which stub endpoints shadow is applied to. It is matched against the whole endpoint URI including the stub: prefix, as an exact URI, a wildcard with a trailing * (such as stub:kafka*), or a regular expression. If not set, shadow applies to all stub endpoints. | String |
Endpoint Options
The Stub endpoint is configured using URI syntax:
stub:name
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The capacity of the queue: the maximum number of messages it can hold. If not set, the queueSize of the component is used. The endpoint that creates the queue sets its capacity, and an endpoint with the same name that sets a different size fails to start. A PriorityBlockingQueue (from PriorityBlockingQueueFactory) is unbounded and uses it only as its initial capacity. | 1000 | int | |
The number of threads that poll the queue and process the messages (at most 500, unless limitConcurrentConsumers is disabled). When virtualThreadPerTask is enabled, it is instead the maximum number of messages processed at the same time, where 0 means no limit; when not set explicitly, the default of 1 is changed to 0 (no limit). | 1 | int | |
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 | |
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 | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
Whether to limit the number of concurrentConsumers to the maximum of 500. By default, an exception will be thrown if an endpoint is configured with a greater number. You can disable that check by turning this option off. | true | boolean | |
Whether several consumers can consume from the queue, each receiving its own copy of every message (publish-subscribe). When disabled, each message goes to one consumer only. Enable it on every consumer endpoint of the queue: a consumer whose setting differs from that of the queue fails to start. | false | boolean | |
The timeout (in milliseconds) used when polling. When a timeout occurs, the consumer can check whether it is allowed to continue running. Setting a lower value allows the consumer to react more quickly upon shutdown. | 1000 | int | |
Whether to discard the messages waiting in the queue when the consumer stops, so the route stops at once instead of waiting until they are processed. Each discarded message fails with a RejectedExecutionException, which also ends the wait of a producer waiting for its reply. Suspending the route keeps the messages. | false | boolean | |
Whether the consumer processes each message in a task of its own, instead of in a fixed pool of concurrentConsumers threads: one thread polls the queue and hands each message to a new task of a cached thread pool, and concurrentConsumers becomes the maximum number of tasks at the same time (0 means no limit). The tasks run on virtual threads when they are enabled with camel.threads.virtual.enabled=true, and on platform threads otherwise. | false | boolean | |
The time in milliseconds the producer waits for the consumer route to process the message, when it waits (see waitForTaskToComplete). On timeout the exchange fails with ExchangeTimedOutException, and the message is removed from the queue if no consumer has taken it yet. 0 or a negative value waits without a time limit. | 30000 | long | |
Whether the producer waits until the consumer route has processed the message, and then uses its result as the reply. Always waits, Never does not wait (the caller continues with its message unchanged), and IfReplyExpected waits only for a message that expects a reply (exchange pattern InOut). The exchange property CamelAsyncWait overrides it for one message. Enum values:
| IfReplyExpected | WaitForTaskToComplete | |
Whether a producer that sends to a full queue waits until there is free space (up to offerTimeout), instead of failing at once with IllegalStateException (Queue full). It cannot be enabled together with discardWhenFull. | false | boolean | |
Whether the producer drops the message, instead of adding it to the queue, when no consumer is active on the queue (no route consumes from it, or its route is suspended or stopped). A producer that waits for the reply fails with RejectedExecutionException. When failIfNoConsumers is also enabled, it takes precedence. | false | boolean | |
Whether a producer that sends to a full queue drops the message and continues, instead of failing with IllegalStateException (Queue full). A producer that waits for the reply fails with RejectedExecutionException instead. It cannot be enabled together with blockWhenFull. | false | boolean | |
Whether the producer fails with SedaConsumerNotAvailableException, instead of adding the message to the queue, when no consumer is active on the queue (no route consumes from it, or its route is suspended or stopped). It takes precedence over discardIfNoConsumers. | false | boolean | |
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 | |
The time in milliseconds a producer waits for free space in a full queue, when blockWhenFull is enabled, before it fails with IllegalStateException. 0 waits without a time limit. | long | ||
Not used by this endpoint: browsing it returns all the messages waiting in the queue. The value is only shown as the browse limit in JMX. | 100 | int | |
A custom BlockingQueue to use as the queue, instead of one that the queue factory creates. Its remaining capacity becomes the size. It is only used when no queue with this name exists yet; otherwise the existing queue is used. | BlockingQueue |
Message Body
The message body at each side of the Stub 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 |
| The body the producer sent. |
Producer input |
| |
Producer output |
| Unchanged when no route consumes from the stub endpoint (it keeps the latest size messages and drops the oldest), or for a message that expects no reply (InOnly). A message that expects one (InOut) waits for the body the stub route ends with. |
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
Internally, the Stub component creates Seda endpoints. The main difference between Stub and Seda is that Seda will validate the URI and parameters you give it, so putting seda: in front of a typical URI with query arguments will usually fail. Stub won’t, though, as it basically ignores all query parameters to let you quickly stub out one or more endpoints in your route temporarily.
Stub endpoints without a consumer
A stub endpoint that no route consumes from keeps the latest messages sent to it, so you can browse them, for example with camel cmd stub --browse from the Camel CLI. It keeps up to size messages (1000 by default), and when the queue is full, it drops the oldest message to make room for the new one. Sending to a stub endpoint without a consumer therefore never fails with Queue full, and the memory it uses stays bounded.
When a route consumes from the stub endpoint, the messages are queued as with Seda, and the size, blockWhenFull and discardWhenFull options apply as usual.
Examples
Here are a few samples of stubbing endpoint uris
stub:smtp://somehost.foo.com?user=whatnot&something=else stub:http://somehost.bar.com/something
In a route, put stub: in front of the endpoint you do not want to call. The message goes to an in-memory queue instead. Another route can read the queue from the same stub endpoint, for example to log what would have been sent. The query parameters do not matter: stub:smtp://somehost.foo.com is the same queue.
- route:
from:
uri: timer:orders
parameters:
repeatCount: 1
steps:
- setBody:
expression:
constant:
expression: A new order has arrived
- to:
uri: "stub:smtp://somehost.foo.com?user=whatnot&something=else"
- route:
from:
uri: "stub:smtp://somehost.foo.com"
steps:
- log:
message: "Stubbed mail: ${body}"