Setting routing addresses in a message header

This topic describes how to add function to a preexisting mediation handler to set routing addresses in the message header.

Before you begin

Before you start this task, you should have created the basic mediation handler in an EJB project (see Writing a mediation handler.

About this task

To work with routing addresses, you will use the SIDestinationAddress and SIDestinationAddressFactory APIs. The SIDestinationAddress is the public interface which 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.

Procedure

  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, SIMessage message = ((SIMessageContext)context).getSIMessage();
  3. Build your mediation header function using these basic steps:
    1. Get a handle to the core runtime. For example, .... SIMediationSession mediationSession = mediationContext.getSession();
    2. Create a forward routing path to set on the cloned object. Use, for example the Vector class to create a extendable array of objects.
    3. Get the SIDestinationAddressFactory which is to be used for creating SIDestinationAddress instances. For instance, SIDestinationAddressFactory destFactory = SIDestinationAddressFactory.getInstance();
    4. Create a new SIDestinationAddress, representing an SIBus Destination. For instance, SIDestinationAddress dest = destFactory.createSIDestinationAddress(remoteDestinationName(),false);. In this case, the second parameter, the boolean false, indicates that 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 instance, 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 changer the forward routing path may look similar to this example:
/* A sample mediation that simply 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 which 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 we shouldn't really enter this block
			e.printStackTrace();
		}
		// allow original message to continue on its path
		return true;
	}



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 12:02:36 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-zos&topic=tjy1505
File name: tjy1505.html