Showing posts with label JCN. Show all posts
Showing posts with label JCN. Show all posts

Monday, 3 October 2016

Backup Of Input Message

Step1:
Create a Message flow like below
MQInputNode---->JavaComputeNode----->MQOutputNode


Step2:
Write the below code in JCN

//Make a modifiable MbMessage based on the input message passed in on the inAssembly.

MbMessage InMessage = new MbMessage(inAssembly.getMessage());

//Now partially parse the input message one record at a time.
MbElement inputRootElement = InMessage.getRootElement();//root
MbElement inputPropertiesElement = inputRootElement.getFirstElementByPath("Properties");//properties
MbElement inputMQMDElement = inputRootElement.getFirstElementByPath("MQMD");//mqmd
MbElement inputXMLNSCElement = inputRootElement.getFirstElementByPath("XMLNSC");//xmlnsc

MbElement inputXMLNSCRootTagElement = inputXMLNSCElement.getFirstChild(); 
//Child(Root tag of Input

 // Create a new output message for the record that we are going to be propagate.
 MbMessage outputMessage = new MbMessage();
outAssembly = new MbMessageAssembly(inAssembly, outputMessage);

//Create new parsers folders in the output message.
 MbElement outputRootElement = outputMessage.getRootElement();//root
 MbElement outputPropertiesElement = outputRootElement.createElementAsLastChild(inputPropertiesElement.getParserClassName());//properties
MbElement outputMQMDElement = outputRootElement.createElementAsLastChild(inputMQMDElement.getParserClassName());//mqmd
MbElement outputXMLNSCElement = outputRootElement.createElementAsLastChild(inputXMLNSCElement.getParserClassName());   //xmlnsc
MbElement BACKUP=outputMessage.getRootElement().createElementAsLastChild(MbElement.TYPE_NAME, "BackUp", null);//New tag in xmlnsc
 
//Create the root tag in the output XMLNSC folder that will be used for this output record.
//  MbElement outputXMLNSCRootTag = inputXMLNSCRootTagElement.createElementAsLastChild(MbElement.TYPE_NAME, "BackUp", null);

//MbElement Payload = outputMessage.getRootElement().getLastChild();
// outputXMLNSCRootTag.addAsFirstChild(outputXMLNSCElement.copy());
 
//Copy the Properties Folder, MQMD header, and the current record.
 outputPropertiesElement.copyElementTree(inputPropertiesElement);//Properties
 
outputMQMDElement.copyElementTree(inputMQMDElement);//mqmd
 
 outputXMLNSCElement.copyElementTree(inputXMLNSCElement); //xmlnsc
 
 BACKUP.copyElementTree(inputXMLNSCElement);//backup
//  MbElement Payload = outputMessage.getRootElement().getLastChild();
// outputRootElement.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BackUp",Payload.copy().getValue());  
 
//Propagate this message, requesting that the output message assembly be cleared after propagation.
out.propagate(outAssembly);

Step3:-
Place the below test Message
<Details><Summary><Name>Bhanu</Name><Designation>Technical Lead</Designation><Domain>EAI</Domain><Location>Vishakapatnam</Location></Summary></Details>

Observe the Obtained Output as below

Wednesday, 18 June 2014

Accessing Configurable Service using Java

Configurable Services are typically run time properties. We can use them to define properties that are related to external services on which the broker relies. Instead of defining properties on the node or message flow, we can create configurable services so that nodes and message flows can refer to them to find properties at run time. If we use this method, we can change the values of attributes for a configurable service on the broker, which then affects the behavior of a node or message flow without the need for redeployment.

1. Userdefined Configurable Service
2. Java Method
3. ESQL

Create a user defined configurable service
Go to Websphere MQ > Configurable Service > New > Configurable Service
Select type as Userdefined configurable service.
Add property in the GUI.
Or
You can use command to create Configurable service with Key and Value
mqsicreateconfigurableservice IB9NODE -c UserDefined -o UD1 -n
BHANU -v "2032" 

Java Class
To retrieve the Value from the configurable service we use a simple java call.
We need a ConfigurationManagerProxy jar file in our project workspace.

Version 1 :
package com.get.configvalue;
import com.ibm.broker.config.proxy.*;
public class GET_CONFIG_VALUE
{
public static String getValue(String strKey)
{
String strValue = null;
try{
BrokerProxy b = BrokerProxy.getLocalInstance();

while(!b.hasBeenPopulatedByBroker())
 {
}

ConfigurableService[] CS_set =b.getConfigurableServices("UserDefined");

strValue =CS_set[0].getProperties().getProperty(strKey);
}
catch (Exception e)
{
e.printStackTrace();
}
return strValue;
}
}


Version 2 :
package com.external.java;
import com.ibm.broker.config.proxy.*;
public class GetConfigProperty
{
public static String getValue(String ConfigService,String KeyValue)
{
String ResultValue = null;
try{
BrokerProxy b = BrokerProxy.getLocalInstance();

while(!b.hasBeenPopulatedByBroker()) 
{
}

ConfigurableService CS_set =b.getConfigurableService("UserDefined",ConfigService);

ResultValue = CS_set.getProperties().getProperty(KeyValue);
}
catch (Exception e)
{
e.printStackTrace();
}
return ResultValue;
}
}

ESQL

Version 1 :
Calling Java method in ESQL

CREATE PROCEDURE getValueFromConfig (IN KeyChar CHAR)
RETURNS CHAR
LANGUAGE JAVA
EXTERNAL NAME "com.get.configvalue.GET_CONFIG_VALUE.getValue";

getValueFromConfig : Procedure Name
KeyChar : Input Parameter
com.get.configvalue : Package Name
GET_CONFIG_VALUE : Java Class Name
getValue : Java Method

Calling ESQL procedure

DECLARE strKey CHARACTER 'BHANU';
--result will be stored in strValue

DECLARE strValue CHARACTER;
--retrieves the Value from configurable service properties

CALL getValueFromConfig(strKey) INTO strValue;
 -- (Key:BHANU Value : 2032)

CALL getValueFromConfig('SampleKey') INTO strValue;
 --(Key:SampleKey Value:SampleValue)

Version 2 :
Calling Java method in ESQL

CREATE PROCEDURE getValueFromConfig
(IN ConfigService CHARACTER,IN KeyValue CHARACTER)
RETURNS CHAR
LANGUAGE JAVA
EXTERNAL NAME "com.external.java.GetConfigProperty.getValue";

getValueFromConfig : Procedure Name
ConfigService : ConfigService Name
KeyValue : Key Value in Properties file
GetConfigProperty : Java Class Name
getValue : Java Method

Calling ESQL procedure

DECLARE strKey CHARACTER 'BHANU';
--result will be stored in strValue

DECLARE strValue CHARACTER;
--retrieves the Value from configurable service properties

CALL getValueFromConfig('UD1','BHANU') INTO strValue; 
--(Key:BHANU Value : 2032)

CALL getValueFromConfig('UD2','UD2') INTO strValue;
CALL getValueFromConfig('AD1','AD1') INTO strValue;


Tuesday, 5 November 2013

IMPORTANT SNIPPETS FOR JAVA COMPUTE NODE

1)
create an ESQL module in the mqsi schema. The module is then assigned to a new Compute node by using the setComputeExpression() method:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
ESQLModule module = new ESQLModule();
module.setBrokerSchema("mqsi");
module.setEsqlMain("MyESQLMain");
ComputeNode compNode = new ComputeNode();
compNode.setNodeName("My Compute Node");
compNode.setComputeExpression(module);
mf1.addNode(compNode);

2)
create an ESQL module in the default schema. The setBrokerSchema() method is not required.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
ESQLModule module = new ESQLModule();
module.setEsqlMain("MyESQLMain");
ComputeNode compNode = new ComputeNode();
compNode.setNodeName("My Compute Node");
compNode.setComputeExpression(module);
mf1.addNode(compNode);

3)
Discover an ESQL module from within an ESQL file by using the getEsqlModules() method. You can then use the ESQL module to set the compute expression on a Compute node by using the setComputeExpression() method.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
File esql = new File("FileBatchProcessingSample_Branch.esql");
ESQLFile esqlFile = new ESQLFile(esql);
Vector<ESQLModule> esqlModules = esqlFile.getEsqlModules();
ComputeNode compNode = new ComputeNode();
compNode.setNodeName("My Compute Node");
compNode.setComputeExpression(esqlModules.get(0));
mf1.addNode(compNode);

4)
To load a message flow into memory, create a File object and use the read() method of the FlowRendererMSGFLOW, which makes the message flow available for your Java code. The read() method takes the message flow project containing the required message flow file and the relative path to the message flow file from this project.
File msgFlow = new File("../mqsi/main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);

5)
To rename a node, you must first access the required node. You can access the node by using the existing name of the node and the getNodeByName() method. You can then rename the node by using the setNodeName() method, which takes the new node name as a parameter.

For example:
File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
Node mqinNode = mf1.getNodeByName("My Input Node");
mqinNode.setNodeName("New Input Node");

6)
Example to  show  how to add a new built-in node:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
MQInputNode mqinNode = new MQInputNode();
mqinNode.setNodeName("My Input Node");
mqinNode.setQueueName("INPUTQ");
mf1.addNode(mqinNode);

Example to  show  how to add a new subflow node to a message flow:

  1. A new subflow node is created and assigned to object sfNode.
  2. The subflow node name is set to My Sub Flow Node.
  3. The subflow node is linked to the subflow message flow by using the setSubFlow() method.
  4. The new subflow node is added to the message flow held in object mf1.

The subflow can be stored in a .msgflow format:

  • File msgFlow = new File("main.msgflow");
    MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
    File subFlow = new File("subflow.msgflow");
    MessageFlow sub1 = FlowRendererMSGFLOW.read(subFlow);
    SubFlowNode sfNode = new SubFlowNode();
    sfNode.setNodeName("My Sub Flow Node");
    sfNode.setSubFlow(sub1);
    mf1.addNode(sfNode);
The subflow can be stored in a .subflow format:

  • File msgFlow = new File("main.msgflow");
    MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
    sFile subFlow = new File("subflow.subflow");
    MessageFlow sub1 = FlowRendererMSGFLOW.read(subFlow);
    SubFlowNode sfNode = new SubFlowNode();
    sfNode.setNodeName("My Sub Flow Node");
    sfNode.setSubFlow(sub1);
    mf1.addNode(sfNode);

7)
Set the position of a node on the canvas by using the setLocation() method of the node object. The following example sets the position of a new MQOutput node to coordinates x=300 pixels, y=100 pixels:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
MQOutputNode mqoutNode = new MQOutputNode();
mqoutNode.setLocation(300, 100);

8)
Copy a built-in or subflow node by using the clone() method. In the following example, a new MQInput node mqinNode is created and properties on the node are set. A new MQInput node mqinNode1 is then created by copying mqinNode by using the clone() method. When the node is copied, the node properties are also copied:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
MQInputNode mqinNode = new MQInputNode();
mqinNode.setNodeName("My Input Node");
mqinNode.setQueueName("INPUTQ");
MQInputNode mqinNode1 = (MQInputNode) mqinNode.clone();
mqinNode1.setNodeName("Copy of My Input Node");
mf1.addNode(mqinNode1);

9)
Remove a node you must first get the required node from the message flow object. In the following example, the getNodeByName() method is used to get the required node from message flow object mf1. The node is then removed by using the removeNode() method. When a node is removed, any connections to or from the node are also removed:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
Node mqinNode = mf1.getNodeByName("My Input Node");
mf1.removeNode(mqinNode);

10)
Example  to shows how to connect two built-in nodes:

  • A MQInput node and a Collector node are created.
  • The getInputTerminal() method is used to create a dynamic Input terminal called NEWIN on the Collector node.
  • The Input terminal is connected to the Output terminal of the MQInput node by using the connect() method of the message flow object mf1.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
MQInputNode mqinNode = new MQInputNode();
mqinNode.setNodeName("My Input Node");
mqinNode.setQueueName("INPUTQ");
CollectorNode colNode = new CollectorNode();
colNode.getInputTerminal("NEWIN");
mf1.connect(mqinNode.OUTPUT_TERMINAL_OUT, colNode.getInputTerminal("NEWIN"));

Example to show  how to connect a subflow node to a built-in node.

The main message flow, main.msgflow, and a Compute node in the main message flow are loaded into memory.
  • The subflow message flow, subflow.msgflow, and the subflow node in the main message flow are loaded into memory.
  • The setSubFlow() method of the subflow node is used to link the subflow message flow sub1 to the subflow node sfNode.
  • The getOutputTerminal() method is used to get the Process terminal of the subflow node. The connect() method of the message flow object is used to connect this terminal to the Input terminal of the Compute node.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
ComputeNode compNode = (ComputeNode)mf1.getNodeByName("My Compute Node");
File subFlow = new File("subflow.msgflow");
MessageFlow sub1 = FlowRendererMSGFLOW.read(subFlow);
SubFlowNode sfNode = (SubFlowNode)mf1.getNodeByName("My Subflow Node");
sfNode.setSubFlow(sub1);
mf1.connect(sfNode.getOutputTerminal("Process"), compNode.INPUT_TERMINAL_IN);

11)
Example to  show  how to  add a user-defined node to a message flow and connect it to a built-in node:

  1. An MQInput node is created and added to the message flow.
  2. A user-defined node is created by using the GenericNode class and is added to the message flow object.
  3. The static output terminal of the MQInput is assigned to the variable outputTerminal.
  4. The input terminal of the user-defined node is assigned to the variable inputTerminal by using the getInputTerminal() method with the known terminal name In.
  5. The nodes are connected by using the connect() method.
  6. The final section of code shows that the input node is now available for use in the message flow, by using the getInputTerminals() method of the user-defined node instance.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);

MQInputNode mqinNode = new MQInputNode();
mqinNode.setNodeName("My Input Node");
mqinNode.setQueueName("IN");
mf1.addNode(mqinNode);

GenericNode myNode = new GenericNode("MyUserDefinedNode");
myNode.setNodeName("MyNode");
mf1.addNode(myNode);

OutputTerminal outputTerminal = mqinNode.OUTPUT_TERMINAL_OUT;
InputTerminal inputTerminal = myNode.getInputTerminal("In");
mf1.connect(outputTerminal, inputTerminal);

InputTerminal[] inputTerminals = myNode.getInputTerminals();
System.out.println("Input terminals on my node:");
for (int i = 0; i < inputTerminals.length; i++)
{
  InputTerminal inputTerminal = inputTerminals[i];
  System.out.println(inputTerminal.getName());
}

12)
Disconnect two nodes by using the disconnect() method of the message flow object. You must provide this method with the names of the terminal instances that you want to disconnect.

For example:
File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
MQInputNode mqinNode = (MQInputNode)mf1.getNodeByName("My Input Node");
MQOutputNode mqoutNode = (MQOutputNode)mf1.getNodeByName("My Output Node");
mf1.disconnect(mqinNode.OUTPUT_TERMINAL_OUT, mqoutNode.INPUT_TERMINAL_IN);

13)
Example to  show how to create a UDP and add it to a message flow:

  1. A UDP called Property1 is created in parameter group Group1. The data type of the UDP is defined as a string and the UDP is given the default value Hello World!
  2. The UDP is then added to the message flow by using the addFlowProperty() method.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
UserDefinedProperty udp = new UserDefinedProperty("Group1", "Property1", UserDefinedProperty.Usage.MANDATORY, UserDefinedProperty.Type.STRING, "Hello World!");
mf1.addFlowProperty(udp);

UDPs in a message flow are discovered by using the getFlowProperties() method on the message flow. The setName() method is then used to set the name of the first UDP to Property3:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
Vector<FlowProperty> flowProperties = mf1.getFlowProperties();
flowProperties.get(0).setName("Property3");

14)
Rename a message flow by using the setName() method. In the following example, a message flow file called main.msgflow is renamed to mainGenerated.msgflow:

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
mf1.setName(mf1.getName()+"Generated");

15)
Add and update rows on the filter table of a Route node.
Adding a new row
The following example shows you how to add a new row to a filter table by using the createRow() method:
  1. The message flow and the Route node are loaded into memory.
  2. The filter table of the Route node is loaded into memory by using the getFilterTable() method of the RouteNode object.
  3. A new filter table row is created by using the createRow() method.
  4. The value of the filter pattern property on this new row is set to value="123" by using the setFilterPattern() method.
  5. The routing output terminal property is set to NEWOUT by using the setRoutingOutputTerminal() method.
  6. The new row is then added to the filter table by using the addRow() method.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
RouteNode routeNode = (RouteNode)mf1.getNodeByName("My Route Node");
RouteNode.FilterTable filterTable = (RouteNode.FilterTable)routeNode.getFilterTable();
RouteNode.FilterTableRow newRow = filterTable.createRow();
newRow.setFilterPattern("value=\"123\"");
newRow.setRoutingOutputTerminal("NEWOUT");
filterTable.addRow(newRow);

Updating a row

The following example shows you how to update rows on the filter table of a Route node.
  1. The message flow, Route node, and filter table of the Route node are loaded into memory.
  2. The rows of the filter table are loaded into memory by using the getRows() method.
  3. The filter pattern property of the first row of the filter table is set to value2="456".
  4. The routing output terminal property of the first row of the filter table is set to NEWOUT2.

File msgFlow = new File("main.msgflow");
MessageFlow mf1 = FlowRendererMSGFLOW.read(msgFlow);
RouteNode routeNode = (RouteNode)mf1.getNodeByName("My Route Node");
RouteNode.FilterTable filterTable = (RouteNode.FilterTable)routeNode.getFilterTable();
Vector<RouteNode.FilterTableRow> filterTableRows = filterTable.getRows();
filterTableRows.get(0).setFilterPattern("value2=\"456\"");
filterTableRows.get(0).setRoutingOutputTerminal("NEWOUT2");

Thursday, 11 July 2013

JAVA COMPUTE NODE CODING & STANDARDS TO CREATE A OUTPUT STRUCTURE FOR AN INPUT XML


import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;

public class JavaComputeNodeMsgFlow_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>
 
 

   
     //-- This Line used when you have Single <EMP></EMP> Record---
//MbElement OutEmp = OutDetails.createElementAsFirstChild(MbElement.TYPE_NAME,"EMP",null);   
                                                                                                         //<EMP></EMP>
       

   
     //Extracting child elements from <EMP>
    
     for(int i=0;i<Emp.length;i++){
         //-- This Line used when you have Multiple <EMP></EMP> Record---
MbElement OutEmp = OutDetails.createElementAsFirstChild(MbElement.TYPE_NAME,"EMP",null);
                                                                                                           //<EMP></EMP>
        
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 Multiple <EMP></EMP> Record ,OutEmp
         OutEmp.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"ENAME",Ename);
         OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"LOCATION",Location);
         OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"BATCH",Batch);
         OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"COMPANY",Company);
          } // End Of For Loop

       
     //Create Values inside Single <EMP></EMP> Record ,OutEmp
 
//OutEmp.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"ENAME",Ename);
     //OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"LOCATION",Location);
     //OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"BATCH",Batch);
     //OutEmp.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"COMPANY",Company);
 

   
//detach XMLNSC last child because outMessage is created from inMessage and already contains InMessage Structure.
  OutXmlNsc.getLastChild().detach();
    
    
//Finally Propogate the Output to OutAssembly
out.propagate(outAssembly);
    
//clear the output message
 outMessage.clearMessage();   
  }
}