Creating a new message by using a JavaCompute node
Many message transformation scenarios require a new outgoing message to be built. The Create Message Class template in the JavaCompute node wizard generates template code for this.
About this task
In
the template code, the default constructor of MbMessage is called
to create a blank message, as shown in the following Java™ code:
MbMessage outMessage = new MbMessage();
The
headers can be copied from the incoming message by using the supplied
utility method, copyMessageHeaders(), as shown in this Java code:copyMessageHeaders(inMessage, outMessage);
The
new message body can now be created. First, add the top level parser
element. For XML, this is: MbElement outRoot = outMessage.getRootElement();
MbElement outBody = outRoot.createElementAsLastChild(MbXMLNSC.PARSER_NAME);
The
remainder of the message can then be built up by using the createElement methods
and the extended syntax of the integration node XPath implementation. When
you want to create a BLOB message, that is handled as a single byte
string by using the BLOB parser domain. The message data is
added as a byte array to the single element named "BLOB" under
the parser level element, described as follows:
String myMsg = "The Message Data";
MbElement outRoot = outMessage.getRootElement();
// Create the Integration Bus Blob Parser element
MbElement outParser = outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
// Create the BLOB element in the Blob parser domain with the required text
MbElement outBody = outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", myMsg.getBytes());
You can use the following code to create a BLOB message
in a JavaCompute node:
MbElement outMsgRootEl = outMessage.getRootElement();
String parserName = MbBLOB.PARSER_NAME;
String messageType = "";
String messageSet = "";
String messageFormat = "";
int encoding = 0;
int ccsid = 0;
int options = 0;
outMsgRootEl.createElementAsLastChildFromBitstream(responseBodyXmlData,
parserName, messageType, messageSet, messageFormat, encoding, ccsid,
options);
To add a JSON message in a JavaCompute node, see Creating a JSON message.