Routers are a crucial element in many messaging architectures. They consume Messages from a Message Channel and forward each consumed message to one or more different Message Channel depending on a set of conditions.
There are few types of routers available out of the box:
Route the message based on class type of the message payload.
RouterBuilder::createPayloadTypeRouter(
"routingChannelName",
[
\stdClass::class => 'channel1',
Order::class => 'channel2'
])
A HeaderValueRouter will send Messages to the channel based on the individual header value mappings. When a HeaderValueRouter is created it is initialized with the name of the header to be evaluated.
$headerToEvaluateName = "emailType";
RouterBuilder::createHeaderValueRouter("routingChannelName", $headerName, [
'private' => 'channel1',
'public' => 'channel2'
])
Points to Reference, that must return target channel name or array of channel names.
$referenceName = "isHighValueOrder";
RouterBuilder::create("routingChannelName", $referenceName, "route")
class isHighValueOrderService
{
public function route(Order $order) : string
{
if ($order->isHighValueOrder()) {
return "exlusiveChannelName";
}
return "normalChannelName";
}
}
If router will not receive channel name to resolve, default channel can be set.
RouterBuilder::create("routingChannelName", "someReference", "route")
->withDefaultResolutionChannel("debugChannel")
If router will not resolve any channel and default resolution is not set, then it will throw exception on default.
This behaviour can be changed, so it will behave like Message Filter
RouterBuilder::create("routingChannelName", "someReference", "route")
->setResolutionRequired(false)