IBM WebSphere Message Broker JavaCompute节点的使用.
import java.util.List;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;
public class Sub_FFN_JavaCompute extends MbJavaComputeNode {
private final ArticleCreator articleCreator;
private final StatementCreator statementCreator;
private final SaleListCreator saleListCreator;
public Sub_FFN_JavaCompute() throws MbException
{
// instantiate the output tree creation objects
saleListCreator = new SaleListCreator();
statementCreator = new StatementCreator();
articleCreator = new ArticleCreator();
}
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = inAssembly.getMessage();
// create new message
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,
outMessage);
try {
MbElement root = outMessage.getRootElement();
MbElement document = root.getLastChild().getFirstChild();
MbElement chapter2 = document.createElementAsLastChild(MbElement.TYPE_NAME,"Chapter",null);
// add title attribute
MbElement title2 = chapter2.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,
"title", "Message Flows");
document.createElementAsLastChild(MbElement.TYPE_NAME,"NAME","ZhouHaiTao");
out.propagate(outAssembly);
} finally {
// clear the outMessage
outMessage.clearMessage();
}
}
//复制头信息到outMessage中;
public void copyMessageHeaders(MbMessage inMessage, MbMessage outMessage)
throws MbException
{
MbElement outRoot = outMessage.getRootElement();
// iterate though the headers starting with the first child of the root element
MbElement header = inMessage.getRootElement().getFirstChild();
while (header != null && header.getNextSibling() != null) // stop before the last child (body)
{
// copy the header and add it to the out message
outRoot.addAsLastChild(header.copy());
// move along to next header
header = header.getNextSibling();
}
}
/**
* Iterates over the incoming message's SaleList elements and builds the output
* message's SaleList
* 在入报文的遍历SaleList元素并建立输出讯息。
*
*/
private final class SaleListCreator extends XPathOperation
{
private SaleListCreator() throws MbException
{
super("/SaleEnvelope/SaleList"); // expression evaluated on the incoming message
}
/**
* Called once for each SaleList element
*/
@SuppressWarnings("unchecked")
protected void forEachElement(MbElement saleList) throws MbException
{
List <MbElement> nodeset = (List <MbElement>)getOutputElement().evaluateXPath("/?SaleEnvelope/?$SaleList");
MbElement outSaleList = nodeset.get(0);
// create the statements for each SaleList
statementCreator.setOutputElement(outSaleList);
statementCreator.evaluate(saleList);
}
}
private final class ArticleCreator extends XPathOperation {
private ArticleCreator() throws MbException
{
super("Item"); // expression evaluated on the incoming Invoice sub-tree
}
public ArticleCreator(String expression) throws MbException {
super(expression);
// TODO 自动生成的构造函数存根
}
//遍历元素;
@Override
protected void forEachElement(MbElement element) throws MbException {
// TODO 自动生成的方法存根
}
}
/**
* Iterates over the incoming message's Invoice elements and builds the output
* message's Statement sub-tree
* 遍历的发票在入报文元素并建立输出;
*/
private final class StatementCreator extends XPathOperation
{
private final MbXPath setCustomer = new MbXPath(
"?$Statement[?@Type[set-value('Monthly')]]" +
" [?@Style[set-value('Full')]]" +
" [?Customer[?Initials[set-value(concat($invoice/Initial[1], $invoice/Initial[2]))]]" +
" [?Name[set-value($invoice/Surname)]]" +
" [?Balance[set-value($invoice/Balance)]]]" +
" /?Purchases");
private StatementCreator() throws MbException
{
super("Invoice"); // expression evaluated on the incoming SaleList sub-tree
}
/**
* Called once for each Invoice element
*/
@SuppressWarnings("unchecked")
protected void forEachElement(MbElement invoice) throws MbException
{
MbElement outSaleList = getOutputElement();
setCustomer.assignVariable("invoice", invoice);
List <MbElement> nodeset = (List <MbElement>)outSaleList.evaluateXPath(setCustomer);
MbElement purchases = nodeset.get(0);
// Now create the articles for each invoice
articleCreator.setOutputElement(purchases);
articleCreator.evaluate(invoice);
}
}
/**
* XPathOperation is an abstract helper class allowing a method to be repeatedly applied to
* results of an XPath expression. The user can optionally access the output message tree
* allowing message transformations to be defined based on XPath evaluations of the input
* tree.
* 是一种抽象的XPathOperation帮助类允许一个方法反复应用到,
* 的节点。用户可以选择性地访问输出消息树,
* 许信息转换的被定义基于0评估输入的树
*/
abstract class XPathOperation
{
private MbXPath xpath = null;
private MbElement outputElement = null;
/**
* Constructor taking the XPath expression to evaluate. The expression must evaluate to
* a nodeset.
* 通过构造方法,expression 参数值生成一个xpath路径;
*
*/
public XPathOperation(String expression) throws MbException
{
xpath = new MbXPath(expression);
}
/**
* Must be called prior to the evaluate method if the user wishes to work with the output
* tree (eg for message transformation).
*
*/
public void setOutputElement(MbElement out)
{
outputElement = out;
}
/**
* Allows the user to access the output tree in the forEachElement() method
*/
public MbElement getOutputElement()
{
return outputElement;
}
/**
* Can be overridden by the user to do initialisation processing before iterating over the
* XPath nodeset results
*/
protected void before() throws MbException { }
/**
* This gets called once for each element in the XPath nodeset results. The current element
* is passed in as the only argument. This method is abstract and must be implemented
* in the derived class.
* 这将被调用一次为每个元素在0 nodeset结果。当前元素,
* 通过是作为唯一的论点。该方法是抽象的,必须执行
* 在派生类。
*/
protected abstract void forEachElement(MbElement element) throws MbException;
/**
* Can be overridden by the user to do post-processing after iterating over the
* XPath nodeset results
*/
protected void after() throws MbException { }
/**
* Called by the user to iterate over the XPath nodeset.
*被称为由用户自行遍历的nodeset 0。
* @param element The context element to which the XPath expression will be applied.
*/
@SuppressWarnings("unchecked")
public void evaluate(MbElement element) throws MbException
{
//根据xpath获得一个结果;
Object result = element.evaluateXPath(xpath);
//如果result不是一个List实例;则return;
if(!(result instanceof List))
{
// error
return;
}
before();
//强制转换成List<MbElement>List集合;
List<MbElement> nodeset = (List<MbElement>)result;
for(MbElement node : nodeset)
{
//循环遍历node节点元素;
forEachElement(node);
}
after();
}
}
}