+

Search Tips   |   Advanced Search

Writing a routing mediation

Use this topic to create a mediation that chooses a particular forward route for a message.

For an introduction to using mediations with the service integration bus, see Learning about mediations. For details of how to install a mediation into WebSphere Application Server and associate it with a bus destination, see Work with mediations.

This topic assumes that you are familiar with using a JEE session bean development environment such as the assembly tools or IBM Rational Application Developer.

A routing mediation is a mediation application containing a routing handler. You associate a routing mediation with a service integration bus destination, and use the mediation to choose a particular route from a range of available routes. For example when you create a new outbound service configuration or modify an existing outbound service configuration we can apply a port selection mediation to choose a particular outbound port from the range of ports that are available to the outbound service.

To create a routing mediation, use a JEE session bean development environment to complete the following steps:

  1. Create an empty mediation handler project. This creates the project, and creates the handler class that implements the handler interface. For detailed instructions on how to do this, see Writing the mediation handler.

  2. Use the mediation pane on the EJB descriptor to define the handler class as a mediation handler.

    When you do this, specified a name by which the mediation handler list is known. Make a note of this name, for later reference when creating the mediation in the bus.

  3. Add the routing function to the handler. Before beginning, review Adding mediation function to handler code, in particular its subtopic Work with message context. Add import statements to the handler class, and modify the handle method by adding the routing code. Specify the routing destination by adding that destination to the front of the forward routing path list. The forward routing path list is available from the message context. For example:
    import javax.xml.rpc.handler.MessageContext;
    import com.ibm.websphere.sib.mediation.handler.MediationHandler;
    import com.ibm.websphere.sib.mediation.handler.MessageContextException;
    import com.ibm.websphere.sib.mediation.messagecontext.SIMessageContext;
    import com.ibm.websphere.sib.SIMessage;
    import com.ibm.websphere.sib.SIDestinationAddress;
    import com.ibm.websphere.sib.SIDestinationAddressFactory;
    import java.util.List;
    public class RouteMediationHandler implements MediationHandler {
      public boolean handle(MessageContext ctx) throws MessageContextException {
      SIMessageContext siCtx = (SIMessageContext) ctx;
      SIMessage msg = siCtx.getSIMessage();
      List frp = msg.getForwardRoutingPath();
      try {
       SIDestinationAddress destination =
        SIDestinationAddressFactory
         .getInstance()
         .createSIDestinationAddress(
         "RoutingDestination", //this is the name of the target destination      false);
       frp.add(0, destination);
      } catch (Exception e) {
       return false;
      }
      msg.setForwardRoutingPath(frp);
      return true;
     }
     }
    For more information about the service integration technologies classes, including the mediation handler and message context classes, see the Generated API documentation - Application programming interfaces .

  4. Export the routing mediation enterprise application.


What to do next

We are now ready to install the mediation into WebSphere Application Server and associate it with a bus destination, as described in Work with mediations.