You can add function to a pre-existing 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 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.
Procedure
- 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.
- Get the SIMessage from the MessageContext object. For
example:
SIMessage message = ((SIMessageContext)context).getSIMessage();
- Build your mediation header function by using these basic steps:
- Get a handle to the runtime environment. For
example:
.... SIMediationSession mediationSession = mediationContext.getSession();
- Create a forward routing path to set on the cloned object.
For example, use the Vector class to create a extendable array of objects.
- Get the SIDestinationAddressFactory that is to be used for
creating SIDestinationAddress instances. For example:
SIDestinationAddressFactory destFactory = SIDestinationAddressFactory.getInstance();
- 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 that
the destination should not be localized to the local messaging engine, but
can be anywhere on the service integration bus.
- Use the add method of the Vector class to add another destination
name to the array.
- Clone the message, and modify the forward routing path in the
cloned message. For example:
clonedMessage.setForwardRoutingPath(forwardRoutingPath);
- Send the cloned message by 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);
- 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;
}