Friday, 5 December 2014

Simple flow in Message broker using JavaComputeNode

*************************************************************************

Step 1:- Create a Flow using MQInput Node,JavaComputeNode & MQOutput Nodes.
Provide some queue name for -->MQInput Node
Message Domain as                -->XMLNSC

Similarly provide some queue name for MQOutput node as well.

Step 2:- Double click on JavaComputeNode and select "Filter Javaclass".

import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
import com.ibm.broker.plugin.MbUserException;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;

public class SIMPLE_JCN_JavaCompute extends MbJavaComputeNode
     {

public void evaluate(MbMessageAssembly inAssembly) throws MbException
            {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");

//Declaration of Variables
String Ename = null,Location = null,Batch = null,Company = null;

               MbMessage inMessage = inAssembly.getMessage();
               // create new message
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new                     MbMessageAssembly(inAssembly,outMessage);
//-------- INPUT PARSING ------------
MbElement InRoot           = inMessage.getRootElement();                // MESSAGE
MbElement InXmlNsc         = InRoot.getLastChild();                     // XMLNSC
MbElement  InDetails       = InXmlNsc.getLastChild();                   //<DETAILS></DETAILS>
//creating MbElement array to store all employee details
MbElement Emp[]           = InDetails.getAllElementsByPath("*");        // <EMP></EMP>
// ------- CREATING STRUCTURE FOR OUTPUT PARSING --------------
MbElement OutRoot         = outMessage.getRootElement();          // MESSAGE
MbElement OutXmlNsc       = OutRoot.getLastChild();               // XMLNSC
MbElement OutDetails      = OutXmlNsc.createElementAsFirstChild(MbElement.TYPE_NAME,"EMP_DETAILS",null);  //<EMP_DETAILS></EMP_DETAILS>
MbElement OutEmp = OutDetails.createElementAsFirstChild(MbElement.TYPE_NAME,"EMP",null); //<EMP></EMP>

//Extracting child elements from <EMP>

for(int i=0;i<Emp.length;i++){
Ename       = Emp[i].getFirstChild().getValueAsString();
Location    = Emp[i].getFirstChild().getNextSibling().getValueAsString();
Batch       = Emp[i].getFirstChild().getNextSibling().getNextSibling().getValueAsString();
Company     = Emp[i].getLastChild().getValueAsString();
}
//Create Values inside OutEmp

OutEmp.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"EMPNAME",Ename);
OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"MSS_LOCATION",Location);
OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"ITG_BATCH",Batch);
OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"PAR_COMPANY",Company);

//detach XMLNSC last child because outMessage is created from inMessage and already contains InMessage Structure.
OutXmlNsc.getLastChild().detach();
// if not propagating message to the 'out' terminal
out.propagate(outAssembly);
}
}

Step 3:- Test the flow using the Test cases:-

************************************************************************
Input:-
<?xml version="1.0"?>
        <DETAILS>
                <EMP>
                         <ENAME>RAJESH</ENAME>
                         <LOCATION>MHEIGHTS</LOCATION>
                        <BATCH>35</BATCH>
                       <COMPANY>CTS</COMPANY>
              </EMP>
         </DETAILS> 
************************************************************************

Expected Output:-
<?xml version="1.0"?>
        <EMP_DETAILS>
                  <EMP>
                          <EMPNAME>RAJESH</EMPNAME>
                          <MSS_LOCATION>MHEIGHTS</MSS_LOCATION>
                         <ITG_BATCH>35</ITG_BATCH>                                                                                      <PAR_COMPANY>CTS</PAR_COMPANY>
                 </EMP>
       </EMP_DETAILS>

 ************************************************************************