WAS v8.5 > Develop applications > Develop Service integration > Programming mediations > Add mediation function to handler code > Work with the message header

Set routing addresses in a message header

We can add function to a pre-existing mediation handler to set routing addresses in the message header.

Before starting this task, you should have created the basic mediation handler in an EJB project (see Writing a mediation handler. To work with routing addresses, you will use the SIDestinationAddress and SIDestinationAddressFactory APIs. The SIDestinationAddress is the public interface that represents an service integration bus, and gives your mediation access to the name of the destination and the bus name. SIDestinationAddressFactory enables you to create a new SIDestinationAddress to represent an service integration bus destination. For reference information about these APIs, see SIDestinationAddress and SIDestinationAddressFactory.

  1. Locate the point in your mediation handler where you insert the functional mediation code, in the method handle (MessageContext context). The interface is MessageContext, and you should cast this to SIMessageContext unless you are only interested in the methods provided by MessageContext.
  2. Get the SIMessage from the MessageContext object. For example:

  3. Build your mediation header function using these basic steps:

    1. Get a handle to the runtime environment. For example:

    2. Create a forward routing path to set on the cloned object. For example, use the Vector class to create a extendable array of objects.
    3. Get the SIDestinationAddressFactory that is to be used for creating SIDestinationAddress instances. For example:

        SIDestinationAddressFactory destFactory = SIDestinationAddressFactory.getInstance();

    4. Create a new SIDestinationAddress, representing a service integration bus destination. For example:

        SIDestinationAddress dest = destFactory.createSIDestinationAddress(remoteDestinationName(),false);
      In this case, the second parameter, the Boolean "false", indicates the destination should not be localized to the local messaging engine, but can be anywhere on the service integration bus.

    5. Use the add method of the Vector class to add another destination name to the array.
    6. Clone the message, and modify the forward routing path in the cloned message. For example:

        clonedMessage.setForwardRoutingPath(forwardRoutingPath);
    7. Send the cloned message using the send method in the SIMediationSession interface to send the message to the service integration bus. For example, if named "clonedMessage":

        mediationSession.send(clonedMessage, false);

  4. Return true to ensure the message passed into the handle method of the MediationHandler interface continues along the handler chain.


Example

The complete mediation function code to change the forward routing path might look like this example:
/* A sample mediation that clones a message 
 * and sends the clone off to another destination  */

public class RoutingMediationHandler implements MediationHandler {

 public String remoteDestinationName="newdest";

 public boolean handle(MessageContext context) throws MessageContextException {
  SIMessage clonedMessage = null;
  SIMessageContext mediationContext = (SIMessageContext) context;
  SIMessage message = mediationContext.getSIMessage();
  SIMediationSession mediationSession = mediationContext.getSession();

  // Create a forward routing path that will be set on the cloned message   Vector forwardRoutingPath = new Vector();
  SIDestinationAddressFactory destFactory = 
        SIDestinationAddressFactory.getInstance();
  SIDestinationAddress dest = 
        destFactory.createSIDestinationAddress(remoteDestinationName,false);
  forwardRoutingPath.add(dest);
  
  try {
   // Clone the message    clonedMessage = (SIMessage) message.clone();
   // Modify the forward routing path for the clone
   clonedMessage.setForwardRoutingPath(forwardRoutingPath);
   // Send the message to the next destination in the frp
   mediationSession.send(clonedMessage, false);
  } catch (SIMediationRoutingException e1) {
   e1.printStackTrace();
  } catch (SIDestinationNotFoundException e1) {
   e1.printStackTrace();
  } catch (SINotAuthorizedException e1) {
   e1.printStackTrace();
  } catch (CloneNotSupportedException e) {
   // SIMessage should clone OK so you shouldn't need to enter this block    e.printStackTrace();
  }
  // allow original message to continue on its path   return true;
 }


Subtopics


+

Search Tips   |   Advanced Search