`

WebSphere Message Broker -JavaCompute组件的使用

阅读更多

 

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

	}

}

 

 

 

 

 

 

下面有一个文档,可以参考一下:

 

 

分享到:
评论

相关推荐

    精通WebSphere Message Broker》-陈宇翔-源代码

    《精通WebSphere Message Broker》-陈宇翔-源代码及相关工具-4482这个压缩包文件主要聚焦于IBM的WebSphere Message Broker(WMB)技术,这是一款强大的企业级消息中间件产品。通过深入理解和掌握WMB,开发者能够构建...

    WebSphere Message Broker QPP0.doc

    3.1. WEBSPHERE MESSAGE BROKER的附加组件 10 3.1.1. WebSphere Message Broker Extender for TIBCO RV for Multiplatforms 10 3.1.2. WebSphere Message Broker File Extender 10 3.1.3. WebSphere Message Broker ...

    Websphere Message Broker配置总结

    通过对Websphere Message Broker 6.1配置操作的详细梳理,我们了解了如何进行队列管理器的基本操作、数据库管理、配置管理器管理、代理管理、组件数据库操作以及MQ控制命令的使用方法。这些基础知识对于部署和维护...

    精通WebSphere Message Broker

    精通WebSphere Message Broker,清晰版本

    Message Broker 中JavaCompute Node 的一个示例

    JavaCompute Node是IBM Message Broker(也称为IBM WebSphere MQ Message Broker)中的一个组件,它允许开发人员用Java编写自定义处理逻辑,以接收、处理和转发消息。这种节点类型提供了强大的灵活性,因为它可以...

    Websphere Message Broker实践,WebSphere MQ Java编程

    Websphere Message Broker实践,WebSphere MQ Java编程,Message Broker 计时器节点编程模式,MessageBroker TCPIP通信协议,wmb关于ws服务的引用,WMB连接oracle数据库实践,全部组件

    IBM WebSphere message broker 命令详解

    ### IBM WebSphere Message Broker 命令详解 IBM WebSphere Message Broker(以下简称“Message Broker”)是IBM提供的一款用于企业级消息处理的软件,它能够高效地管理和传递大量的消息数据,支持复杂的消息处理...

    Websphere Message Broker实例教程

    精通Websphere Message Broker,最主要的功能是消息选路、消息传送、消息扩展和发布/订阅。显然,这些功能使WebSphere Message Broker成为实现ESB业务集成的首选

    WebSphere Message Broker 开发和部署最佳实践

    WebSphere Message Broker 是一个企业服务总线(ESB),提供了用于各种协议的通用连接以及为使用结构化和非结构化数据的应用程序提供数据转换功能。为了提高消息处理性能,需要遵循一些最佳实践。 消息流开发阶段的...

    WebSphere Message Broker Basics_v6.pdf

    WebSphere Message Broker是IBM提供的一个企业级的消息中间件产品,用于实现应用程序和服务之间的异步通信和数据交换,是构建SOA的关键组件之一。它能够处理大量消息,并支持多种消息传递模式,如点对点和发布/订阅...

    WebSphere Message Broker之WebService连接

    WebSphere Message Broker(WMB),是IBM提供的一款强大的企业级消息中间件,它允许应用程序通过消息传递进行通信,实现业务流程的集成。在本主题中,我们将深入探讨WebSphere Message Broker如何与Web服务进行交互...

    Websphere Message Broker MB初学者教程

    Websphere Message Broker(WMB),也称为IBM Integration Bus(IIB),是IBM提供的一款强大的企业服务总线(ESB)和消息中间件产品。它允许企业构建、部署和管理集成解决方案,通过消息传递实现系统之间的通信。在...

    WebSphere Message Broker

    在IBM的WebSphere Message Broker(WMB)中,JavaCompute节点是一种强大的功能,允许开发人员使用Java代码来处理和转换消息。这个节点提供了对消息内容的直接访问,使得可以进行复杂的业务逻辑处理和数据操作。以下...

    Websphere Message Broker ESQL

    **Websphere Message Broker ESQL**是IBM WebSphere Message Broker平台中的一个重要组件,它提供了一种类似于SQL的语言,用于处理消息数据流中的数据。通过使用ESQL,开发人员能够灵活地操纵消息中的数据,实现数据...

    IBM Connecting Your Business Using WebSphere Message Broker V7 as an ESB红皮书

    ##### 2.1 WebSphere Message Broker 的使用场景 WebSphere Message Broker 可以应用于各种复杂的集成场景,例如: - **消息流设计**:使用图形化工具设计消息流,以实现特定的业务逻辑。 - **服务编排**:将多个...

    IBM WebSphere Message Broker Toolkit 7.0 教程(一)

    本教程《精通Websphere Message Broker.pdf》将深入探讨这些主题,帮助读者掌握如何使用IBM WebSphere Message Broker Toolkit 7.0来设计、构建和维护高效的消息传递解决方案。通过学习,你将能够熟练地利用WMB构建...

Global site tag (gtag.js) - Google Analytics