IBM® WebSphere® MQ bindings in IBM WebSphere ESB 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 modify the MQMD. The MQHeaderSetter primitive can be used to create and change MQ headers to a message, but some fields cannot be populated in WebSphere ESB V6.2, including:
- UserIdentifier
- AppIdentityData
- PutApplType
- PutApplName
- ApplOriginData
Although these MQMD fields can be propagated by setting the custom property MDCTX=SET_ALL_CONTEXT on the JNDI queue definition in WebSphere ESB V7 or later, a custom MQ SendExit is introduced in this paper to solve this issue for WebSphere ESB V6.2. The following products are used in this article:
- WebSphere Integration Developer V6.2.0.2
- WebSphere ESB V6.2.0.2 Integrated Test Environment (included with WebSphere Integration Developer)
- WebSphere MQ V6.0.2.9
Populating MQMD using a custom MQ SendExit
A sample project will show you how to write a custom MQ SendExit to populate AppIdentityData in MQMD:
First, create an MQ binding test module:
- Create a new module called MQHeaderTest. Ensure that Create mediation component is checked
- Open the Dependencies on the library, and under Predefined Resources, check Schema for simple JMS Data Bindings, and then click Save. The JMS body types are now available.
- In the module, create a new interface called MQHeaderTestService.
- Create a one-way operation named sendMessage.
- Change the type of input1 to JMSTextBody and click Save to save the interface:
Figure 1. MQHeaderTestService Interface

- Create a business object called MQTestMessage:
Figure 2. MQTestMessage Business Object

- Create another interface called TestRequestService:
Figure 3. TestRequestService Interface

- Right-click the mediation component, select Add: Interface, and select TestRequestService.
- Add an Import to the assembly diagram.
- Wire the mediation component to the Import, and when asked to add a reference, select MQHeaderTestService.
- Right-click Import and select Generate Binding => Messaging Binding => MQ Binding.
- Specify the JNDI name for MQ connection factory and queue, and then click OK:
Figure 4. MQ binding

The assembly diagram is shown in Figure 5:
Figure 5. MQHeaderTest assembly

Next, create the mediation flow to convert the value of appIdentityData and payload in the MQTestMessage business object into a string, whose structure is shown below. The purpose of this operation is to pass the appIdentityData value to the custom MQ SendExit via the MQ message content.
Figure 6. MQTestMessage string structure
- Double-click on the mediation component MQHeaderTest to generate an implementation.
- In the Mediation Flow Editor, wire request to sendMessage to create a flow.
- In the Palette, select Transformation => Business Object Map.
- 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 is shown in Figure 7:
Figure 7. MQHeaderTest mediation flow

- Click BOMapper1 to implement the mapping:
Figure 8. BOMapper1 implementation

- Implement the custom map with the code in Listing 1:
Listing 1. Custom code for BOMapper1//get the length of appIdentityData String appIDLength = StringUtils.getHexString(((String)requestRequestMsg_request_input_appIdentityData) .length()); sendMessageRequestMsg_sendMessage_input1_value = appIDLength + (String)requestRequestMsg_request_input_appIdentityData + (String)requestRequestMsg_request_input_payload;
Building the custom MQ SendExit
Create a new library named MQExitLib and a new Java class named MQExitImp in the com.ibm.test.mqexit package to implement MQ SendExit:
Listing 2. Custom MQ SendExit
package com.ibm.test.mqexit;
import java.io.ByteArrayOutputStream;
import com.ibm.mq.MQC;
import com.ibm.mq.MQChannelDefinition;
import com.ibm.mq.MQChannelExit;
import com.ibm.mq.data.MQDataOutputStream;
import com.ibm.ws.sca.internal.mq.exit.MQInternalSendExitImpl;
public class MQExitImp extends MQInternalSendExitImpl {
public static final int mqQueueType = 131;
public static final int queueControlOffset = 212;
public static final int applIDControlOffset = 416;
public static final int applIDOffset = 284;
public static final int dataLengthOffset = 536;
public static final int dataOffset = 540;
public MQExitImp(String version) {
super("Centrelink MQ SendExit implementation");
}
/**
*
* Data format: AppIDLength(2 chars) + AppID + AppData
*/
public byte[] sendExit(MQChannelExit exit, MQChannelDefinition chl,
byte[] buf) {
byte[] msg = super.sendExit(exit, chl, buf);
if (exit.exitReason == MQChannelExit.MQXR_XMIT) {
//send data
if (byteToInt(buf[controlFlag2]) == mqputRequest) {
int controlFlag = byteToInt(buf[controlFlag3]);
if ((controlFlag == firstSegment)||(controlFlag ==
onlySegment)) {
System.out.println(">>>>> Queue data input = " +
StringUtils.getHexString(msg));
System.out.println(">>>>> remoteUserId = " +
chl.remoteUserId);
//process message data
boolean reversed = (byteToInt(msg[controlFlag1]) ==
mqFapReversed);
int ccsid = readShortInt(msg, mqFapCcsidOffset,
reversed);
int encoding = readInt(msg, mqFapMqEncOffset,
reversed);
//set IDENTITY_CONTEXT
int exitOptions = readInt(msg, applIDControlOffset,
reversed);
System.out.println(">>>>> Original Options = " +
exitOptions);
if ((exitOptions & MQC.MQPMO_PASS_ALL_CONTEXT) ==
MQC.MQPMO_PASS_ALL_CONTEXT) return msg;
ByteArrayOutputStream baos = null;
MQDataOutputStream mqdos = null;
try {
//set AppID
String appIdLenStr = new String(msg,
dataOffset, 2);
int appIDLen = 0;
try {
appIDLen =
Integer.parseInt(appIdLenStr);
} catch (Exception ex) {
System.out.println(">>>>> appIDLen
exception = " + ex.getMessage());
return msg;
}
System.out.println(">>>>> appIDLen = " +
appIDLen);
//set options
baos = new ByteArrayOutputStream();
mqdos = new MQDataOutputStream(baos);
mqdos.setEncoding(encoding);
mqdos.setCCSID(ccsid);
mqdos.writeMQLONG(exitOptions |
MQC.MQPMO_SET_IDENTITY_CONTEXT);
byte[] valBytes = baos.toByteArray();
System.out.println(">>>>> New Options = " +
StringUtils.getHexString(valBytes));
System.arraycopy(valBytes, 0, msg,
applIDControlOffset,
valBytes.length);
//set AppID
String appID = new String(msg, dataOffset+2,
appIDLen);
valBytes = appID.getBytes();
System.out.println(">>>>> AppID = " +
StringUtils.getHexString(valBytes));
System.arraycopy(valBytes, 0, msg,
applIDOffset,
valBytes.length);
//set AppData Length
int dataLength = readInt(msg,
dataLengthOffset, reversed);
System.out.println(">>>>> Original dataLength
= " + dataLength);
baos = new ByteArrayOutputStream();
mqdos = new MQDataOutputStream(baos);
mqdos.setEncoding(encoding);
mqdos.setCCSID(ccsid);
mqdos.writeMQLONG(dataLength - appIDLen -2);
valBytes = baos.toByteArray();
System.out.println(">>>>> New dataLength = "
+ StringUtils.getHexString(valBytes));
System.arraycopy(valBytes, 0, msg,
dataLengthOffset,
valBytes.length);
//set AppData
byte[] newMsg = new byte[msg.length -
appIDLen -2];
//copy header
System.arraycopy(msg, 0, newMsg,
0,
dataOffset);
//copy data
System.arraycopy(msg, dataOffset + appIDLen +
2, newMsg,
dataOffset,
dataLength - appIDLen -2);
System.out.println(">>>>> Queue data output =
" + StringUtils.getHexString(newMsg));
return newMsg;
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (mqdos != null) {
try {
mqdos.close();
} catch(Exception e1) {}
}
if (baos != null) {
try {
baos.close();
} catch(Exception e1) {}
}
}
}
} else if (byteToInt(buf[controlFlag2]) == mqQueueType) {
System.out.println(">>>>> Queue connection input = " +
StringUtils.getHexString(msg));
System.out.println(">>>>> remoteUserId = " +
chl.remoteUserId);
//process queue control
boolean reversed = (byteToInt(msg[controlFlag1]) ==
mqFapReversed);
int ccsid = readShortInt(msg, mqFapCcsidOffset,
reversed);
int encoding = readInt(msg, mqFapMqEncOffset,
reversed);
int exitOptions = readInt(msg, queueControlOffset,
reversed);
System.out.println(">>>>> Original Options = " +
exitOptions);
if ((exitOptions & MQC.MQOO_PASS_ALL_CONTEXT) ==
MQC.MQOO_PASS_ALL_CONTEXT) return msg;
if ((exitOptions & MQC.MQOO_OUTPUT) == MQC.MQOO_OUTPUT) {
ByteArrayOutputStream baos = null;
MQDataOutputStream mqdos = null;
try {
baos = new ByteArrayOutputStream();
mqdos = new MQDataOutputStream(baos);
mqdos.setEncoding(encoding);
mqdos.setCCSID(ccsid);
mqdos.writeMQLONG(exitOptions |
MQC.MQOO_SET_IDENTITY_CONTEXT);
byte[] valBytes = baos.toByteArray();
System.out.println(">>>>> New Options = " +
StringUtils.getHexString(valBytes));
System.arraycopy(valBytes, 0, msg,
queueControlOffset,
valBytes.length);
System.out.println(">>>>> Queue connection
output = " + StringUtils.getHexString(msg));
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (mqdos != null) {
try {
mqdos.close();
} catch(Exception e1) {}
}
if (baos != null) {
try {
baos.close();
} catch(Exception e1) {}
}
}
}
}
}
return msg;
}
}
|
The custom SendExit class extends com.ibm.ws.sca.internal.mq.exit.MQInternalSendExitImpl. Change two cases to change the MQ message buffer:
- If the message type is mqQueue, set MQOO_SET_IDENTITY_CONTEXT for the queue options.
- If the message type is mqputRequest, set MQPMO_SET_IDENTITY_CONTEXT will be set for the message options. The appIdentityData value will be retrieved from the message content and populated in the message buffer. The appIdentityData value will be removed from the message content data.
Deploying the custom MQ SendExit
Installing the SendExit JAR file
- Export the Java project as a JAR file.
- Copy the JAR file into the directory <WESB_HOME>/lib/ext, where WESB_HOME is the installation directory of WebSphere ESB.
Configuring the MQ Queue Connection Factory
- Open the WebSphere Process Server admin console.
- Navigate to Resources => JMS Providers => WebSphere MQ => WebSphere MQ queue connection factories => MQHeaderTest_QCF => Custom properties.
- Select the existing property SENDEXIT, and replace the current value with com.ibm.test.mqexit.MQExitImp:
Figure 9. MQ Queue Connection Factory custom properties

Use the universal test client to test the module:
- Right-click on the MQHeaderTest 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 response:
Figure 10. Test component

- Using WebSphere MQ Explorer, right-click on the Send queue and select Browse Messages.
- Double-click on the message, and you will see the application identity data set in the mediation component:
Figure 11. MQMessage

Here is the trace log:
Listing 3. Trace log
[15/07/10 15:47:05:110 EST] 00000047 SystemOut O >>>>> Queue connection input = 54534820000000D80183300000000000000000000000011103520000000000D8000000000000000000000000 4F44202000000001000000052020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020414D512E2A20202020202020202020202020202020202020 20202020202020202020202020202020202020202020202020202020202020202020202000000020 [15/07/10 15:47:05:110 EST] 00000047 SystemOut O >>>>> remoteUserId = Administrator [15/07/10 15:47:05:110 EST] 00000047 SystemOut O >>>>> Original Options = 32 [15/07/10 15:47:05:515 EST] 00000047 SystemOut O >>>>> Queue connection input = 54534820000000D80183300000000000000000000000011103520000000000D8000000000000000000000000 4F44202000000001000000052020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020414D512E2A20202020202020202020202020202020202020 20202020202020202020202020202020202020202020202020202020202020202020202000000020 [15/07/10 15:47:05:515 EST] 00000047 SystemOut O >>>>> remoteUserId = Administrator [15/07/10 15:47:05:515 EST] 00000047 SystemOut O >>>>> Original Options = 32 [15/07/10 15:47:05:671 EST] 00000047 SystemOut O >>>>> Queue connection input = 54534820000000D80183300000000000000000000000011103520000000000D8000000000000000000000000 4F4420200000000100000001736D62496E202020202020202020202020202020202020202020202020202020 20202020202020202020202020202020514D5F69626D5F6C3374303534382020202020202020202020202020 2020202020202020202020202020202020202020414D512E2A20202020202020202020202020202020202020 20202020202020202020202020202020202020202020202020202020202020202020202000002010 [15/07/10 15:47:05:671 EST] 00000047 SystemOut O >>>>> remoteUserId = Administrator [15/07/10 15:47:05:671 EST] 00000047 SystemOut O >>>>> Original Options = 8208 [15/07/10 15:47:05:718 EST] 00000047 SystemOut O >>>>> New Options = 00002410 [15/07/10 15:47:05:718 EST] 00000047 SystemOut O >>>>> Queue connection output = 54534820000000D80183300000000000000000000000011103520000000000D8000000000000000000000000 4F4420200000000100000001736D62496E202020202020202020202020202020202020202020202020202020 20202020202020202020202020202020514D5F69626D5F6C3374303534382020202020202020202020202020 2020202020202020202020202020202020202020414D512E2A20202020202020202020202020202020202020 20202020202020202020202020202020202020202020202020202020202020202020202000002410 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> Queue data input = 545348200000023901863000000000000000000000000111035200000000023900000000000000000071FA40 4D442020000000020000000000000008FFFFFFFF0000000000000111000004B82020202020202020FFFFFFFF 0000000200000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000002020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020000000000000000000000000 0000000000000000000000000000000000000000202020202020202020202020202020202020202020202020 2020202020202020000000002020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202000000000000000000000000000000000000000000000000000000001 0000000000000000FFFFFFFF504D4F200000000100002002FFFFFFFF00000000000000000000000000000001 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 20202020202020200000001D3036546573744944546869732069732061207465737420737472696E67 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> remoteUserId = Administrator [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> Original Options = 8194 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> appIDLen = 6 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> New Options = 00002402 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> AppID = 546573744944 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> Original dataLength = 29 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> New dataLength = 00000015 [15/07/10 15:47:05:873 EST] 00000047 SystemOut O >>>>> Queue data output = 54534820000002390186300000000000000000000000011103520000000002390000 0000000000000071FA404D442020000000020000000000000008FFFFFFFF0000000000000111000004B82020 202020202020FFFFFFFF00000002000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020200000 0000000000000000000000000000000000000000000000000000000000005465737449442020202020202020 2020202020202020202020202020202020200000000020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020000000000000000000000000000000000000 000000000000000000010000000000000000FFFFFFFF504D4F200000000100002402FFFFFFFF000000000000 0000000000000000000120202020202020202020202020202020202020202020202020202020202020202020 2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 20202020202020202020202020202020202000000015546869732069732061207465737420737472696E67 |
This article has explained how to use a custom MQ SendExit to populate MQ headers required by native MQ applications
| Description | Name | Size | Download method |
|---|---|---|---|
| Code sample | MQHeaderTestPI.zip | 32 KB | HTTP |
Information about download methods
- WebSphere ESB resources
- 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 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 Development Guide
The Development Guide manual in PDF format. - WebSphere ESB product page
Product descriptions, product news, training information, support information, and more. - WebSphere ESB documentation library
WebSphere ESB product manuals. - WebSphere ESB FAQs
Common questions about WebSphere ESB and its relationship to other ESB products. - WebSphere ESB support
A searchable database of support problems and their solutions, plus downloads, fixes, and problem tracking. - Getting started with WebSphere ESB and WebSphere Integration Developer
This article introduces developers to the IBM WebSphere Enterprise Service Bus server and its accompanying tooling, WebSphere Integration Developer.
- WebSphere ESB information center
- WebSphere Integration Developer resources
- 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 WebSphere Integration Developer. - 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 documentation library
WebSphere Integration Developer product manuals. - WebSphere Integration Developer support
A searchable database of support problems and their solutions, plus downloads, fixes, and problem tracking.
- WebSphere Integration Developer information center
- WebSphere MQ resources
- WebSphere MQ V7 information center
A single Web portal to all WebSphere MQ V7 documentation, with conceptual, task, and reference information on installing, configuring, and using WebSphere MQ V7. - 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, trial download, and more. - WebSphere MQ product family
A description of the ten or so different editions of WebSphere MQ. - WebSphere MQ documentation library
WebSphere MQ information centers and product manuals. - IBM Redbook: WebSphere MQ V7 features and enhancements
Describes the fundamental concepts and benefits of message queuing technology, describes the new features in V7, and provides a business scenario that shows those features in action. - Download a free trial version of WebSphere MQ V7
A 90-day, full featured, no-charge trial of WebSphere MQ V7 - WebSphere MQ support page
A searchable database of support problems and their solutions, plus downloads, fixes, and problem tracking. - WebSphere MQ forum
Get answers to your WebSphere MQ technical questions and share your knowledge with other users. - WebSphere MQ SupportPacs
Downloadable code, documentation, and performance reports for the WebSphere MQ family of products.
- WebSphere MQ V7 information center
- WebSphere resources
- developerWorks WebSphere developer resources
Technical information and resources for developers who use WebSphere products. developerWorks WebSphere provides product downloads, how-to information, support resources, and a free technical library of more than 2000 technical articles, tutorials, best practices, IBM Redbooks, and online product manuals. - developerWorks WebSphere application integration developer resources
How-to articles, downloads, tutorials, education, product info, and other resources to help you build WebSphere application integration and business integration solutions. - developerWorks WebSphere business process management developer resources
WebSphere BPM how-to articles, downloads, tutorials, education, product info, and other resources to help you model, assemble, deploy, and manage business processes. - Most popular WebSphere trial downloads
No-charge trial downloads for key WebSphere products. - WebSphere forums
Product-specific forums where you can get answers to your technical questions and share your expertise with other WebSphere users. - WebSphere on-demand demos
Download and watch these self-running demos, and learn how WebSphere products and technologies can help your company respond to the rapidly changing and increasingly complex business environment. - developerWorks WebSphere weekly newsletter
The developerWorks newsletter gives you the latest articles and information only on those topics that interest you. In addition to WebSphere, you can select from Java, Linux, Open source, Rational, SOA, Web services, and other topics. Subscribe now and design your custom mailing. - WebSphere-related books from IBM Press
Convenient online ordering through Barnes & Noble. - WebSphere-related events
Conferences, trade shows, Webcasts, and other events around the world of interest to WebSphere developers.
- developerWorks WebSphere developer resources
- developerWorks resources
- Trial downloads for IBM software products
No-charge trial downloads for selected IBM® DB2®, Lotus®, Rational®, Tivoli®, and WebSphere® products. - developerWorks blogs
Join a conversation with developerWorks users and authors, and IBM editors and developers. - developerWorks cloud computing resources
Access the IBM or Amazon EC2 cloud, test an IBM cloud computing product in a sandbox, see demos of cloud computing products and services, read cloud articles, and access other cloud resources. - developerWorks tech briefings
Free technical sessions by IBM experts to accelerate your learning curve and help you succeed in your most challenging software projects. Sessions range from one-hour virtual briefings to half-day and full-day live sessions in cities worldwide. - developerWorks podcasts
Listen to interesting and offbeat interviews and discussions with software innovators. - developerWorks on Twitter
Check out recent Twitter messages and URLs. - IBM Education Assistant
A collection of multimedia educational modules that will help you better understand IBM software products and use them more effectively to meet your business requirements.
- Trial downloads for IBM software products

Jun Shen is a Senior Consultant on the WebSphere Lab Services team. He specializes in WebSphere Process Server and SOA implementations, and has 10 of experience in the IT industry in China and Australia. He received a Ph.D. in Computer Science from Tsinghua University in China in 1999. You can contact Jun at <a href="mailto:junshen@au1.ibm.com">junshen@au1.ibm.com</a>.




