`
yarafa
  • 浏览: 89171 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JBoss ESB学习笔记15——第十四个ESB应用Transform XML to POJO

阅读更多

续上篇介绍了第十三个ESB应用,本文介绍第十四个ESB应用——Transform XML to POJO。

 

说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com/,谢谢合作。

 

 

1 概述
本实例主要演示了如何通过配置Smooks将一个XML文件转换成POJO。

 

2 新建ESB工程
操作过程略。

 

3 ESB配置
3.1 创建消息队列

<?xml version="1.0" encoding="UTF-8"?>
<server>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=transformpojoGw" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=transformpojoEsb" 	xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=transformpojoResponse" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
</server>

 

3.2 定义Provider
这里将定义一个JMS Provider,并定义2个消息通道,内容如下:

<jms-provider connection-factory="ConnectionFactory" name="JBossMQ">
	<jms-bus busid="gwChanel">
		<jms-message-filter dest-name="queue/transformpojoGw"
			dest-type="QUEUE" />
	</jms-bus>
	<jms-bus busid="esbChanel">
		<jms-message-filter dest-name="queue/transformpojoEsb"
			dest-type="QUEUE" />
	</jms-bus>
</jms-provider>

 

3.3 定义Java Bean
3.3.1 Customer

package com.thu.afa.esb.jbossesb.bean;

import java.io.Serializable;

public class Customer implements Serializable
{
	private static final long serialVersionUID = 1L;
	private String userName;
	private String firstName;
	private String lastName;
	private String state;

	/**
	 * @return Returns the firstName.
	 */
	public String getFirstName()
	{
		return firstName;
	}

	/**
	 * @param firstName
	 *            The firstName to set.
	 */
	public void setFirstName(String firstName)
	{
		// System.out.println("**** firstName: " + firstName);
		this.firstName = firstName;
	}

	/**
	 * @return Returns the lastName.
	 */
	public String getLastName()
	{
		return lastName;
	}

	/**
	 * @param lastName
	 *            The lastName to set.
	 */
	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

	/**
	 * @return Returns the state.
	 */
	public String getState()
	{
		return state;
	}

	/**
	 * @param state
	 *            The state to set.
	 */
	public void setState(String state)
	{
		this.state = state;
	}

	/**
	 * @return Returns the userName.
	 */
	public String getUserName()
	{
		return userName;
	}

	/**
	 * @param userName
	 *            The userName to set.
	 */
	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return userName + "," + firstName + "," + lastName + "," + state;
	}

}

 

3.3.2 OrderHeader

package com.thu.afa.esb.jbossesb.bean;

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.Serializable;

public class OrderHeader implements Serializable
{
	private static final long serialVersionUID = 1L;
	private String orderId;
	private Calendar orderDate;
	private int statusCode;
	private double netAmount;
	private double totalAmount;
	private double tax;

	/**
	 * @return Returns the netAmount.
	 */
	public double getNetAmount()
	{
		return netAmount;
	}

	/**
	 * @param netAmount
	 *            The netAmount to set.
	 */
	public void setNetAmount(double netAmount)
	{
		this.netAmount = netAmount;
	}

	/**
	 * @return Returns the orderDate.
	 */
	public Calendar getOrderDate()
	{
		return orderDate;
	}

	/**
	 * @param orderDate
	 *            The orderDate to set.
	 */
	public void setOrderDate(Calendar orderDate)
	{
		this.orderDate = orderDate;
	}

	/**
	 * @return Returns the orderId.
	 */
	public String getOrderId()
	{
		return orderId;
	}

	/**
	 * @param orderId
	 *            The orderId to set.
	 */
	public void setOrderId(String orderId)
	{
		this.orderId = orderId;
	}

	/**
	 * @return Returns the statusCode.
	 */
	public int getStatusCode()
	{
		return statusCode;
	}

	/**
	 * @param statusCode
	 *            The statusCode to set.
	 */
	public void setStatusCode(int statusCode)
	{
		this.statusCode = statusCode;
	}

	/**
	 * @return Returns the tax.
	 */
	public double getTax()
	{
		return tax;
	}

	/**
	 * @param tax
	 *            The tax to set.
	 */
	public void setTax(double tax)
	{
		this.tax = tax;
	}

	/**
	 * @return Returns the totalAmount.
	 */
	public double getTotalAmount()
	{
		return totalAmount;
	}

	/**
	 * @param totalAmount
	 *            The totalAmount to set.
	 */
	public void setTotalAmount(double totalAmount)
	{
		this.totalAmount = totalAmount;
	}

	private SimpleDateFormat dateFormat = new SimpleDateFormat("d-MMM-yyyy");

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return orderId
				+ ", "
				+ (orderDate == null ? null : dateFormat.format(orderDate
						.getTime())) + ", " + statusCode + ", " + netAmount
				+ ", " + totalAmount + ", " + tax + ", ";
	}
}

 

3.3.3 OrderItem

package com.thu.afa.esb.jbossesb.bean;

import java.io.Serializable;

public class OrderItem implements Serializable
{
	private static final long serialVersionUID = 1L;
	private int position;
	private int quantity;
	private String productId;
	private String title;
	private double price;

	/**
	 * @return Returns the position.
	 */
	public int getPosition()
	{
		return position;
	}

	/**
	 * @param position
	 *            The position to set.
	 */
	public void setPosition(int position)
	{
		// System.out.println("**** position: " + position);
		this.position = position;
	}

	/**
	 * @return Returns the price.
	 */
	public double getPrice()
	{
		return price;
	}

	/**
	 * @param price
	 *            The price to set.
	 */
	public void setPrice(double price)
	{
		// System.out.println("**** price: " + price);
		this.price = price;
	}

	/**
	 * @return Returns the productId.
	 */
	public String getProductId()
	{
		return productId;
	}

	/**
	 * @param productId
	 *            The productId to set.
	 */
	public void setProductId(String productId)
	{
		this.productId = productId;
	}

	/**
	 * @return Returns the quantity.
	 */
	public int getQuantity()
	{
		return quantity;
	}

	/**
	 * @param quantity
	 *            The quantity to set.
	 */
	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}

	/**
	 * @return Returns the title.
	 */
	public String getTitle()
	{
		return title;
	}

	/**
	 * @param title
	 *            The title to set.
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return position + "," + quantity + "," + productId + "," + title + ","
				+ price;

	}
}

 

3.4 定义转换映射文件
在src目录下新建文件smooks-res.xml,内容如下:

<?xml version='1.0' encoding='UTF-8'?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd">
    <!-- Populate the OrderHeader -->
    <jb:bean beanId="orderHeader" class="com.thu.afa.esb.jbossesb.bean.OrderHeader" createOnElement="order">
        <jb:value property="orderId"     data="Order/@orderId" />
        <jb:value property="orderDate"   data="Order/@orderDate" decoder="Calendar">
            <jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam>
			<jb:decodeParam name="locale-language">en</jb:decodeParam>
            <jb:decodeParam name="locale-country">US</jb:decodeParam> 
        </jb:value>
        <jb:value property="statusCode"  data="Order/@statusCode" />
        <jb:value property="netAmount"   data="Order/@netAmount" />
        <jb:value property="totalAmount" data="Order/@totalAmount" />
        <jb:value property="tax"         data="Order/@tax" />
    </jb:bean>

    <!-- Populate the Customer -->
    <jb:bean beanId="customer" class="com.thu.afa.esb.jbossesb.bean.Customer" createOnElement="customer">
        <jb:value property="userName"  data="customer/@userName" />
        <jb:value property="firstName" data="customer/@firstName" />
        <jb:value property="lastName"  data="customer/@lastName" />
        <jb:value property="state"     data="customer/@state" />
    </jb:bean>

    <!-- Populate the OrderItem list -->
    <jb:bean beanId="orderItemList" class="java.util.ArrayList" createOnElement="orderlines">
        <jb:wiring beanIdRef="orderItem" />
    </jb:bean>

    <!-- Populate the OrderItem instance -->
    <jb:bean beanId="orderItem" class="com.thu.afa.esb.jbossesb.bean.OrderItem" createOnElement="orderlines/orderline">
        <jb:value property="position"  data="orderline/@position" />
        <jb:value property="quantity"  data="orderline/@quantity" />
        <jb:value property="productId" data="orderline/product/@productId" />
        <jb:value property="title"     data="orderline/product/@title" />
        <jb:value property="price"     data="orderline/product/@price" />
    </jb:bean>    
</smooks-resource-list>

 

3.4 定义Service

<service category="Transform" description="Transform"
name="TransformService">
	<listeners />
	<actions mep="OneWay" />
</service>

 

3.5 定义Listener

<listeners>
	<jms-listener busidref="gwChanel" is-gateway="true"
		name="gwListener" />
	<jms-listener busidref="esbChanel" name="esbListener" />
</listeners>

 

3.6 定义Action类
3.6.1 ReturnJMSMessage

/***********************************************************************
 * <p>Project Name: transformxml2pojo</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.ReturnJMSMessage.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import java.util.Hashtable;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.jboss.soa.esb.addressing.eprs.JMSEpr;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;

/**
 * <p>Class Name: ReturnJMSMessage</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-17
 * @version 1.0
 */
public class ReturnJMSMessage
{
	public static void sendMessage(Message esbMessage, String destination,
			ConfigTree config) throws Exception
	{
		if (esbMessage == null || destination == null)
			throw new Exception("Message and JMS Destination are required");

		QueueConnection connection;
		QueueSession session;
		Queue queue;

		final Hashtable<String, String> env = new Hashtable<String, String>();
		final String providerURL = config.getAttribute(JMSEpr.JNDI_URL_TAG);
		if (providerURL != null)
		{
			env.put(Context.PROVIDER_URL, providerURL);
		}
		final String contextFactory = config.getAttribute(JMSEpr.JNDI_CONTEXT_FACTORY_TAG);
		if (contextFactory != null)
		{
			env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
		}

		final String urlPkgPrefixes = config.getAttribute(JMSEpr.JNDI_PKG_PREFIX_TAG);
		if (urlPkgPrefixes != null)
		{
			env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
		}

		InitialContext context = new InitialContext(env);
		QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
		connection = factory.createQueueConnection();
		queue = (Queue) context.lookup("queue/" + destination);
		session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		connection.start();

		String message = (String) esbMessage.getBody().get();

		QueueSender send = session.createSender(queue);
		TextMessage textMessage = session.createTextMessage(message);
		send.send(textMessage);

		connection.stop();
	}
}

 

3.6.2 MyJMSListenerAction

/***********************************************************************
 * <p>Project Name: transformxml2pojo</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.MyJMSListenerAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;


/**
 * <p>Class Name: MyJMSListenerAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-17
 * @version 1.0
 */
public class MyJMSListenerAction extends AbstractActionLifecycle
{
	protected ConfigTree configTree;
	
	public MyJMSListenerAction(ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	public Message displayMessage(Message message) throws Exception
	{
		System.out.println("******* Display Message ********");
		System.out.println(message.getBody().get());
		
		return message;
	}
	
	public Message playWithMessage(Message message) throws Exception
	{
		Body body = message.getBody();
		String content = (String) body.get();
		StringBuffer buffer = new StringBuffer();
		buffer.append("********* Before *********");
		buffer.append(content);
		buffer.append("********* After *********");
		body.add(buffer.toString());
		
		return message;
	}
	
	public Message sendResponse(Message message) throws Exception
	{
		ReturnJMSMessage.sendMessage(message, "transformpojoResponse", configTree);
		return message;
	}
	
	public void exceptionHandler(Message message, Throwable exception)
	{
		System.out.println("!ERROR!");
        System.out.println(exception.getMessage());
        System.out.println("For Message: ");
        System.out.println(message.getBody().get());
	}
}

 

3.6.3 DVDStoreAction

/***********************************************************************
 * <p>Project Name: transformxml2pojo</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.DVDStoreAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import java.util.List;
import java.util.Map;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;

import com.thu.afa.esb.jbossesb.bean.Customer;
import com.thu.afa.esb.jbossesb.bean.OrderHeader;

/**
 * <p>Class Name: DVDStoreAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-17
 * @version 1.0
 */
public class DVDStoreAction extends AbstractActionLifecycle
{
	protected ConfigTree configTree;
	
	public DVDStoreAction(ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	@SuppressWarnings("unchecked")
	public Message process(Message message) throws Exception
	{
		StringBuffer results = new StringBuffer();
		Map<String, Object> map = (Map<String, Object>) message.getBody().get();
		OrderHeader header = (OrderHeader) map.get("orderHeader");
		Customer customer = (Customer) map.get("customer");
		List orderItems = (List) map.get("orderItemList");
		
		results.append("Demonstrates Smooks ability to rip the XML into Objects\n");
		results.append("********* DVDStoreAction - Order Value Objects Populated *********\n");
		results.append("Header: " + header + "\n");
		results.append("Customer: " + customer + "\n");
		if (orderItems != null)
		{
			results.append("Order Items (" + orderItems.size() + "):\n");
			for (int i = 0; i < orderItems.size(); i++)
			{
				results.append("\t" + i + ": " + orderItems.get(i) + "\n");
			}
		}
		results.append("\n****************************************************************** ");
		message.getBody().add(results.toString());

		return message;
	}
}

 

3.7 配置Action

<actions mep="OneWay">
	<action name="printBefore" process="displayMessage"
class="com.thu.afa.esb.jbossesb.action.MyJMSListenerAction" />
	<action class="org.jboss.soa.esb.smooks.SmooksAction"
name="transform">
		<property name="smooksConfig" value="/smooks-res.xml" />
		<property name="resultType" value="JAVA" />
	</action>
	<action class="com.thu.afa.esb.jbossesb.action.DVDStoreAction"
		name="convertPOJO2Message" process="process" />
	<action name="printAfter" process="displayMessage"
class="com.thu.afa.esb.jbossesb.action.MyJMSListenerAction" />
	<action name="returnToSender" process="sendResponse"
class="com.thu.afa.esb.jbossesb.action.MyJMSListenerAction" />
	<action class="org.jboss.soa.esb.actions.SystemPrintln"
name="print">
		<property name="message"
			value=">>>> Message after Smooks intermediate xml -> target pojos :" />
	</action>
</actions>

 

3.8 配置部署文件
部署依赖文件deployment.xml内容如下:

<jbossesb-deployment>
	<depends>jboss.esb:deployment=smooks.esb</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=transformpojoGw
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=transformpojoEsb
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=transformpojoResponse
	</depends>
</jbossesb-deployment>

 

3.9 部署ESB
将整个工程导出成一个ESB文件,并保存至JBoss ESB Server的部署目录下,启动JBoss ESB Server即可。

 

4 ESB客户端
4.1 新建Java工程

这里略去操作过程以及添加所需要的Jar包,具体操作过程可参考第一个ESB实例说明。

 

4.2 发送消息的客户端

/***********************************************************************
 * <p>Project Name: helloworldclient</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.client.TransformXML2POJOClient.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.client;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Class Name: TransformXML2POJOClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-17
 * @version 1.0
 */
public class TransformXML2POJOSendClient
{
	private QueueConnection connection;
	private QueueSession session;
	private Queue queue;
	
	public void setupConnection() throws Exception
	{
		Properties properties = new Properties();
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");   
		properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");   
		properties.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
		InitialContext context = new InitialContext(properties);
		
		QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
		connection = factory.createQueueConnection();
		queue = (Queue) context.lookup("queue/transformpojoGw");
		session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		connection.start();
		
		System.out.println("Connection Started");
	}
	
	public void stop() throws Exception
	{
		if(connection != null) connection.stop();
		if(session != null) session.close();
		if(connection != null) connection.close();
	}
	
	public void sendAMessage(String text) throws Exception
	{
		QueueSender sender = session.createSender(queue);
		ObjectMessage message = session.createObjectMessage(text);
		sender.send(message);
		sender.close();
	}
	
	public String readAsciiFile(String fileName) throws IOException
	{
		FileReader fr = null;
		char[] thechars = null;
		
		File thefile = new File(fileName);
		fr = new FileReader( thefile );
		int size = (int) thefile.length();
		thechars = new char[size];
		int count, index = 0;
		while( ( count = fr.read( thechars, index, size ) ) > 0 )
		{
			size -= count;
			index += count;
		}
		fr.close();
		
		return new String(thechars);
	}
	
	/**
	 * <p>Title: </p>
	 * <p>Method Name: main</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: 2010-9-17
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		TransformXML2POJOSendClient client = new TransformXML2POJOSendClient();
		client.setupConnection();
		String fileContent = client.readAsciiFile("SampleOrder.xml");
		System.out.println("---------------------------------------------");
		System.out.println(fileContent);
    	System.out.println("---------------------------------------------");
    	client.sendAMessage(fileContent);
    	client.stop();
	}

}

 

在客户端工程下创建SampleOrder.xml文件,并输入以下内容:

<Order orderId="1" orderDate="Wed Nov 15 13:45:28 EST 2006" statusCode="0" netAmount="59.97" totalAmount="64.92" tax="4.95">
	<Customer userName="user1" firstName="Harry" lastName="Fletcher" state="SD"/>
	<OrderLines>
		<OrderLine position="1" quantity="1">
			<Product productId="364" title="The 40-Year-Old Virgin " price="29.98"/>
		</OrderLine>
		<OrderLine position="2" quantity="1">
			<Product productId="299" title="Pulp Fiction" price="29.99"/>
		</OrderLine>
	</OrderLines>
</Order>

 

4.3 接收消息的客户端

/***********************************************************************
 * <p>Project Name: helloworldclient</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.client.TransformXML2POJOReceiveClient.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.client;

import java.util.Properties;

import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Class Name: TransformXML2POJOReceiveClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-17
 * @version 1.0
 */
public class TransformXML2POJOReceiveClient
{
	private QueueConnection connection;
	private QueueSession session;
	private Queue queue;
	
	public void setupConnection() throws Exception
	{
		Properties properties = new Properties();
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");   
		properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");   
		properties.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
		InitialContext context = new InitialContext(properties);
		
		QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
		connection = factory.createQueueConnection();
		queue = (Queue) context.lookup("queue/transformpojoResponse");
		session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		connection.start();
		
		System.out.println("Connection Started");
	}
	
	public void stop() throws Exception
	{
		if(connection != null) connection.stop();
		if(session != null) session.close();
		if(connection != null) connection.close();
	}
	
	public void receiveMessage() throws Exception
	{
		QueueReceiver receiver = session.createReceiver(queue);
		Message message = receiver.receive();
		if(message != null)
		{
			if(message instanceof ObjectMessage)
			{
				ObjectMessage objectMessage = (ObjectMessage) message;
				System.out.println(objectMessage.getObject().toString());
			} else if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				System.out.println(textMessage.getText());
			}
		}
		receiver.close();
	}

	public static void main(String[] args) throws Exception
	{
		TransformXML2POJOReceiveClient client = new TransformXML2POJOReceiveClient();
		client.setupConnection();
		client.receiveMessage();
		client.stop();
	}
}

 

4.4 运行客户端

首先运行接收消息的客户端,然后运行发送消息的客户端,即可得到如下输出结果,以下三图从上到下依次是:发送消息客户端输出结果,接收消息客户端输出结果,ESB服务器控制台输出结果。

JBoss ESB

 

JBoss ESB

 

JBoss ESB 

 

 

-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Apr 12nd, 2011
-----------------------------------------------------

 

分享到:
评论
1 楼 wujihuio 2011-09-06  
谢谢,太感谢了,贡献太大了

相关推荐

    JBoss ESB 学习笔记

    #### 十五、第十四个ESB应用Transform XML to POJO - **重点**:学习如何将XML数据解析为Java对象。 - **实现**:使用XML解析技术,如XStream等工具将XML数据转换为Java对象。 #### 十六、第十五个ESB应用Web ...

    ESB应用Transform_XML_to_POJO

    ESB应用Transform_XML_to_POJO

    ESB产品说明

    Mule的核心优势在于其灵活的消息对象模型——Universal Message Objects (UMO),自Mule 2.0版本起,UMO成为了Mule架构的一部分,支持多种消息格式,如UMO POJO、JavaBean等。Mule支持超过20种的传输协议,包括但不...

    用例及领域模型[汇编].pdf

    - JBoss(BO)、WebLogic(BO):它们提供了企业服务总线(Enterprise Service Bus,ESB)能力,用于服务集成。 - XML(Extensible Markup Language):是一种通用标记语言,用来存储和传输数据。 - Bean:在Java中...

    漫画作品与时间旅行题材.doc

    漫画作品与时间旅行题材

    基于SpringBoot框架的的在线视频教育平台的设计与实现(含完整源码+完整毕设文档+PPT+数据库文件).zip

    Spring Boot特点: 1、创建一个单独的Spring应用程序; 2、嵌入式Tomcat,无需部署WAR文件; 3、简化Maven配置; 4、自动配置Spring; 5、提供生产就绪功能,如指标,健康检查和外部配置; 6、绝对没有代码生成和XML的配置要求;第一章 绪 论 1 1.1背景及意义 1 1.2国内外研究概况 2 1.3 研究的内容 2 第二章 关键技术的研究 3 2.1 相关技术 3 2.2 Java技术 3 2.3 ECLIPSE 开发环境 4 2.4 Tomcat介绍 4 2.5 Spring Boot框架 5 第三章 系统分析 5 3.1 系统设计目标 6 3.2 系统可行性分析 6 3.3 系统功能分析和描述 7 3.4系统UML用例分析 8 3.4.1管理员用例 9 3.4.2用户用例 9 3.5系统流程分析 10 3.5.1添加信息流程 11 3.5.2操作流程 12 3.5.3删除信息流程 13 第四章 系统设计 14 4.1 系统体系结构 15 4.2 数据库设计原则 16 4.3 数据表 17 第五章 系统实现 18 5.1用户功能模块 18 5.2

    PyTorch入门指南:从零开始掌握深度学习框架.pdf

    内容概要:本文作为PyTorch的入门指南,首先介绍了PyTorch相较于TensorFlow的优势——动态计算图、自动微分和丰富API。接着讲解了环境搭建、PyTorch核心组件如张量(Tensor)、autograd模块以及神经网络的定义方式(如nn.Module),并且给出了详细的神经网络训练流程,包括前向传播、计算损失值、进行反向传播以计算梯度,最终调整权重参数。此外还简要提及了一些拓展资源以便进一步探索这个深度学习工具。 适用人群:初次接触深度学习技术的新学者和技术爱好者,有一定程序基础并希望通过PyTorch深入理解机器学习算法实现的人。 使用场景及目标:该文档有助于建立使用者对于深度学习及其具体实践有更加直观的理解,在完成本教程之后,读者应当能够在个人设备上正确部署Python环境,并依据指示独立创建自己的简易深度学习项目。 其他说明:文中所提及的所有示例均可被完整重现,同时官方提供的资料链接也可以方便有兴趣的人士对感兴趣之处继续挖掘,这不仅加深了对PyTorch本身的熟悉程度,也为未来的研究或者工程项目打下了良好的理论基础和实践经验。

    古镇美食自驾游:舌尖上的历史韵味.doc

    古镇美食自驾游:舌尖上的历史韵味

    基于人工神经网络(ANN)的高斯白噪声的系统识别 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    漫画作品与神话传说融合.doc

    漫画作品与神话传说融合

    实时电价机制下交直流混合微网优化运行方法 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    ADC推理软件AI程序

    ADC推理软件AI程序

    漫画作品与科幻元素融合.doc

    漫画作品与科幻元素融合

    【电缆】中压电缆局部放电的传输模型研究 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    基于人工神经网络的类噪声环境声音声学识别 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    多约束、多车辆VRP问题 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    基于麻雀搜索算法(SSA)优化长短期记忆神经网络参数SSA-LSTM冷、热、电负荷预测 附Python代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    java-springboot+vue景区民宿预约系统实现源码(完整前后端+mysql+说明文档+LunW+PPT).zip

    java-springboot+vue景区民宿预约系统实现源码(完整前后端+mysql+说明文档+LunW+PPT).zip

    56页-智慧园区解决方案(伟景行).pdf

    在智慧城市建设的大潮中,智慧园区作为其中的璀璨明珠,正以其独特的魅力引领着产业园区的新一轮变革。想象一下,一个集绿色、高端、智能、创新于一体的未来园区,它不仅融合了科技研发、商业居住、办公文创等多种功能,更通过深度应用信息技术,实现了从传统到智慧的华丽转身。 智慧园区通过“四化”建设——即园区运营精细化、园区体验智能化、园区服务专业化和园区设施信息化,彻底颠覆了传统园区的管理模式。在这里,基础设施的数据收集与分析让管理变得更加主动和高效,从温湿度监控到烟雾报警,从消防水箱液位监测到消防栓防盗水装置,每一处细节都彰显着智能的力量。而远程抄表、空调和变配电的智能化管控,更是在节能降耗的同时,极大地提升了园区的运维效率。更令人兴奋的是,通过智慧监控、人流统计和自动访客系统等高科技手段,园区的安全防范能力得到了质的飞跃,让每一位入驻企业和个人都能享受到“拎包入住”般的便捷与安心。 更令人瞩目的是,智慧园区还构建了集信息服务、企业服务、物业服务于一体的综合服务体系。无论是通过园区门户进行信息查询、投诉反馈,还是享受便捷的电商服务、法律咨询和融资支持,亦或是利用云ERP和云OA系统提升企业的管理水平和运营效率,智慧园区都以其全面、专业、高效的服务,为企业的发展插上了腾飞的翅膀。而这一切的背后,是大数据、云计算、人工智能等前沿技术的深度融合与应用,它们如同智慧的大脑,让园区的管理和服务变得更加聪明、更加贴心。走进智慧园区,就像踏入了一个充满无限可能的未来世界,这里不仅有科技的魅力,更有生活的温度,让人不禁对未来充满了无限的憧憬与期待。

    边境自驾游异国风情深度体验.doc

    边境自驾游异国风情深度体验

Global site tag (gtag.js) - Google Analytics