`
yarafa
  • 浏览: 89530 次
  • 性别: 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中...

    ### 制造业上市公司高质量发展研究报告(2023年)

    内容概要:报告由中国信息通信研究院发布,旨在评估制造业上市公司高质量发展,强调制造业高质量发展的重要性,并构建了涵盖创新力、竞争力、影响力、贡献力四大维度的评价体系。通过对3500余家制造业上市公司2022年年报数据的综合评估,评选出百强企业。研究显示,百强企业专注主业,半数以上成长为制造业单项冠军;民营企业在盈利效率、创新发展方面表现优异;东部地区引领发展,装备制造业领先,新能源产业呈现爆发性增长。百强企业在科技创新、质效提升、持续增长、稳定就业等方面发挥重要作用,但也存在品牌建设和创新水平差距、领军企业竞争力提升空间、高端领域龙头企业培育不足等问题。 适用人群:制造业企业管理者、政策制定者、投资者及相关研究人员。 使用场景及目标:①帮助企业管理者了解行业发展趋势,提升企业竞争力;②为政策制定者提供决策参考,推动制造业高质量发展;③为投资者提供投资参考,识别优质企业;④为研究人员提供详实数据,助力学术研究。 其他说明:报告建议从重突破促升级、重创新补短板、重质量树品牌三个方面进一步推进制造业企业高质量发展,以加快建设具有全球竞争力的一流企业。

    异步电机无感矢量控制仿真:关键技术和代码实现技巧

    内容概要:本文详细介绍了异步电机无感矢量控制仿真的关键技术与常见问题解决方案。首先讨论了坐标变换(Clarke和Park变换)的基础操作及其注意事项,强调了正确选择系数的重要性。接下来深入探讨了滑模观测器的设计与优化方法,包括使用查表法替代三角函数计算以提高效率,以及加入低通滤波器减少高频抖振。此外,文章还涉及了速度估算的方法,如频域法和改进型滑模观测器的应用,并提供了具体的Python和Matlab代码片段。最后,针对电流环控制提出了前馈补偿机制,确保在突加负载情况下仍能保持良好的电流跟踪效果。文中多次提到调参技巧,特别是对于PI参数的选择给出了实用建议。 适合人群:从事电机控制系统研究与开发的技术人员,尤其是对异步电机无感矢量控制感兴趣的工程师。 使用场景及目标:适用于希望深入了解并掌握异步电机无感矢量控制仿真技术的研究人员和技术开发者。主要目标是在没有编码器的情况下实现对电机转速和扭矩的精确控制,同时提供详细的代码实现指导和调试经验。 其他说明:文章不仅提供了理论知识,还包括大量实际操作中的经验和教训,帮助读者避免常见的陷阱,快速搭建起有效的仿真环境。

    (源码)基于Arduino的火箭动力学参数监测项目.zip

    # 基于Arduino的火箭动力学参数监测项目 ## 项目简介 这是一个基于Arduino平台的火箭动力学参数监测项目,旨在通过Adafruit BMP280压力传感器和Adafruit LIS3DH加速度传感器收集火箭飞行过程中的环境数据和运动数据。项目结合了Adafruit的BMP280库和LIS3DH库,实现对传感器数据的读取、处理及初步分析。 ## 项目的主要特性和功能 1. 环境数据监测通过BMP280压力传感器,实时监测并记录火箭周围的气压、温度和海拔高度变化。 2. 运动数据监测借助LIS3DH加速度传感器,获取火箭在飞行过程中的加速度、速度及方向变化数据。 3. 数据处理与传输Arduino负责收集和初步处理这些数据,然后通过串行通信或其他方式将数据发送到地面站或飞行控制软件。 4. 安全与警报基于收集的数据,项目可设置警报阈值,当超过预设的安全限制时,触发警报或采取相应的安全措施。 ## 安装使用步骤

    (源码)基于Arduino的EPSleepy智能家居控制系统.zip

    # 基于Arduino的EPSleepy智能家居控制系统 ## 一、项目简介 EPSleepy是一个基于Arduino的智能家居控制系统原型。该项目旨在通过Arduino控制ESP32 WiFi和蓝牙板,结合MP3模块、shiftregister和按钮等硬件,实现智能家居的自动化控制。 ## 二、项目的主要特性和功能 1. 自动化控制通过Arduino代码控制ESP32板,实现家居设备的自动化控制。 2. 多种硬件支持支持MP3模块、shiftregister和按钮等硬件,实现音频播放、灯光控制、SD驱动等功能。 3. 模块化设计代码采用模块化设计,方便测试每个部分的功能,方便维护和调试。 4. 图形化界面可通过按钮和LED等硬件进行图形化操作和控制。 ## 三、安装使用步骤 1. 下载并解压项目源码文件。 2. 打开Arduino IDE,导入项目代码。 3. 连接硬件,包括ESP32板、MP3模块、shiftregister和按钮等。

    Delphi 12.3控件之PowerPDF for Delphi11 FullSource.zip

    Delphi 12.3控件之PowerPDF for Delphi11 FullSource.zip

    电动工具领域中微CMS32M5533 800W角磨机方案的硬件设计与反电动势检测算法详解

    内容概要:本文深入探讨了中微CMS32M5533在800W角磨机方案中的应用,涵盖硬件设计和软件实现的关键技术。硬件方面,介绍了三相桥驱动电路、MOSFET选择、电流检测电阻、PCB布局等细节;软件方面,重点讲解了反电动势检测算法、ADC采样时机、PWM配置以及换相时机的动态补偿。此外,还提供了调试技巧和成本控制方法。 适合人群:从事电动工具开发的技术人员,尤其是对电机控制有一定经验的研发人员。 使用场景及目标:适用于希望深入了解电动工具控制系统的设计和优化,特别是希望通过反电动势检测减少霍尔传感器使用的开发者。目标是提高系统的可靠性和性能,同时降低成本。 其他说明:文中提供的代码片段和硬件设计细节有助于实际项目的开发和调试。建议读者结合提供的GitHub资源进行实践,并关注硬件选型和PCB布局的注意事项。

    2004-2023年 上市公司CEO绿色经历

    CEO的绿色经历是指该首席执行官(CEO)在其个人职业发展过程中,所积累的与环境保护、可持续发展、绿色经济等相关的教育背景、工作经验或社会活动经验。 涵盖了教育背景、工作经验、社会活动与个人价值观等多个方面。这些经历不仅塑造了CEO对环境保护和可持续发展的认知和态度,还可能影响他们在企业决策中优先考虑环保因素的程度,从而对企业的长期发展和环境保护产生重要影响。 根据现有研究(姜付秀和黄继承,2013;许年行和李哲,2016),从高管个人简历数据中查找CEO以前是否接受过“绿色”相关教育或从事过“绿色”相关工作,若企业CEO具有绿色经历,Green取值1,否则,取值0。 数据 Stkcd、年份、D0801c、Green、股票简称、行业名称、行业代码、制造业取两位代码,其他行业用大类、当年ST或PT为1,否则为0、样本区间内ST或PT为1,否则为0、金融业为1,否则为0、制造业为1,否则为0、沪深A股为1,否则为0、第一种重污染行业为1,否则为0、第二种重污染行业为1,否则为0、第三种重污染行业为1,否则为0、产权性质,国企为1,否则为0、所属省份代码、所属城市代码、所在省份、所在地级市

    电动汽车18650电池组蛇形液冷系统的COMSOL多物理场仿真与优化

    内容概要:本文详细介绍了利用COMSOL Multiphysics对18650电池组进行蛇形液冷系统仿真的全过程。首先探讨了快充场景下电池过热的风险及其对电动车安全性和寿命的影响。接着,通过集总电池模型简化电化学反应,重点分析了电池产热方程和温度对产热的影响。随后,深入讨论了蛇形流道几何参数优化,如流道宽度与压降之间的非线性关系,以及流固交界面处理方法。此外,还涉及了多物理场耦合求解技巧,包括流场与传热模块的设置,以及后处理阶段的数据提取和可视化。最终得出优化设计方案,显著降低了电池组的最高温度和温度不均性。 适合人群:从事电动汽车电池管理系统设计的研究人员和技术工程师,尤其是熟悉COMSOL仿真工具的专业人士。 使用场景及目标:适用于需要评估和优化电动汽车电池组热管理系统的场合,旨在提高电池组的安全性和使用寿命,同时减少能量损耗。 其他说明:文中提供了大量具体的代码片段和参数设置建议,有助于读者快速上手并应用于实际工程项目中。

    通信领域CCSDS LDPC译码器设计:基于修正最小和算法的C语言与Vivado实现

    内容概要:本文详细介绍了CCSDS LDPC译码器的设计与实现,主要采用了修正最小和译码算法。该算法通过对传统最小和算法的改进,引入缩放因子α,提高了译码性能。文中具体讨论了(8176,7154)和(1280,1024)两种码组的应用场景及其优劣,并展示了如何通过C语言和Vivado进行仿真和硬件实现。此外,文章还探讨了硬件实现中的关键技术,如定点化处理、校验矩阵的压缩存储、动态阈值机制以及硬件流水线设计等。 适合人群:从事通信系统开发的研究人员和技术人员,尤其是对LDPC编码和译码感兴趣的工程师。 使用场景及目标:①帮助研究人员理解和实现CCSDS LDPC译码器;②为实际工程项目提供高效的译码解决方案;③提高译码性能,减少误码率,提升通信系统的可靠性和效率。 其他说明:文章不仅提供了理论分析,还包括了大量的代码示例和实践经验分享,有助于读者全面掌握CCSDS LDPC译码器的设计与实现。

    (源码)基于Arduino的超声波距离测量系统.zip

    # 基于Arduino的超声波距离测量系统 ## 项目简介 本项目是一个基于Arduino平台的超声波距离测量系统。系统包含四个超声波传感器(SPS)模块,用于测量与前方不同方向物体的距离,并通过蜂鸣器(Buzz)模块根据距离范围给出不同的反应。 ## 项目的主要特性和功能 1. 超声波传感器(SPS)模块每个模块包括一个超声波传感器和一个蜂鸣器。传感器用于发送超声波并接收回波,通过计算超声波旅行时间来确定与物体的距离。 2. 蜂鸣器(Buzz)模块根据超声波传感器测量的距离,蜂鸣器会给出不同的反应,如延时发声。 3. 主控制器(Arduino)负责控制和管理所有传感器和蜂鸣器模块,通过串行通信接收和发送数据。 4. 任务管理通过主控制器(Arduino)的 loop() 函数持续执行传感器任务(Task),包括测距、数据处理和蜂鸣器反应。 ## 安装使用步骤 1. 硬件连接

    主角跑步动作素材图包含6张图片

    主角跑步动作素材图包含6张图片

    2003-2023年 企业数字化转型测算结果

    企业数字化转型是指企业或组织将传统业务转化为数字化业务,利用人工智能、大数据、云计算、区块链、5G等数字技术提升业务效率和质量的过程。 当无形资产明细项包含“软件”“网络”“客户端”“管理系统”“智能平台”等与数字化转型技术相关的关键词以及与此相关的专利时,将该明细项目界定为“数字化技术无形资产”,再对同一公司同年度多项数字化技术无形资产进行加总,计算其占本年度无形资产的比例,即为企业数字化转型程度的代理变量。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 参考文献:张永珅,李小波,邢铭强-企业数字化转型与审计定价[J].审计研究,2021(03):62-71. 数据 证券代码、证券简称、统计截止日期、报表类型、无形资产净额、资产总计、年份、期末余额(元)、数字化转型。

    h5py-3.1.0-cp36-cp36m-win_amd64.whl

    该资源为h5py-3.1.0-cp36-cp36m-win_amd64.whl,欢迎下载使用哦!

    QRBayes-LSTM用于Excel数据的多/单变量时序预测及其应用

    内容概要:本文介绍了一种基于QRBayes-LSTM的多/单变量时序预测方法,适用于不确定性强的场景如股票预测和电力负荷预测。该方法结合了分位数回归和贝叶斯优化,不仅能提供未来的趋势预测,还能给出预测值的置信区间。文中详细解释了数据准备、模型结构、损失函数设计、训练配置以及预测结果的可视化和评估指标。此外,还提供了变量重要性分析的方法,帮助理解哪些特征对预测结果的影响最大。 适合人群:从事数据分析、机器学习研究的专业人士,尤其是关注时序预测和不确定性量化的人群。 使用场景及目标:① 对于需要进行时序预测并希望获得置信区间的用户;② 关注模型性能评估和变量重要性的研究人员;③ 寻求提高预测精度和可靠性的从业者。 其他说明:本文提供的代码可以直接应用于Excel格式的数据,用户只需将数据导入即可运行。需要注意的是,为了获得最佳效果,应该确保数据格式正确并且符合特定的要求。

    ADAS系统核心技术解析:ACC、FCW、AEB、LKA的设计与实现

    内容概要:本文详细介绍了ADAS(高级驾驶辅助系统)中四个主要功能模块的设计与实现,分别是自适应巡航控制系统(ACC)、前向碰撞预警系统(FCW)、自动紧急制动系统(AEB)和车道保持辅助系统(LKA)。文章不仅展示了各个系统的具体算法实现,如ACC中的PID控制、FCW中的TTC计算、AEB中的状态机设计和LKA中的PD控制器,还分享了许多实际开发中的经验和挑战,如参数调校、传感器融合、时间同步等问题。此外,文中还提到了一些有趣的细节,如在暴雨天气下LKA的表现优化,以及AEB系统在测试过程中遇到的各种corner case。 适合人群:汽车电子工程师、自动驾驶研究人员、嵌入式软件开发者。 使用场景及目标:帮助读者深入了解ADAS系统的工作原理和技术细节,掌握关键算法的实现方法,提高在实际项目中的开发和调试能力。 其他说明:文章通过生动的语言和具体的代码示例,使复杂的理论变得通俗易懂,有助于初学者快速入门并深入理解ADAS系统的开发流程。

    【高端制造业】2023年中国上市公司行业与区域分布分析:机械制造、电子、电力设备领头沿海地区优势明显

    内容概要:文章主要阐述了2023年中国高端制造业上市公司的发展概况,包括行业与区域两个维度的分布详情。从行业上看,高端制造业上市公司超过2400家,其中机械制造以628家的数量位居首位,电子(352家)和电力制造(336家)紧随其后,而像航空航天国防等也有一定的占比。从区域分布来看,广东、江苏、浙江三省处于领先地位,分别有410家、342家和199家,这表明东南沿海地区对于高端制造业的发展具有显著优势。数据来源于中国上市公司协会以及Wind。 适合人群:对中国经济结构、产业发展趋势感兴趣的读者,尤其是关注高端制造业发展的投资者、政策制定者及研究人员。 使用场景及目标:①帮助投资者了解中国高端制造业上市公司的行业布局,为投资决策提供参考依据;②为政策制定者提供数据支持,助力优化产业布局和发展规划;③供研究人员分析中国高端制造业的现状与未来发展趋势。 阅读建议:本文提供了丰富的数据和图表,读者应重点关注各行业的具体数据及其背后反映出的产业特点,同时结合区域分布情况,深入理解中国高端制造业的发展格局。

Global site tag (gtag.js) - Google Analytics