Set Variable

The SetVariable EIP is used for setting an Exchange variable.

Options

The Set Variable eip supports 0 options, which are listed below.

Name Description Default Type

note

Sets the note of this node.

String

description

Sets the description of this node.

String

disabled

Disables this EIP from the route.

false

Boolean

name

Required Name of variable to set a new value The simple language can be used to define a dynamic evaluated variable name to be used. Otherwise a constant name will be used.

String

expression

Required Expression to return the value of the variable.

ExpressionDefinition

Exchange properties

The Set Variable eip has no exchange properties.

Example

The following example shows how to set a variable on the exchange in a Camel route:

  • Java

  • XML

  • YAML

from("direct:a")
    .setVariable("myVar", constant("test"))
    .to("direct:b");
<route>
    <from uri="direct:a"/>
    <setVariable name="myVar">
        <constant>test</constant>
    </setVariable>
    <to uri="direct:b"/>
</route>
- route:
    from:
      uri: direct:a
      steps:
        - setVariable:
            name: myVar
            expression:
              constant:
                expression: test
        - to:
            uri: direct:b

Setting a variable from a message header

You can also set a variable with the value from a message header.

  • Java

  • XML

  • YAML

from("direct:a")
    .setVariable("foo", header("bar"))
    .to("direct:b");
<route>
    <from uri="direct:a"/>
    <setVariable name="foo">
        <header>bar</header>
    </setVariable>
    <to uri="direct:b"/>
</route>
- route:
    from:
      uri: direct:a
      steps:
        - setVariable:
            name: foo
            expression:
              header:
                expression: bar
        - to:
            uri: direct:b

Setting variable with the current message body

It is of course also possible to set a variable with a value from anything on the Exchange such as the message body, where we use the Simple language to refer to the message body:

  • Java

  • XML

  • YAML

from("direct:a")
    .setVariable("myBody", simple("${body}"))
    .to("direct:b");
<route>
    <from uri="direct:a"/>
    <setVariable name="myBody">
        <simple>${body}</simple>
    </setVariable>
    <to uri="direct:b"/>
</route>
- route:
    from:
      uri: direct:a
      steps:
        - setVariable:
            name: myBody
            expression:
              simple:
                expression: "${body}"
        - to:
            uri: direct:b