324 Patterns: Implementing Self-Service in an SOA Environment
Rational Application Developer provides support for developing mediation
handler code and adding mediation handlers to the J2EE deployment
descriptors.
10.6.1 Create the router mediation
The router mediation performs the following tasks:
򐂰 Examines the delivery type specified in the incoming message.
򐂰 If the type is Home, no action is required. The forward routing path of the
message is already set to the HomeDelivery Web service.
򐂰 If the type is Business, it modifies the forward routing path of the message to
send it to route it to the BusinessDelivery Web service.
򐂰 If the type is All, it:
Clones the message.
Modifies the forward routing path of one message to BusinessDelivery.
Modifies the reverse routing path of both messages to a queue destination
(for aggregation).
Use the following steps to create the router mediation:
1. Build the code:
a. Create a simple project called RoutingMediations.
b. Right-click the RoutingMediations project and select Add Others
Java Class. Name the class RoutingMediation.
c. Implement the MediationHandler interface and add the routing code in the
handle method (see “RouterMediation code” on page 325).
d. Create a JAR file from the package and call it RoutingMediations.jar.
2. Create the mediation EJB:
a. Create an EJB project called RoutingMediationsEJB with an EAR file
name of RoutingMediationsEJBEAR.
b. Drag and drop RoutingMediations.jar to the Utility JARS folder in
RoutingMediationsEJBEAR.
3. Define the mediation handler in the EJB deployment descriptor:
a. Expand the RoutingMediationsEJB project in the navigator and double-
click Deployment Descriptor.
b. Select the Mediation Handler tab.
Chapter 10. Web services scenario 325
c. Click the Add button and enter RoutingHandler as the name. Enter
myPackage.RoutingMediation as the handler class.
d. Save the descriptor.
RouterMediation code
The handle() method (Example 10-3) is invoked by the arrival of a message on
the mediated port. It gets a handle to the MessageContext interface, which
provides methods to manage a property set. Message context properties enable
handlers in a handler chain to share processing related state. The context
properties used for the mediation (Table 10-3 on page 350) are defined when you
prepare the runtime for mediation.
The handle() method casts the message context to SIMessageContext. This is
the object that is required on the interface of a mediation handler. In addition to
the context information that can be passed from one handler to another, it can
return a reference to an SIMessage and an SIMediationSession. The SIMessage
is the service integration bus representation of the message being processed by
the MediationHandler. An SIMessage contains message properties, header
contents, routing path, and the message body. The SIMediationSession is a
handle to the runtime resources.
Example 10-3 RoutingMediation part 1
public class RoutingMediation implements MediationHandler {
public boolean handle(MessageContext context) throws MessageContextException {
String businessDestination=(String)context.getProperty("businessDestination");
SIMessageContext ctx =(SIMessageContext)context;
SIMediationSession session = ctx.getSession();
String busName = session.getBusName();
SIMessage message = ctx.getSIMessage();
SIMessage newMessage=null;
List frp=null;
SIDestinationAddressFactory factory =
SIDestinationAddressFactory.getInstance();
Next (Example 10-4 on page 326), we retrieve the DataGraph object from the
message. The message is inspected to find the type of delivery the customer has
requested-Home, Delivery, or All (both).
Note: The entire source code for the router mediation can be seen by
downloading the sample application. The code is in the RoutingMediations.jar
in the RoutingMediationsEJB project.
326 Patterns: Implementing Self-Service in an SOA Environment
Example 10-4 RoutingMediation part 2
try {
DataGraph graph = message.getDataGraph();
DataObject rootNode = graph.getRootObject();
DataObject infoNode = rootNode.getDataObject("Info");
// Get the body node
DataObject bodyNode = infoNode.getDataObject("body");
// Get the data object for the first part of the body
DataObject part1Node = bodyNode.getDataObject("parameters");
// Determine the delivery option requested by the customer (Home, Business, All)
String ticker = part1Node.getString((String)context.getProperty("type"));
If the user has requested both home and business delivery (type=All), the next
section (Example 10-5) clones the message.
The original message has a forward routing path that will send it to the
HomeDelivery service. The forward routing path in the cloned message is set to
send it to the BusinessDelivery service, using the businessDestination context
property.
The reverse routing path for both messages is changed to the value of the
DeliveryResponseDestination context property. In this case, the property is set to
a destination queue defined on the bus. Once the forward and reverse routing
paths are properly set, the new message is sent.
Example 10-5 RoutingMediation part 3
if (ticker.equalsIgnoreCase((String)context.getProperty("ALL")))
{
newMessage = (SIMessage)message.clone();
List frp1= newMessage.getForwardRoutingPath();
newMessage.setApiMessageId(message.getApiMessageId());
newMessage.setDataGraph(message.getDataGraph(),message.getFormat());
SIDestinationAddress businessDestAddress = factory.
createSIDestinationAddress(businessDestination,busName);
frp1.add(businessDestAddress);
newMessage.setForwardRoutingPath(frp1);
List reverse1 = newMessage.getReverseRoutingPath();
System.out.println("Reverse Routing Path -- "+reverse1);
SIDestinationAddress deliveryResponseAddress = factory.

Get Patterns: Implementing Self-Service in an SOA Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.