IBM® WebSphere® ESB V6.0.2 introduced WebSphere MQ bindings, which let you send and receive messages via WebSphere MQ. The MQ binding automatically adds an MQ Message Descriptor (MQMD) to any messages that don't have one, but does not let you modify the MQMD. However, if the message already contains an MQMD, then the binding uses it instead of creating a new one. Therefore if an MQMD is created before the message reaches the MQ binding, you have a level of control over the content of the header.
With the introduction of the MQ bindings came a new API, enabling you to add an MQMD and you own headers to a message. You can use this API in a custom mediation to modify the Service Message Object (SMO) to include these MQ headers, thus ensuring that when the MQ binding receives the message, it does not create a new MQMD, but instead uses the existing one.
Software used in this article:
- WebSphere Integration Developer V6.1
- WebSphere ESB V6.1 Integrated Test Environment (included with WebSphere Integration Developer)
- WebSphere MQ V6.0.2.2
Start by creating a simple Library and Mediation Module in which to use custom mediation:
- Create a new Library called
MQHeaderLibrary. - Open the dependencies on the library, and under Predefined Resources, click Schema for simple JMS Data Bindings and click Save. This step makes the JMS body types available.
- In the Library, create a new interface named
MQBytesInterface. - Create a one-way operation, leaving the operation name as the default.
- Change the type of
input1to JMSBytesBody and click Save to save the interface. - Create a new mediation module named
MQHeaderModule. Ensure that Create mediation component is ticked and click Next. - Tick the MQHeaderLibrary and click Finish.
- Add an import to the assembly diagram.
- Wire the mediation component to the import, and when asked to add a reference, select MQBytesInterface.
- Right-click on Import1 and select Generate Binding => Messaging Binding => MQ Binding.
- Specify your queue manager name, send destination, host name, server channel, and port of your WebSphere MQ installation.
- Under Data Configuration, click Browse.
- Select the Show predefined data bindings radio button.
- Select MQ unstructured binary message, click OK, and then click OK again.
- Right-click on the mediation component, select Add => Interface, select MQBytesInterface, and then click OK.
Your module should now look like this:
Figure 1. MQHeaderModule

Here is an explanation of the MQ data binding that has just been configured. The MQ unstructured binary message (com.ibm.websphere.sca.mq.data.impl.MQDataBindingImplXML) data binding reads and writes the MQ message body as a byte array. It does not attempt to parse the body, and therefore has no understanding of the structure. The data binding requires an input type of JMSBytesBody business object, which contains a single hexbinary value. Do not get confused by the use of JMSBytesBody with an MQ binding, as there is an unstructured binary data binding for JMS, and the JMSBytesBody is just reused from there. You can use this data binding only if no work is to be performed on the message body. The advantage of using the data binding is that you can add and modify message headers without the module having to parse the body of the message, resulting in a performance gain.
Next, create the mediation flow and use the MQ structures API to add an MQ header:
- Double-click on the mediation component MQHeaderModule to generate an implementation.
- In the Mediation Flow Editor, wire operation1 to operation1 to create a flow.
- In the Palette, select Transformation => Custom Mediation.
- Add the custom mediation to the canvas.
- Wire the input node to the input terminal of the custom mediation.
- Wire the output terminal of the custom mediation to the callout node.
Your mediation flow should now look like this:
Figure 2. MQHeaderModule Mediation Flow

- On the Properties panel of the custom mediation, select the Details tab.
- Click the Java radio button.
- Replace the content with the code in Listing 1:
Listing 1. Custom Mediation Code for Adding MQ Header
//Retrieve the MQ header
MQHeaderType mqHeader = smo.getHeaders().getMQHeader();
//If there is no MQHeader
if (mqHeader == null) {
//Create the SMO MQHeaderType. This contains all the MQ headers
mqHeader = ServiceMessageObjectFactory.eINSTANCE.createMQHeaderType();
//Set the MQHeader in the SMO
smo.getHeaders().setMQHeader(mqHeader);
}
//Retrieve the MQMD from the MQHeader
MQMD mqmd = smo.getHeaders().getMQHeader().getMd();
//If there is no MQMD
if (mqmd == null) {
//Create a new MQMD structure
mqmd = WMQStructuresFactory.eINSTANCE.createMQMD();
//Set it in the MQHeader
smo.getHeaders().getMQHeader().setMd(mqmd);
}
mqmd.setExpiry(300000);
mqmd.setMsgFlags(MQC.MQMF_SEGMENTATION_INHIBITED);
mqmd.setPersistence(MQC.MQPER_PERSISTENT);
mqmd.setPriority(8);
//Retrieve the MQControl from the MQHeader
MQControl mqControl = smo.getHeaders().getMQHeader().getControl();
//If there is no control
if (mqControl == null) {
//Create the MQControl structure. This describes the format of the message body
mqControl = WMQStructuresFactory.eINSTANCE.createMQControl();
//Set it in the MQHeader
smo.getHeaders().getMQHeader().setControl(mqControl);
}
//Set CCSID to UTF-8
mqControl.setCodedCharSetId(1208);
//Set encoding to big endian
mqControl.setEncoding(MQC.MQENC_INTEGER_NORMAL |
MQC.MQENC_DECIMAL_NORMAL |
MQC.MQENC_FLOAT_IEEE_NORMAL);
//Set format to none
mqControl.setFormat(MQC.MQFMT_NONE);
out.fire(smo);
|
- Select the Java Imports tab, enter the content of Listing 2, and save the mediation component:
Listing 2. Custom Mediation Imports
import com.ibm.websphere.sibx.smobo.MQHeaderType; import com.ibm.websphere.sca.mq.structures.MQMD; import com.ibm.websphere.sca.mq.structures.MQControl; import com.ibm.websphere.sca.mq.structures.MQHeaders; import com.ibm.websphere.sca.mq.structures.WMQStructuresFactory; import com.ibm.websphere.sibx.smobo.ServiceMessageObjectFactory; import com.ibm.mq.MQC; |
The diagram of the MQHeader SMO structure shown below in Figure 3 helps explain the code shown above in Listing 1. The top-level type MQHeaderType is an SMO element, so it is created using the ServiceMessageObjectFactory. This type contains a single MQMD, an MQControl type, and a MQChainedHeaderType. The MQMD and MQControl are MQ structure types, so you create them using the WMQStructuresFactory:
- MQMD describes the MQ message descriptor that will be added to the message.
- MQControl describes the formatting used for the message body.
The MQChainedHeaderType is an SMO element that allows additional MQ headers to be added to the message. It isn't used in this article, but Part 3 uses it to add a custom header to an MQ message.
Figure 3. MQHeader Structure

When creating an MQMD and setting its fields, keep in mind:
- For information on valid field values, see Using Java in the WebSphere MQ Information Center.
- Any fields that are not set will automatically be set to the default value for that field.
- Some fields will be overwritten when the message is sent to MQ, including:
- MsgType
- MsgID
- BackoutCount
- ReplyToQ
- ReplyToQMgr
- UserIdentifier
- AccountingToken
- ApplIdentityData
- PutApplType
- PutApplName
- PutDate
- PutTime
- ApplOriginData
- GroupId
- MsgSeqNumber
- Offset
- OriginalLength
Use the universal test client to test the module:
- Right-click on the MQHeaderModule mediation component and select Test component.
- The body of the message is not important, so click Continue.
- In the Deployment Location window, select WebSphere ESB Server and click Finish.
The server will start and the application will be deployed and invoked. After the component test is complete, you will see the invoke returned, as shown below:
Figure 4. Invoke Returned

- Using WebSphere MQ Explorer, Right-click on the Send queue and select Browse Messages.
- Double-click on the message and you will see the properties set in the mediation component, as shown in Figure 5:
Figure 5. MQMessage

Congratulations, you have successfully created a mediation module that adds a MQHeader to a message.
| Description | Name | Size | Download method |
|---|---|---|---|
| Project Interchange containing MQHeaderModulePI | MQHeaderModulePI.zip | 14KB | HTTP |
Information about download methods
- Getting started with WebSphere Enterprise Service Bus and WebSphere Integration Developer
This article introduces developers to the IBM WebSphere Enterprise Service Bus server and its accompanying tooling, WebSphere Integration Developer. - Developing custom mediations for WebSphere Enterprise Service Bus
This article introduces the use of custom mediations using the WebSphere Integration Developer V6 environment for WebSphere Enterprise Service Bus V6. - Invoke Web Services with WebSphere MQ and WebSphere ESB
This tutorial introduces the WebSphere ESB MQ bindings and uses a custom body data binding to read and write MQ messages. - Invoking a Web Service using a JMS client
This tutorial introduces the WebSphere ESB JMS bindings and shows how a JMS message can be used to invoke a Web Service. - WebSphere ESB developer resources page
Technical resources to help you use WebSphere ESB as a flexible connectivity infrastructure for integrating applications and services to support an SOA. - WebSphere ESB product page
Product descriptions, product news, training information, support information, and more. - WebSphere ESB information center
A single Web portal to all WebSphere ESB documentation, with conceptual, task, and reference information on installing, configuring, and using WebSphere ESB. - WebSphere ESB documentation library
WebSphere ESB product manuals. - WebSphere ESB FAQs
Basic questions and answers about the new WebSphere ESB product and its relationship to other WebSphere products. - WebSphere ESB support
A searchable database of support problems and their solutions, plus downloads, fixes, problem tracking, and more. - Redbook: Patterns: SOA Design Using WebSphere Message Broker and WebSphere ESB
Patterns for e-business are a group of proven, reusable assets that can be used to increase the speed of developing and deploying e-business applications. This Redbook shows you how to use WebSphere ESB together with WebSphere Message Broker to implement an ESB within an SOA. Includes scenario to demonstrate design, development, and deployment. - WebSphere Integration Developer developer resources page
Technical resources to help you use the WebSphere Integration Developer IDE to render your existing IT assets as service components, encouraging reuse and efficiency as you build SOA-based integration solutions across WebSphere Process Server, WebSphere ESB, and WebSphere Adapters. - WebSphere Integration Developer product page
Product descriptions, product news, training information, support information, and more. - WebSphere Integration Developer information center
A single Web portal to all WebSphere Integration Developer documentation, with conceptual, task, and reference information on installing, configuring, and using your WebSphere Integration Developer environment. - WebSphere Integration Developer information roadmap
Roadmap of articles and resources to help you with installation, migration, administration, development, troubleshooting, and understanding the underlying technology. - WebSphere Integration Developer documentation library
WebSphere Integration Developer product manuals. - WebSphere Integration Developer support
A searchable database of support problems and their solutions, plus downloads, fixes, problem tracking, and more. - WebSphere MQ developer resources page
Technical resources to help you design, develop, and deploy messaging middleware with WebSphere MQ to integrate applications, Web services, and transactions on almost any platform. - WebSphere MQ product page
Product descriptions, product news, training information, support information, and more. - WebSphere MQ V7 trial download
A no-charge trial download of WebSphere MQ V6. Includes limited online support for Windows® and Linux® installations at no charge during the trial period. - WebSphere MQ V6 information center
A single Web portal to all WebSphere MQ V6 documentation, with conceptual, task, and reference information on installing, configuring, and using your WebSphere MQ environment. - WebSphere MQ documentation library
WebSphere MQ product manuals. - WebSphere MQ support page
A searchable database of support problems and their solutions, plus downloads, fixes, problem tracking, and more. - WebSphere MQ public newsgroup
A non-IBM forum where you can get answers to your WebSphere MQ technical questions and share your WebSphere MQ knowledge with other users. - WebSphere MQ SupportPacs
Downloadable code, documentation, and performance reports for the WebSphere MQ family of products. - WebSphere SOA solutions developer resources page
Get technical resources for WebSphere SOA solutions. - developerWorks SOA and Web services zone
Technical resources for evaluating, planning, designing, and implementing solutions that involve SOA and Web services. - developerWorks WebSphere Business Integration zone
For developers, access to WebSphere Business Integration how-to articles, downloads, tutorials, education, product info, and more. - WebSphere Business Integration products page
For both business and technical users, a handy overview of all WebSphere Business Integration products - WebSphere forums
Product-specific forums where you can get answers to your technical questions and share your expertise with other WebSphere users. - Most popular WebSphere trial downloads
No-charge trial downloads for key WebSphere products. - Trial downloads for IBM software products
No-charge trial downloads for selected IBM® DB2®, Lotus®, Rational®, Tivoli®, and WebSphere® products. - Technical books from IBM Press
Convenient online ordering through Barnes & Noble. - developerWorks technical events and Webcasts
Free technical sessions by IBM experts that can accelerate your learning curve and help you succeed in your most difficult software projects. Sessions range from one-hour Webcasts to half-day and full-day live sessions in cities worldwide.

Philip Norton is a software engineer at IBM Hursley Lab. He works on the WebSphere ESB Development team. His expertise includes Java and JMS for Websphere MQ. He is a certified Java Programmer and he has a degree in Computer Science from Canterbury University.

Alex Wood works at IBM Hursley Lab in England as a software developer for the IBM WebSphere Business Integration suite of products. He has extensive experience in development on many of the WebSphere products, including WebSphere MQ, WebSphere Message Broker, WebSphere Enterprise Service Bus, and WebSphere Process Server. He received a BSc in Physics with Astrophysics from Birmingham University in the UK in 1998.





