Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Mediations made simple

Write SIBus mediations using E4X and sample code

Anthony Elder (ant.elder@uk.ibm.com), Consulting IT Specialist, IBM
Anthony Elder is a developer in the IBM Hursley development laboratory working in the area of web services, messaging, and Enterprise Service Bus.
Paul Fremantle, Senior Technical Staff Member, IBM Application and Integration Middleware
Paul Fremantle is an architect working in the IBM Hursley development laboratory on web services and XML intermediaries. He is the co-author of Building Web Services in Java 2nd Edition, as well as a number of articles and publications on XML and web services.

Summary:  Use the messaging and web services support that IBM® WebSphere® Application Server V6 provides to transform and route web services requests. The authors demonstrate how you can use scripting and lightweight intermediary code to create a new web service from existing services. The code they provide takes away much of the hard work involved, and provides a simple, extensible framework for building and using script-based mediations in a WebSphere Application Server-based Enterprise Service Bus. The authors also introduce you to the Service Integration Bus (SIBus), ECMAScript for XML (E4X), and the WSMediations package.

Date:  08 Jul 2005
Level:  Advanced

Activity:  3919 views
Comments:  

Introduction

WebSphere Application Server web services and messaging

IBM WebSphere Application Server V6 (WebSphere Application Server) introduces the Service Integration Bus (SIBus) support. The SIBus enables integration between loosely-coupled applications and services. Applications connect to the SIBus and are supported using message-based and Service-Oriented Architecture. A SIBus installation can have a number of destinations. Destinations are logical addresses which can have the behavior of a service endpoint, a queue, or a topic. Most importantly, destinations are active. In other words, they can perform work on the message as it passes through that destination. This is enabled by attaching a mediation to the destination.

The SIBus supports web services by using Inbound and Outbound service definitions. These define how a destination is exposed as a SOAP endpoint, and how it talks to other SOAP endpoints. Web serviceS clients external to the SIBus can send requests to an Inbound service, and web services external to the SIBus can be invoked from within the SIBus by sending requests to an Outbound service. Web services messages in the SIBus flowing between Inbound services and Outbound services can then be routed and transformed using mediations.

The SIBus also supports a mix and match approach, where SOAP/HTTP and JMS messages are routed and mediated. For example, a message can come in as SOAP/HTTP and be sent to a JMS consumer. The mediation framework is very important in this scenario because it allows the messages to be modified to match each end of the communications.

We recommend you read the two articles on this subject listed in the Resources section: "Working with the Enterprise Service Bus and Mediations", and "Building an Enterprise Service Bus with WebSphere Application Server."

The XML language E4X

ECMAScript for XML (E4X) is an extension to the JavaScript language which adds direct support for XML. It lets you create, manipulate, and use XML directly from JavaScript without requiring the use of any other XML library or API. This makes working with XML in E4X much simpler and easier than using alternatives such as DOM. It is also a more natural programming model for most Java and script programmers than XSLT. We strongly recommend you read our previous developerWorks series referenced in Resources, "AJAX and scripting web services with E4X, Part 1: Introducing E4X."

In this paper we show you how to use E4X for SIBus mediations. We also show how, when combined with the WSMediations package sample code, this can lead to very rapid development of powerful SIBus mediation flows.

The WSMediations package

The WSMediations package provides a simple framework for making SIBus mediations easy to use. Because it uses a convention over configuration approach, the work required to set up a mediation is minimized. This feature, combined with the simplicity of E4X scripts, means new mediation flows can be created much faster than when implementing flows from scratch using Enterprise JavaBeans (EJB) and XSLT transforms.

The WSMediations package uses a single mediation EJB that gets customized for each mediation based on the destination to which it runs. Mediations are written as E4X scripts, and the scripts are stored in a WebSphere Application Server shared library. That means, instead of rebuilding and redeploying an EJB, all that is required in order to update a script is to update a JAR file in the shared library. The scripts are named the same as the destination they are associated with, which enables the single mediation EJB to automatically discover the script to run at each mediated destination without requiring any specific configuration.

While the SIBus allows two dimensions of mediation flow (flows through destinations and chains of mediation at each destination), for the purposes of simplicity, we chose to support one mediation at each destination and recommend a flow of mediations to be defined as a number of destinations.

Effectively, we are promoting a pattern of usage of the SIBus in which mediation is defined by using a set of special destinations whose only purpose is to mediate a message.

The script environment that the WSMediation framework provides extends the standard WebSphere Application Server mediations APIs to provide simplified methods for getting and setting the XML representation of a message. This enables easy access and transformation of messages using E4X. For SOAP-based web service messages from or to SIBus Inbound and Outbound service destinations, the mediation framework is able to automatically determine the new Service Data Object (SDO) format string when messages have been transformed. This makes transformations very easy to code.

You can create a new mediation in three simple steps:

  1. Create a new SIBus destination.
  2. Mediate it with the WSMediation EJB.
  3. Add a mediation script with the same name as the destination into the scripts-shared library.

Configure WebSphere Application Server to use the WSMediations package

Before using E4X and the WSMediations, you must complete the following steps to install the WSMediations package into WebSphere:

  • Install the WSMediations EAR file.
  • Define the WSMediations EJB as a mediation handler.
  • Create a WebSphere Application Server shared library to hold the mediation scripts.
  • Add xbeans.jar and replace js.jar in the WebSphere Application Server lib directory (to provide E4X support).

These steps are described in more detail in the readme file within the ws-mediationcode.zip file that you can download from this paper.


The CurrencyStock choreography example

Now that we've explained all the parts and have the WSMediations code installed into WebSphere Application Server, we're ready to start writing mediations. To demonstrate how to use the simplified mediations, we show you how to create a new web service from a choreography of existing services. The choreography is effectively a new service whose logic is completely provided by "gluing" together existing services. This is the same example shown in Part 2 of the "AJAX and scripting web services with E4X" series (see Resources), but this time we use the WebSphere Application Server SIBus run-time instead of Apache AXIS. If you still haven't read that article (and why not!?) then this simple service takes two existing services -- a stock quote service and a currency service -- and provides a new service -- stock quotes in your local currency.

Two SIBus Oubound services are created, one for the xMethods stockquote service, and the other for the xMethods currency exchange rate service. We then use a SIBus Inbound service to expose a new currencystock service which enables external web services clients to get stock prices adjusted by currency. In this example, SIBus mediations are used to transform the CurrencyStock request into two new requests for the xMethods StockQuote and currency conversion services, which then execute concurrently. Finally, another mediation merges the two xMethod responses into a single CurrencyStock response, which is returned to the original web services client.

Figure 2 shows this choreography configuration.

WebSphere Application Server SIBus resources used to create the CurrencyStock choreography


Figure 2. The SIBus services and mediations used for the choreography
The SIBus services and mediations used for the choreography

Configure the WebSphere Application Server resources

Create the Outbound services:

  • Create the xmethods stockquote service at: http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl.
  • Create the xmethods currency conversion service at: http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl.

As the Outbound service and destination names generated by default are a bit unwieldy, you should change the default service and destination names to xmethodsStockquote and xmethodsCurrency when defining the Outbound service in the Name the outbound service and destinations panel.

Create the mediation destinations:

  • CurrencyStockIBS
  • StockquoteXForm
  • CurrencyXForm
  • CurrencyReply
  • MergeReplies

Attach the WSMediation to all of those destinations except for the CurrencyReply destination.

Define a SIBus InboundService for the new CurrencyStock service:

  • Use the CurrencyStockIBS destination and the CurrencyStockquote.wsdl supplied in the downloadable zip file ws-mediationcode.zip

The resulting WebSphere Application Server destinations should look like the wsadmin panel shown in Figure 3.


Figure 3. wsadmin panel with resulting WebSphere Application Server destinations
wsadmin panel with resulting WebSphere Application Server destinations

The final piece of configuration is to set the forward and reverse routing paths on some of the new destinations:

  • CurrencyStockIBS frp=:StockquoteXForm replyDest=MergeReplies replyBus=dWESB
  • StockquoteXForm frp=:xmethodsStockquote
  • CurrencyXForm frp=:xmethodsCurrency replyDest=CurrencyReply replyBus=dWESB

Figure 4 shows the wsadmin panel showing how to configure the routing paths on a destination.


Figure 4. Configuring a destination's forward and reply destinations
Configuring a destination's forward and reply destinations

The mediation E4X scripts

Now all that is left to do is to create the mediation scripts and add them to the scripts shared library.

Listing 1 shows the mediation script that is associated with the CurrencyStockIBS destination. Line 3 makes a copy of the current message, line 4 sends that copy to the CurrencyXForm destination, and returning true on line 6 means that the current message will continue on to the next destination in the forward routing path (the StockquoteXForm destination).

This copy script has the CurrencyXForm destination name hardcoded in it. An improvement would be to use a destination context property to define the copy-to destinations. The WSMediations framework provides methods giving scripts easy access to destination context properties (for example, the getDestinationProperty(key) method on the MessageContext). By using these methods you could write a general purpose mediation that copies a message to one or many other destinations based on the context property settings. However, we use this simple hardcoding approach in order to keep the example simple.


Listing 1. The CurrencyStockIBS mediations script
 1.  function process(mc) {
 2.  
 3.    var msg = mc.getSIMessage().clone();
 4.    mc.send(msg,"CurrencyXForm");
 5.  
 6.    return true;
 7.  }

SIBus message format strings

A SIBus message has a Service Data Object (SDO) payload and a format string which describes that payload. The structure and value of this format string depends on many things, such as the payload protocol, WSDL definitions, and target destination for the message. When messages are restricted to only web services type messages, the WSMediations code is able to compute the format string automatically. This greatly simplifies the task of transforming messages.

Listing 2 shows the mediation script that is associated with the StockquoteXForm destination. This script converts from a CurrencyStock request into an xMethod StockQuote request, and it shows how simple and clear an XML programming language like E4X makes working with SOAP messages.

Line 3 and 4 extract the share symbol from the CurrencyStock SOAP request. Lines 6 to 14 create a new SOAP envelope suitable for the xMethods StockQuote service. This is simply a copy-and-paste from an example SOAP message from the xMethods web site. Line 11 uses the E4X curly brackets {} syntax to insert the share symbol extracted on line 4 into the envelope.

Line 16 sets the new envelope as the payload on the message. Using the reformatPayload method tells the WSMediations framework that the structure of the SOAP envelope has been changed and therefore the SDO format string needs to be recalculated. See the sidebar describing SIBus message format strings.

The CurrencyXForm script is not shown here as it is almost the same as the StockquoteXForm script -- only the SOAP message is changed to match the xMethods currency conversion service.


Listing 2. The StockquoteXForm mediation script
 1.  function process(mc) {
 2.  
 3.     var origReq = mc.getPayload();
 4.     var symbol = origReq..*::symbol.toString();
 5.     
 6.     var envelope = 
 7.        <s:Envelope xmlns:sq="urn:xmethods-delayed-quotes" 
 8.                    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
 9.           <s:Body>
10.              <sq:getQuote>
11.                 <symbol>{symbol}</symbol>
12.              </sq:getQuote>
13.           </s:Body>
14.        </s:Envelope>
15.  
16.     mc.reformatPayload(envelope);
17.     
18.     return true;
19.  }

SIBus message IDs

It might sound odd or even dangerous for two messages to have the same message ID, but this is perfectly fine. A SIBus message also has a system message ID and that will always be unique. The API message and correlation IDs are for application use, so you are free to set them to whatever you like.

Listing 3 shows the mediation script that is associated with the MergeReplies destination. This script receives the responses from StockQuote and currency services, extracts the returned values, and constructs a CurrencyStock response SOAP message. This mostly works in a very similar way to what has already been described for the StockquoteXForm script, with the additional function on lines 6 and 7, which receive the currency response message from the CurrencyReply destination. This works by using the correlation ID of the response message and probably needs a little more explanation:

A SIBus message has two IDs -- a message ID and a correlation ID. When the Outbound service is invoked, the SIBus service invoker sets the correlation ID on the response message to the same value as the message ID of the request message. This enables the request and response messages to be linked together. As the copy script on the CurrencystockIBS destination clones the request message, the resulting two request messages both have the same message ID. This means that now when the MergeReplies script is trying to receive the currency response message, it can use the correlation ID from the StockQuote response, because the StockQuote and currency requests both used the same message ID.


Listing 3. The MergeReplies mediation script
 1.  function process(mc) {
 2.  
 3.     var stockquoteEnvelope = mc.getPayload();
 4.     var ticker = stockquoteEnvelope..*::Result.toString();
 5.     
 6.     var correlationID = mc.getCorrelationID();
 7.     var currencyEnvelope = mc.receiveXML("CurrencyReply", correlationID);
 8.     var rate = currencyEnvelope..*::Result.toString();
 9.  
10.     var envelope = 
11.        <s:Envelope xmlns:cs="http://sample" 
12.                    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
13.           <s:Body>
14.              <cs:getQuoteResponse>
15.                 <getQuoteReturn>{rate*ticker}</getQuoteReturn>
16.              </cs:getQuoteResponse>
17.           </s:Body>
18.        </s:Envelope>
19.  
20.     mc.reformatPayload("CurrentcyStockIBS", envelope);
21.  
22.     return true;
23.  }


Test the new CurrencyStock web service

That's it! You can now test the new CurrencyStock service from any web services client. In the sample download you can find a simple HTML page that uses E4X, so it requires the latest developer version of Mozilla to run. Alternatively, you could use the web services explorer utility provided with IBM Rational® Application Developer, or use your favorite web services tool to create a web services client from the currencystock.wsdl.

Figure 6 shows an example of the E4X Mozilla client and the result of requesting the IBM share price in my homeland currency. You can get a list of the currencies that the xMethods currency exchange rate service supports from its help page (see Resources).


Figure 6. Test the CurrencyStock service with Mozilla
Test the CurrencyStock service with Mozilla

Conclusion

In this paper, the authors showed you how you can use the combination of a simple framework (WSMediation) together with a lightweight XML scripting language (E4X) to do simple and powerful web services routing and mediation. The power and ease of this approach makes building ESBs around WebSphere Application Server V6 faster and more productive.



Download

DescriptionNameSizeDownload method
Source code for this paperws-mediationcode.zip2,263 KBHTTP

Information about download methods


Resources

About the authors

Anthony Elder is a developer in the IBM Hursley development laboratory working in the area of web services, messaging, and Enterprise Service Bus.

Paul Fremantle is an architect working in the IBM Hursley development laboratory on web services and XML intermediaries. He is the co-author of Building Web Services in Java 2nd Edition, as well as a number of articles and publications on XML and web services.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and web services
ArticleID=88449
ArticleTitle=Mediations made simple
publish-date=07082005
author1-email=ant.elder@uk.ibm.com
author1-email-cc=pzf@uk.ibm.com, flanders@us.ibm.com
author2-email=pzf@uk.ibm.com
author2-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers