Thursday 31 May 2018

Sample Java compute node(JCN) code for beginners

Hi Folks,

I'm posting this post for only freshers on IIB, who are going to use JCN node for first time.

1.First will look how to extract the values from InputTree and will create Output tree for the same.
2.Iterating message in loop.
3.Creating message tree with different parser.
4.Configuring and connecting DB from JCN.

Ex:
1.1.First will look how to extract the values from InputTree and will create Output tree for the same.

Input Mesage :

<Employee>
<Name>Venkat</Name>
<LName>Rathod</LName>
<Company>Cognizant</Company>
<Designation>TeamLead</Designation>
</Employee>

Expected output:

<Response>
<EmployeeDetails>
<EmployeName>Venkat</EmployeName>
<EmployeeLatName>Rathod</EmployeeLatName>
<EmployeComapny>Cognizant</EmployeComapny>
<EmployeDesignation>TeamLead</EmployeDesignation>
</EmployeeDetails>
</Response>

Code :

Steps:
1. use the Default code which IIB provide when you Initially open JCN node.
2.will add our code in the below block
          // Add user code below
 --Here we add our code
           // End of user code
3. Source Code.

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.plugin.MbXMLNSC;


public class POC1_MF_JavaCompute extends MbJavaComputeNode {

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

MbMessage inMessage = inAssembly.getMessage();
MbMessageAssembly outAssembly = null;
try {
// create new message as a copy of the input
MbMessage outMessage = new MbMessage(inMessage);
outAssembly = new MbMessageAssembly(inAssembly, outMessage);
// ----------------------------------------------------------
// Add user code below

-- First Get the values from Input Tree
MbElement oGetRootMessage = inAssembly.getMessage().getRootElement();

MbElement Name = oGetRootMessage.getFirstElementByPath("XMLNSC/Employee/Name");
MbElement Lname = oGetRootMessage.getFirstElementByPath("XMLNSC/Employee/LName");
MbElement Company = oGetRootMessage.getFirstElementByPath("XMLNSC/Employee/Company");
MbElement Desgination = oGetRootMessage.getFirstElementByPath("XMLNSC/Employee/Designation");

-- End Of First Get the values from Input Tree

--Create Output Root for the output message
MbElement oResponseRoot = outAssembly.getMessage().getRootElement().getLastChild();
MbElement oResponse = oResponseRoot.createElementAsLastChild(MbElement.TYPE_NAME, "Response",null );
MbElement oResponseDetails = oResponse.createElementAsLastChild(MbElement.TYPE_NAME, "EmployeeDetails",null );
-- End OF Create Output Root for the output message

--map the values to output root
MbElement EmpName = oResponseDetails.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"EmployeName",Name.getValue());
MbElement EmpLastName = oResponseDetails.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"EmployeeLatName",Lname.getValue().toString());
MbElement EmpComapnay = oResponseDetails.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"EmployeComapny",Company.getValue());
MbElement EmpDesignation = oResponseDetails.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"EmployeDesignation",Desgination.getValue());

-- End of Map the values to output root

oResponseRoot.getFirstChild().detach();
// End of user code
// ----------------------------------------------------------
} catch (MbException e) {
// Re-throw to allow Broker handling of MbException
throw e;
} catch (RuntimeException e) {
// Re-throw to allow Broker handling of RuntimeException
throw e;
} catch (Exception e) {
// Consider replacing Exception with type(s) thrown by user code
// Example handling ensures all exceptions are re-thrown to be handled in the flow
throw new MbUserException(this, "evaluate()", "", "", e.toString(),
null);
}
// The following should only be changed
// if not propagating message to the 'out' terminal
out.propagate(outAssembly);

}

}


ESQL date sorting logic : -

--Get all the dates from Inputmessage
DECLARE VesselDetailsRef REFERENCE TO InputRoot.JSON.Data.VesselSchedule.VesselDetails.PortDetails.Item[1];
CREATE LASTCHILD OF Environment DOMAIN('JSON')  NAME 'VesselDetails';
DECLARE i INTEGER 1;

WHILE LASTMOVE(VesselDetailsRef) DO
SET Environment.VesselDetails.PortDetails.Provider[i].datetimeBegin = VesselDetailsRef.EventDetails.EventDate;
SET Environment.VesselDetails.PortDetails.Provider[i].PortDetails = VesselDetailsRef;

MOVE VesselDetailsRef NEXTSIBLING REPEAT TYPE NAME;
SET i = i + 1;
END WHILE;


--Sort the date in Ascending Order
DECLARE rEnv REFERENCE TO Environment.VesselDetails.PortDetails;
SET rEnv.LastProvEffDate = rEnv.Provider[1].datetimeBegin;
DECLARE k INTEGER 1;
FOR rList AS rEnv.Provider[] DO
IF(rEnv.LastProvEffDate > rList.datetimeBegin) THEN
SET rEnv.TempProvider = rEnv.Provider[k-1];
SET rEnv.Provider[k-1] = rList;
SET rEnv.Provider[k] = rEnv.TempProvider ;
END IF;
SET rEnv.LastProvEffDate = rList.datetimeBegin;
SET k =k+1;
END FOR;
DELETE FIELD rEnv.TempProvider;


-- Search the next date where Unloading is happening

DECLARE rNextDate REFERENCE TO Environment.VesselDetails.PortDetails.Provider[1];
WHILE LASTMOVE(rNextDate) DO
DECLARE PortFunctionCode CHARACTER rNextDate.PortDetails.PortFunctionCode;

--Check the current Protcode is doing Loading
IF (PortFunctionCode ='L') THEN
--Taking Date where Loading is happening
DECLARE LoadDate DATE CAST(rNextDate.PortDetails.EventDetails.EventDate AS DATE);
DECLARE rNextDateFinder REFERENCE TO Environment.VesselDetails.PortDetails.Provider[1];

WHILE LASTMOVE(rNextDateFinder) DO
DECLARE NextDate DATE CAST(rNextDateFinder.PortDetails.EventDetails.EventDate AS DATE);
DECLARE cUnloadingFlag CHARACTER rNextDateFinder.PortDetails.PortFunctionCode;

IF (LoadDate < NextDate) AND (( cUnloadingFlag = 'I') OR ( cUnloadingFlag = 'D')) THEN
DECLARE BackEndCalls CHARACTER;
--Call a Backend
END IF;

MOVE rNextDateFinder NEXTSIBLING REPEAT TYPE NAME;
END WHILE;
END IF;
MOVE rNextDate NEXTSIBLING REPEAT TYPE NAME;

END WHILE;





1 comment: