Accessing message content in IIB using JAVA

J. Snipe :

Does anyone know how to access the content of MbMessage in a Java compute node? I am using IBM integration Toolkit 10 and so far I only understand that Message consists of a root element with various children.

I am receiving JSON text from MQ and I just simply need to access it in compute node, modify it and send it to Azure service bus, yet I struggle to access the JSON text from the message.

I tried to access various MbMessage elements but didn't get any reasonable value from them. I throw the output as an exception into error log. The only reasonable data I got was from Chapter element, but the format was hexadecimal I guess. See this:

"7b224865616422203a207b22536f7572636553797374656d223a224d565844445441505244222c2022546172676574223a2241677265656d656e74222c2022416374696f6e223a2264656c657465222c20224d6573736167654964223a22222c2022436f72656c6174696f6e4964223a22222c2254696d657374616d70223a223230313930333230313631383531333636373230303030303030222c20224368616e67654964223a22227d2c224974656d7322203a207b2241677265656d656e744964223a225357453134305f36303736343336222c224d616368696e654b6579223a2231313131303337313333222c2241677265656d656e74436c617373223a22353631222c2241677265656d656e744e756d626572223a2242303638323135222c224c696e654e756d626572223a22315f3330222c22436f6e74726163746564486f757273223a22313030222c2241677265656d656e7456616c696446726f6d223a223230313930333132222c2241677265656d656e7456616c6964546f223a223230323230333131222c224c6173744d6f646966696564223a223230313930333230313631383531333636373230303030303030222c22497349536974654f726967696e61746564223a2230222c22497344656c657"

// This is how i see in log what is in the element value of a chapter
if(true){
    throw new Exception("Message Content: " + outMessage.getRootElement().getLastChild().getLastChild().getValueAsString());
}

I expect to get the JSON text I am sending to the message queue.

TJA :

So I suspect the preceding input node, or your code for that matter, has specified the BLOB parser rather than the JSON parser. BTW if you haven't supplied the name of a parser the BLOB parser is the default.

Feed part of the BLOB

7b224865616422203a207b22536f7572636553797374656d223a224d565844445441505244222c2022546172676574223a2241677265656d656e74222c20 

Into a Hex To ASCII converter https://www.rapidtables.com/convert/number/hex-to-ascii.html and you get

 {"Head" : {"SourceSystem":"MVXDDTAPRD", "Target":"Agreement",   

Which essentially tells you that your message has not been parsed.

I've included a couple of screen shots for an HTTPInput node, you will generally find the Input/Output Messaging Parsing tab in the Properties editor of Input and Output nodes.

BLOB Parser Selected

JSON Parser Selected

Once you've got the parser issue sorted out then you can start walking the input tree and building your output tree. The diagram below comes from this link https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ac69091_.htm and shows how the element tree is structured. Note that in your case the XML node will in fact be JSON

Traversing the Element Tree

If you know the names you can get an element reference by using XPath.

MbElement inputRoot = inputMessageAssembly.getMessage().getRootElement();
MbElement srcSys = inputRoot.getFirstElementByPath("/JSON/Data/Head/SourceSystem");

To tell IIB that the output message should be in JSON you'll need to supply it with the name of the JSON parser like so.

MbMessage outMessage = new MbMessage();
MbElement outRoot = outMessage.getRootElement();
MbElement outBody = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);

Last but not least your current line of code is now likely to display an empty string or null as the element you are navigating to has no value just a name.

References: A search for MbElement gets you https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.plugin.doc/com/ibm/broker/plugin/MbElement.html

Searching for Java Code Examples MbElement will get you this link which shows how to use the MbElement class. https://www.programcreek.com/java-api-examples/?api=com.ibm.broker.plugin.MbElement

If you want to see the structure of the element trees, there are several ${Root} ${Environment} ${LocalEnvironment} ${ExceptionList}, then add Trace nodes. You can put them pretty much anywhere you like. The screenshots below are from another answer but illustrate their use nicely. Be aware that the output file is held open by the Trace node, the easiest way to flush the buffers is to stop and restart your flow, redeploying also works.

Simple Soap Flow

Trace Node Configuration

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=115937&siteId=1