`
yarafa
  • 浏览: 88656 次
  • 性别: 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学习资料

    6. **数据转换**:《JBoss_ESB学习笔记14——第十三个ESB应用Transform_CSV_to_XML.doc》展示了如何使用ESB进行数据格式转换,例如从CSV转为XML,这是在集成不同系统时经常遇到的需求。 7. **JMS路由**:《JBoss_...

    JBoss ESB 学习笔记

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

    JBossESB学习笔记(1-16全)

    JBossESB学习笔记 收集了网上1-16系列教程,笔记详细介绍了JBossESB各个组件的特性及配置文件的说明

    JBossESB学习笔记.rar_Jboss_ESB_esb和aop

    【JBoss ESB 学习笔记】 JBoss ESB(Enterprise Service Bus,企业服务总线)是Red Hat公司开发的一款开源服务导向架构(SOA)平台,它为分布式应用程序提供了集成和互操作性。本笔记将深入探讨JBoss ESB的核心概念...

    JBoss ESB学习笔记1-搭建ESB开发环境.docx

    JBoss ESB(Enterprise Service Bus,企业服务总线)是Red Hat公司提供的一个开源中间件,用于构建服务导向架构(SOA)。它提供了一个平台,让不同系统和服务之间能够通过标准接口进行通信,实现了服务之间的解耦合...

    Jboss_ESB学习笔记

    【JBoss ESB学习笔记】 JBoss ESB(Enterprise Service Bus)是Red Hat公司开发的一款开源企业服务总线,它是企业级应用集成的核心组件,用于连接不同系统、服务和应用程序,实现服务之间的通信和交互。本学习笔记...

    ESB应用Transform_XML_to_POJO

    ESB应用Transform_XML_to_POJO

    jbossesb开发手册

    JBoss ESB(Enterprise Service Bus)是一款开源的企业服务总线解决方案,旨在为应用程序之间的通信提供一个灵活且可扩展的基础架构。本手册旨在帮助初学者理解JBoss ESB的基本概念、搭建开发环境以及进行简单的开发...

    JBoss ESB新手指南

    总之,《JBoss ESB新手指南》这本书将带领读者逐步了解和掌握这个强大的中间件平台,从基础概念到实战技巧,全面解析JBoss ESB在企业级集成中的应用。通过深入学习,新手可以快速成长为能够熟练运用JBoss ESB解决...

    Jboss-ESB学习笔记.doc

    本篇学习笔记主要围绕 JBoss ESB 的一个基础应用——“Hello World File Action”进行讲解,这个例子展示了如何利用 JBoss ESB 的 File Gateway 功能来监控文件系统变化,并通过 JMS(Java Message Service)消息...

    JbossESB4.6 程序开发

    在构建基于JBoss ESB 4.6的应用程序时,SOA(面向服务的架构)平台配置是至关重要的第一步。正确的配置能够确保服务之间高效稳定地交互。 - **环境搭建**:安装必要的软件包,如Java环境、JBoss ESB等,并进行版本...

    jboss esb 实例

    jboss esb 实例及讲解,我收集的资料整理做成书

    JBOSS ESB程序员指南

    1. 目录 1. 目录 2 2. 关于本指南 5 2.1. 本指南包括什么 5 2.2. 读者 5 2.3. 必要知识 5 2.4. 本指南的组织结构 5 2.5. 关于本文档的协定 5 2.6. 附件 6 2.7. 我们的联系方式 6 ...13. 第十一章 术语表 67

    JbossESB开发环境配置.docx

    JBoss ESB 4.9需要一个特定的服务器环境,你可以从官方网站下载D:\jboss-5.1.0.GA/jbossesb-server,这是一个包含大量示例的独立运行版本。这些示例可以通过Ant脚本进行部署和测试: - 使用`ant deploy`命令来部署...

    Jboss ESB简介及开发实例

    一、Jboss ESB的简介 1、 什么是ESB。 ESB的全称是Enterprise Service Bus,即企业服务总线。ESB是过去消息中间件的发展,ESB采用了“总线”这样一种模式来管理和简化应用之间的集成拓扑结构,以广为接受的开放...

    JBoss ESB 例子

    JBoss ESB 入门例子。主要是一个Server和一个Client。

    JBoss ESB 开发指南

    JBoss ESB(Enterprise Service Bus)是JBoss中间件项目的一个重要组成部分,主要功能在于提供一个灵活、可扩展的平台,用于构建和部署企业级集成解决方案。它支持多种消息传输协议和数据格式,能够处理复杂的消息流...

    JBOSSESB学习小结

    NULL 博文链接:https://siye1982.iteye.com/blog/592400

Global site tag (gtag.js) - Google Analytics