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");

No comments:

Post a Comment