Writing a routing mediation

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

Before you begin

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 Working with mediations.

This topic assumes that you are familiar with using a Java Platform, Enterprise Edition (Java EE) session bean development environment such as the assembly tools or IBM® Rational® Application Developer.

About this task

A routing mediation is a mediation application that contains 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 you 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 Java Platform, Enterprise Edition (Java EE) session bean development environment to complete the following steps:

Procedure

  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.
    Note: When you do this, you specify a name by which the mediation handler list is known. Make a note of this name, for later reference when you create the mediation in the bus.
  3. Add the routing function to the handler.
    Before you begin, review Adding mediation function to handler code, in particular its subtopic Working with message context. Add import statements to your handler class, and modify the handle method by adding your 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

You are now ready to install your mediation into WebSphere Application Server and associate it with a bus destination, as described in Working with mediations.