WebSphere Message Broker -JavaCompute元件的使用

悠悠隱於市發表於2011-05-17

 

 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();
		}

	}

}

 

 

 

 

 

 

下面有一個文件,可以參考一下:

 

 

相關文章