Once
Since Camel 4.17
Only consumer is supported
The Once component is used to generate a message only once. For example to trigger a message after Camel has been started, or for development and testing purposes.
This component is designed to be very basic and only a few options.
You can find more features when using the Timer component, which also can be configured to only trigger once with repeatCount=1.
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 Once component supports 4 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 | |
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 number of milliseconds to wait before triggering. Use 0 or negative to fire as soon as possible. The default value is 1000. | 1000 | long | |
Whether Camel languages are supported such as simple,groovy. | true | boolean |
Endpoint Options
The Once endpoint is configured using URI syntax:
once:name
With the following path and query parameters:
Query Parameters (8 parameters)
| Name | Description | Default | Type |
|---|---|---|---|
The data to use as message body. You can externalize the data by using file: or classpath: as prefix and specify the location of the file. | String | ||
The data to use as message headers as key=value pairs. You can externalize the data by using file: or classpath: as prefix and specify the location of the file. This is a multi-value option with prefix: header. | Map | ||
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 | ||
The number of milliseconds to wait before triggering. The default value is 1000. | 1000 | long | |
The data to use as exchange properties as key=value pairs. You can externalize the data by using file: or classpath: as prefix and specify the location of the file. This is a multi-value option with prefix: exchangeProperty. | Map | ||
The data to use as exchange variables as key=value pairs. You can externalize the data by using file: or classpath: as prefix and specify the location of the file. This is a multi-value option with prefix: variable. | Map |
Example
To set up a route that generates an event every 60 seconds:
from("once:foo?body=file:data.json").to("bean:myBean?method=someMethodName"); The above route will trigger once and load the data.json from file system and use as message body. And then route to call the bean.
And the route in XML DSL:
<route>
<from uri="once:foo?body=file:data.json"/>
<to uri="bean:myBean?method=someMethodName"/>
</route> Using headers and variables
You can also specify headers and variables using multivalue, where each header is prefixed with header.<key>. In the sample below we set 2 headers as follows: foo=abc and bar=123.
from("once:tick?body=world&header.foo=abc&header.bar=123")
.to("bean:myBean"); You can do the same for variables with variable.<key>.
And in YAML DSL you can use YAML map’s directly as shown below:
- route:
from:
uri: once
parameters:
name: hello
body: Hello World
headers:
foo: foolish
bar: 456
steps:
- to: mock:result This requires that the uri does not have any configuration so it must be uri: once and not uri: once:hello. |
Using custom languages
You can use the Camel languages such as simple, or groovy when specifying body, headers, exchange properties, and variables.
The once component will set the data in the following order:
-
variables
-
exchangeProperties
-
headers
-
body
This makes it possible to use groovy or simple language for setting the message body, based on data from the variables and headers.
For example:
from("once:tick?body=language:groovy:file:src/test/resources/calc.groovy&variable.amount=123")
.to("mock:result"); You must use language:groovy: as prefix when using languages. And you can combine this with loading from file. The calc.groovy file is as follows:
variable.amount * 3 As you can see this just tells groovy to multiply the amount variable with 3.
Automatic type conversion
The body, header, and variables will automatically be converted to the best suitable type for boolean and integers:
-
boolean
-
int
-
long
And for any other its String or the output from executing a language.
Firing as soon as possible
By default, the component is fired after 1 seconds when Camel has been fully started. If you want to fire messages in a Camel route as soon as possible, you can use a negative delay:
<route>
<from uri="once:foo?body=file.data.json&delay=-1"/>
<to uri="bean:myBean?method=someMethodName"/>
</route> In this way, the timer will fire messages immediately.
Spring Boot Auto-Configuration
When using once with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-once-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> The component supports 5 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
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 number of milliseconds to wait before triggering. Use 0 or negative to fire as soon as possible. The default value is 1000. | 1000 | Long | |
Whether to enable auto configuration of the once component. This is enabled by default. | Boolean | ||
Whether Camel languages are supported such as simple,groovy. | true | Boolean |