Set Variables
The SetVariables EIP is used for setting multiple Exchange variables at the same time.
Options
The Set Variables eip supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
note | The note for this node. | String | |
description | The description for this node. | String | |
disabled | Whether to disable this EIP from the route during build time. Once an EIP has been disabled then it cannot be enabled later at runtime. | false | Boolean |
variables | Required Contains the variables to be set. | List |
Message Body
The message body at each side of the Set Variables EIP: EIP input is the body it needs, EIP output the body after it, and Steps the body its own steps start with.
| Side | Body | Description |
|---|---|---|
EIP output |
| Sets variables: the body stays as it was. |
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 are described in the Camel Catalog.
Using Set Variables
The following example shows how to set multiple variables in a Camel route using Java, XML or YAML. Note that the syntax is slightly different in each case.
-
Java
-
XML
-
YAML
from("direct:a")
.setVariables("myVar", constant("test"), "otherVar", constant("other"))
.to("direct:b"); <route>
<from uri="direct:a"/>
<setVariables>
<setVariable name="myVar">
<constant>test</constant>
</setVariable>
<setVariable name="otherVar">
<constant>other</constant>
</setVariable>
</setVariables>
<to uri="direct:b"/>
</route> - route:
from:
uri: direct:a
steps:
- setVariables:
variables:
- name: myVar
constant: test
- name: otherVar
constant: other
- to:
uri: direct:b For example, the variables values are constants.
Any of the Camel languages can be used, such as Simple.
-
Java
-
XML
-
YAML
from("direct:a")
.setVariables("randomNumber", simple("${random(1,100)}"), "body", simple("${body}"))
.to("direct:b"); <route>
<from uri="direct:a"/>
<setVariables>
<setVariable name="randomNumber">
<simple>${random(1,100)}</simple>
</setVariable>
<setVariable name="body">
<simple>${body}</simple>
</setVariable>
</setVariables>
<to uri="direct:b"/>
</route> - route:
from:
uri: direct:a
steps:
- setVariables:
variables:
- name: randomNumber
expression:
simple:
expression: "${random(1,100)}"
- name: body
expression:
simple:
expression: "${body}"
- to:
uri: direct:b Setting a variable from another variable
You can also set several variables where later ones depend on earlier ones.
In the example, we first set the variable foo to the body and then set bar based on comparing foo with a value.
-
Java
-
XML
-
YAML
from("direct:a")
.setVariables("foo", simple("${body}"), "bar", simple("${variable.foo} > 10", Boolean.class))
.to("direct:b"); <route>
<from uri="direct:a"/>
<setVariables>
<setVariable name="foo">
<simple>${body}</simple>
</setVariable>
<setVariable name="bar">
<simple resultType="java.lang.Boolean">${variable.foo} > 10</simple>
</setVariable>
</setVariables>
<to uri="direct:b"/>
</route> - route:
from:
uri: direct:a
steps:
- setVariables:
variables:
- name: foo
expression:
simple:
expression: "${body}"
- name: bar
expression:
simple:
expression: "${variable.foo} > 10"
resultType: "boolean"
- to:
uri: direct:b Using a Map with Java DSL
It’s also possible to build a Map and pass it as the single argument to setVariables(). If the order in which the variables should be set is important, use a LinkedHashMap.
Map<String, Expression> variableMap = new java.util.LinkedHashMap<>();
variableMap.put("foo", ConstantLanguage.constant("ABC"));
variableMap.put("bar", ConstantLanguage.constant("XYZ"));
from("direct:startMap")
.setVariables(variableMap)
.to("direct:b"); If the ordering is not critical, then Map.of(name1, expr1, name2, expr2…) can be used.
from("direct:startMap")
.setVariables(Map.of("foo", "ABC", "bar", "XYZ"))
.to("direct:b");