Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Creating Custom Routers

Typically, you implement custom routing in Mule with filters, but occasionally you may need to implement a custom router.

Custom Outbound Routers

Outbound routers control how a message gets routed to a list of endpoints. For example, sometimes a message gets routed based on simple rules or business logic, whereas in other cases you may multicast a message to every router.

The easiest way to write an outbound router is to extend the org.mule.routing.outbound.AbstractOutboundRouterLeaving the Site class:

import org.mule.routing.outbound.AbstractOutboundRouter;

public class CustomOutboundRouter extends AbstractOutboundRouter
{
....
}
java

There are two methods you must implement that control how messages are routed through the system. First, you must implement the isMatch method. This determines if a message should be processed by the router.

For example, to route only messages that have a payload containing the string "hello":

public boolean isMatch(MuleMessage message) throws RoutingException
{
   return "hello".equals(message.getPayloadAsString());
}
text

The second method you must implement is the route method. Each outbound router has a list of endpoints that are associated with it. The route method contains the logic to control how the event is propagated to the endpoints.

For example, if there were two endpoints you want to route to based on a condition, you would use this method to select the endpoint:

MuleEvent route(MuleEvent event) throws MessagingException
{
    OutboundEndpoint ep = null;
    if (isConditionMet(event))
    {
        ep = getRoutes().get(0);
    }
    else
    {
        ep = getRoutes().get(1);
    }
....
text

Once you’ve selected an endpoint, you must call process() on it.