开始之前
WebSphere Message Broker 为称为 SwitchNode 和 TransformNode 的两个用户定义的样本节点提供了数据源。您可以使用当前状态下的这些节点,也可以修改这些节点。
在概念上,消息处理节点用于按相同方法处理消息,输出节点用于将消息输出为位流。但是,当您编码消息处理节点或输出节点时,它们本质上是相同的东西。您可以在输出节点中执行消息处理,同样您可以使用消息处理节点将消息输出到位流。为简便起见,本主题在提到节点时大都指的是消息处理节点,不过本主题依然会讲述这两类节点的功能。
显示“Java 设置”面板。
任何实现 MbNodeInterface 且包含在代理的 LIL 路径中的类都在代理中注册为消息处理节点。当您实现 MbNodeInterface 时,您还必须为此类实现 evaluate 方法。代理为每条经过这个流的消息调用 evaluate 方法。
package com.ibm.jplugins; import com.ibm.broker.plugin.*; public class BasicNode extends MbNode implements MbNodeInterface
当实例化节点时,将调用用户的节点类的构造函数。这是您创建节点的终端并初始化属性的任何缺省值的地方。
消息处理节点有许多个与它关联的输入终端和输出终端。当实例化节点时,方法 createInputTerminal 和 createOutputTerminal 用于将终端添加到节点。例如,要创建带有一个输入终端和两个输出终端的节点:
public MyNode() throws MbException { // create terminals here createInputTerminal ("in"); createOutputTerminal ("out"); createOutputTerminal ("failure"); }
在许多情况下,用户定义的节点需要访问在它的输入终端上接收到的消息的内容。消息由语法元素的树表示。提供了 evaluate 方法使用的多组实用程序函数,这些函数用于消息管理、消息缓冲区访问、语法元素导航和语法元素访问。
MbElement 类提供了到语法元素的接口。有关 Java API 的进一步详细信息,请参阅 Javadoc。
例如:
接收到的输入消息是只读的,因此在消息可被转换之前,您必须将它写到新的输出消息。您可以从输入消息复制元素,或者您可以在输出消息中创建新元素。 新的元素通常在解析器的域中。
MbMessage 类提供了副本构造器,以及获取消息的根元素的方法。MbElement 类提供了到语法元素的接口。
MbMessage newMsg = new MbMessage(assembly.getMessage()); MbMessageAssembly newAssembly = new MbMessageAssembly(assembly, newMsg);
MbElement rootElement = newAssembly.getMessage().getRootElement(); MbElement switchElement = rootElement.getFirstElementByPath("/XML/data/action");
String elementValue = (String)switchElement.getValue(); if(elementValue.equals("add")) switchElement.setValue("change"); else if(elementValue.equals("change")) switchElement.setValue("delete"); else if(elementValue.equals("delete")) switchElement.setValue("hold"); else switchElement.setValue("failure");
MbElement tag = switchElement.createElementAsLastChild(MbElement.TYPE_NAME, "PreviousValue", elementValue);
tag.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "NewValue", switchElement.getValue()); MbOutputTerminal out = getOutputTerminal("out");
createElementAfter(String) createElementAsFirstChild(String) createElementAsLastChild(String) createElementBefore(String) createElementAsLastChildFromBitstream(byte[], String, String, String, String, int, int, int)应该使用这些方法,因为它们是专门用于将解析器分配给消息树文件夹的。
createElementAfter(int) createElementAfter(int, String, Object) createElementAsFirstChild(int) createElementAsFirstChild(int, String, Object) createElementAsLastChild(int) createElementAsLastChild(int, String, Object) createElementBefore(int) createElementBefore(int, String, Object)
在您传播消息之前,您必须确定您要传播的消息流数据,以及用于接收数据的节点的终端。
MbOutputTerminal out = getOutputTerminal("out"); out.propagate(newAssembly);
要清空分配给该消息树的内存,调用最后一个 try/catch 程序段中的 clearMessage() 函数。
您需要声明节点的名称,因为它将由工作台标识。所有节点名称必须以“Node”结尾。您使用下列方法声明此名称:
public static String getNodeName() { return "BasicNode"; }
package com.ibm.pluginsamples; public class BasicNode extends MbNode implements MbNodeInterface { ...
您使用与 Java Bean 属性相同的方法声明节点属性。您负责编写属性的 getter 和 setter 方法,且 API 框架使用 Java 自省规则推断属性名。例如,如果您声明下列两个方法:
private String attributeVariable; public String getFirstAttribute() { return attributeVariable; } publc void setFirstAttribute(String value) { attributeVariable = value; }
代理推断此节点有一个称为 firstAttribute 的属性。此名称从 get 或 set 方法的名称派生而来,而不是从任何内部类成员变量名称派生而来。属性只能以字符串的形式显示,因此您必须在 get 或 set 方法中将任何数字类型转换为字符串或将字符串转换为数字类型。例如,下列方法定义称为 timeInSeconds 的属性:
int seconds; public String getTimeInSeconds() { return Integer.toString(seconds); } public void setTimeInSeconds(String value) { seconds = Integer.parseInt(value); }
如之前描述的那样,对于消息处理或输出节点,您必须实现在 MbNodeInterface 中定义的 evaluate 方法。代理调用此方法来处理消息。此方法应该提供节点的所有处理功能。
public void evaluate(MbMessageAssembly assembly, MbInputTerminal inTerm) throws MbException { // add message processing code here getOutputTerminal("out").propagate(assembly); }
消息流数据由消息、全局环境、本地环境和异常列表组成,可以在节点的输入终端上接收。