Launching Routes from a Custom Module
You can trigger the execution of a new route from within a custom module. The child route executes and returns the output message of the last module back to the custom module that invoked it. Your custom module can then use the out child route's output as its own output, modify the output, or ignore it and generate its own output.
The child route executes in the same thread as its parent route. This synchronous execution means that your custom module waits until the child route completes before it continues. You can trigger more than one child route from the same parent route, but each route finishes execution before the next one is called.
To execute a new route from a custom module:
- Inject an instance of AKRouteService in your custom module. For more information on injecting services, see Invoking Services in Custom Modules.
- Create a new exchange object. You do this with the
newExchange()method, which takes the parent exchange, the ID of the child route to execute, and a message that is the input to the new route.
- Start the new route with the AKRouteService class's
invokeRoute()method. This method takes the new exchange object.
- Handle the result of the child route: the exchange object contains the response the the route. This includes properties of the AKExchange object as well as the AKMessage object.
The following example passes the custom module's input message to a child route. When the child route finishes execution, this custom module passes the output of the child route to the next module in the parent route:
Because the child route takes an AKMessage object as its input, you can add headers, change the body, or perform any other operation on the child route's input that you might perform on any message. For more information, see Creating an Output Message.
Within the child route, you can get access to the parent's request by calling
getAKRequest() on the child's exchange.
Child routes can call custom modules that execute additional child routes. Each route executes within its own exchange, so they are independent from one another. Modules in child routes can share data only with other modules in the child route by using exchange properties. Modules in a child route cannot access exchange properties of the parent route, and vice versa. For information on using exchange properties, see Using Exchange Properties in a Custom Module.