//tag::ref-doc[]
= Router Sink

This application routes messages to named channels.

== Options

The **$$router$$** $$sink$$ has the following options:

//tag::configuration-properties[]
$$router.default-output-binding$$:: $$Where to send un-routable messages.$$ *($$String$$, default: `$$<none>$$`)*
$$router.destination-mappings$$:: $$Destination mappings as a new line delimited string of name-value pairs, e.g. 'foo=bar\n baz=car'.$$ *($$Properties$$, default: `$$<none>$$`)*
$$router.expression$$:: $$The expression to be applied to the message to determine the channel(s) to route to. Note that the payload wire format for content types such as text, json or xml is byte[] not String!. Consult the documentation for how to handle byte array payload content.$$ *($$Expression$$, default: `$$<none>$$`)*
$$router.refresh-delay$$:: $$How often to check for script changes in ms (if present); < 0 means don't refresh.$$ *($$Integer$$, default: `$$60000$$`)*
$$router.resolution-required$$:: $$Whether channel resolution is required.$$ *($$Boolean$$, default: `$$false$$`)*
$$router.script$$:: $$The location of a groovy script that returns channels or channel mapping resolution keys.$$ *($$Resource$$, default: `$$<none>$$`)*
$$router.variables$$:: $$Variable bindings as a new line delimited string of name-value pairs, e.g. 'foo=bar\n baz=car'.$$ *($$Properties$$, default: `$$<none>$$`)*
$$router.variables-location$$:: $$The location of a properties file containing custom script variable bindings.$$ *($$Resource$$, default: `$$<none>$$`)*
//end::configuration-properties[]

NOTE: This router sink is based on a `StreamBridge` API from Spring Cloud Stream, therefore destinations can be created as needed.
In this case a `defaultOutputBinding` can only be reached if key is not included into `destinationMappings`.
The `resolutionRequired = true` neglects `defaultOutputBinding` and throws an exception if no mapping and no respective binding already declared.

You can restrict the creation of dynamic bindings using the `spring.cloud.stream.dynamicDestinations` property.
By default, all resolved destinations will be bound dynamically; if this property has a comma-delimited list of destination names, only those will be bound.
Messages that resolve to a destination that is not in this list will be routed to the `defaultOutputBinding`, which must also appear in the list.

The `destinationMappings` are used to map the evaluation results to an actual destination name.

== SpEL-based Routing

The expression evaluates against the message and returns either a channel name, or the key to a map of channel names.

For more information, please see the "Routers and the Spring Expression Language (SpEL)" subsection in the Spring Integration Reference manual https://docs.spring.io/spring-integration/reference/html/router.html#router-namespace[Configuring a Generic Router] section.

== Groovy-based Routing

Instead of SpEL expressions, Groovy scripts can also be used. Let's create a Groovy script in the file system at "file:/my/path/router.groovy", or "classpath:/my/path/router.groovy" :

[source,groovy]
----
println("Groovy processing payload '" + payload + "'")
if (payload.contains('a')) {
    return "foo"
}
else {
    return "bar"
}
----

If you want to pass variable values to your script, you can statically bind values using the _variables_ option or optionally pass the path to a properties file containing the bindings using the _propertiesLocation_ option.
All properties in the file will be made available to the script as variables. You may specify both _variables_ and _propertiesLocation_, in which case any duplicate values provided as _variables_ override values provided in _propertiesLocation_.
Note that _payload_ and _headers_ are implicitly bound to give you access to the data contained in a message.

For more information, see the Spring Integration Reference manual https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#groovy[Groovy Support].

//end::ref-doc[]
