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

JBoss ESB学习笔记9——第八个ESB应用JMS Router

阅读更多

续上篇介绍了第七个ESB应用,本文介绍第八个ESB应用——JMS Router。

 

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

 

1 概述
该实例演示如何配置JMSRouter action类来实现消息的路由和转发,以及JMSCorrelationID在ESB中的使用。

 

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


3 ESB配置
3.1 创建消息队列

本例中只用到了四个消息队列,其中:

jmsrouterRequestGw:用于接收外部消息

jmsrouterRequestEsb:用于接收ESB内部消息

jmsrouterRouteTo:用于接收JMSRouter 转发的消息

jmsrouterReplyTo:用于客户端转发消息

如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<server>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=jmsrouterRequestGw" 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=jmsrouterRequestEsb" 	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=jmsrouterRouteTo" 	xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
		<attribute name="SecurityConfig">
			<security>
				<role name="guest" read="true" write="true" />
			</security>
		</attribute>
	</mbean>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=jmsrouterReplyTo" 	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,分别用于监听外部消息和ESB消息,内容如下:

<providers>
	<jms-provider connection-factory="ConnectionFactory"
		name="JBossMessaging">
		<jms-bus busid="jmsrouterGwChanel">
			<jms-message-filter dest-name="queue/jmsrouterRequestGw"
				dest-type="QUEUE" />
		</jms-bus>
		<jms-bus busid="jmsrouterEsbChanel">
			<jms-message-filter est-name="queue/jmsrouterRequestEsb"
				dest-type="QUEUE" />
		</jms-bus>
	</jms-provider>
</providers>

 

3.3 定义Service

<service category="JmsRouter" description="Jms Router"
name="JmsRouterService">
	<listeners />
	<actions /
</service>

 

3.4 定义Listener
这里定义两个listener,分别用于监听外部消息和ESB消息,内容如下:

<listeners>
	<jms-listener busidref="jmsrouterGwChanel" is-gateway="true"
		name="jmsrouterGwListener" />
	<jms-listener busidref="jmsrouterEsbChanel"
name="jmsrouterEsbListener" />
</listeners>

 

3.5 配置Action

<actions mep="OneWay">
<action class="org.jboss.soa.esb.actions.SystemPrintln"
name="printMessage">
		<property name="message" value="JMS Router Message" />
		<property name="printfull" value="false" />
	</action>
	<action class="org.jboss.soa.esb.actions.routing.JMSRouter"
		name="routerToReplyQueue">
		<property name="jndiName" value="queue/jmsrouterRouteTo" />
		<property name="connection-factory"
value="ConnectionFactory" />
		<property name="unwrap" value="true" />
		<property name="security-principal" value="guest" />
		<property name="security-credential" value="guest" />
		<property name="jndi-prefixes" value="org.xyz" />
		<property name="org.xyz.propertyName" value="propertyValue" />
		<property name="java.naming.someproperty"
value="propertyValue" />
	</action>
</actions>

配置说明:这里定义了两个action,第一个是JBoss ESB提供的action类,用于打印输出消息内容。第二个action是一个JMS消息路由器,它将接收到的消息转发到消息队列queue/jmsrouterRouteTo,客户端可从该消息队列获取经转发后的消息。 JMSRouter的其他属性说明如下,摘自JBoss ESB API,英文比较简单,懒得翻译了。

unwrap: 'true' will extract the message payload from the Message object before sending. false (default) will send the serialized Message object.
jndi-context-factory: The JNDI context factory to use. Default is "org.jnp.interfaces.NamingContextFactory"
jndi-URL: The JNDI URL to use. Default is "127.0.0.1:1099"
jndi-pkg-prefix: The JNDI naming package prefixes to use. Default is "org.jboss.naming:org.jnp.interfaces".
connection-factory: The name of the ConnectionFactory to use. Default is "ConnectionFactory".
persistent: The JMS DeliveryMode. 'true' or 'false'. Default is "true".
priority: The JMS Priority to be used. Default is "javax.jms.Message.DEFAULT_PRIORITY"
time-to-live: The JMS Time-To-Live to be used. Default is "javax.jms.Message.DEFAULT_TIME_TO_LIVE"
security-principal: Security principal use when creating the JMS connection.
security-credential: The security credentials to use when creating the JMS connection.
property-strategy: The implementation of the JMSPropertiesSetter interface, if overriding the default.
message-prop: Properties to be set on the message are prefixed with "message-prop-".
jndi-prefixes: A comma separated string of prefixes. Properties that have these prefixes will be added to the JNDI environment.
org.xyz.propertyName: A jndi environment property that will be added to the jndi environment if the prefix 'org.xyz' was specified in the jndi-prefixes list.

 

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

<jbossesb-deployment>
	<depends>		jboss.esb.quickstart.destination:service=Queue,name=jmsrouterRequestGw
	</depends>
	<depends>		jboss.esb.quickstart.destination:service=Queue,name=jmsrouterRequestEsb
	</depends>
	<depends>		jboss.esb.quickstart.destination:service=Queue,name=jmsrouterRouteTo
	</depends>
</jbossesb-deployment>

 

3.7 部署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.JmsRouterClient.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.Calendar;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Class Name: JmsRouterClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-12
 * @version 1.0
 */
public class JmsRouterClient
{
	private Connection connection;
	private Session session;
	private Destination gatewayDestination;
    private Destination responseDestination;
    private Destination replyToDestination;
    private String correlationId;
    
    private String propertyKey = "MyProperty";
    
    public void setupConnection(String destination) 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);
		
		ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
		connection = factory.createConnection();
		gatewayDestination = (Destination) context.lookup("queue/jmsrouterRequestGw");
    	responseDestination = (Destination)context.lookup(destination);
    	replyToDestination = (Destination) context.lookup("queue/jmsrouterReplyTo");
    	session = connection.createSession(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
	{
    	correlationId = "QuickstartId[" + Calendar.getInstance().get(Calendar.SECOND) + "]";
    	MessageProducer producer = session.createProducer(gatewayDestination);
    	ObjectMessage objectMsg = session.createObjectMessage(text);
    	objectMsg.setJMSCorrelationID( correlationId );
    	objectMsg.setJMSReplyTo( replyToDestination );
    	objectMsg.setStringProperty(propertyKey, "My property value");
    	producer.send(objectMsg);
    	System.out.println("Sent message with CorrelationID : " + correlationId );
    	System.out.println("");
        producer.close();
	}
    
    public void receiveMessage() throws Exception
    {
    	MessageConsumer consumer = session.createConsumer(responseDestination, "JMSCorrelationID = '" + correlationId + "'");
    	Message message = consumer.receive();
    	System.out.println("Received from " + responseDestination + ":");
        System.out.println("\t[JMSMessageID : " +  message.getJMSMessageID() + "]" );
		System.out.println("\t[JMSCorrelelationID : " +  message.getJMSCorrelationID() + "]" );
		System.out.println("\t[JMSReplyto : " +  message.getJMSReplyTo() + "]" );
		if(message instanceof ObjectMessage)
		{
			System.out.println("\t[MessageType : ObjectMessage]");
    		System.out.println( "\t[Object : " +  ((ObjectMessage)message).getObject() + "]" );
		}
		else if (message instanceof TextMessage)
		{
			System.out.println("\t[MessageType : TextMessage]");
    		System.out.println( "\t[Text : " +  ((TextMessage)message).getText() + "]" );
		}
		System.out.println("\t[Property: "+ propertyKey + " : " +  message.getStringProperty(propertyKey) + "]" );
		
		consumer.close();
    }
	
	/**
	 * <p>Title: </p>
	 * <p>Method Name: main</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: 2010-9-12
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		JmsRouterClient client = new JmsRouterClient();
		client.setupConnection("queue/jmsrouterRouteTo");
		client.sendAMessage("Llu, miss you, afa - jms router");
		client.receiveMessage();
		client.stop();
	}

}

 

运行客户端程序,可分别在JBoss ESB服务器控制台和Java程序控制台看到如下输出:

JBoss ESB

 

JBoss ESB

 

上述便是ESB第八个应用实例。如有问题,欢迎指正。

 

 

-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Mar 11st, 2011
-----------------------------------------------------

分享到:
评论
2 楼 apollo211 2012-11-24  
确实  为何route后 JMSReplyTo丢失了。。。
1 楼 gccjava 2012-09-19  
为何Route后JMSReplyTo丢失了为NULL

相关推荐

    JBOSS_Esb学习资料

    7. **JMS路由**:《JBoss_ESB学习笔记9——第八个ESB应用JMS_Router.doc》和《JBoss_ESB学习笔记10——第九个ESB应用JMS_Topic.doc》涉及到了JMS(Java Message Service)在ESB中的应用,讲解了如何利用ESB进行消息...

    JBoss ESB 学习笔记

    #### 九、第八个ESB应用JMS Router - **重点**:学习如何使用JMS Router组件实现消息路由。 - **实现**:配置JMS Router,使其能够根据规则将消息路由到不同的目的地。 #### 十、第九个ESB应用JMS Topic - **重点...

    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学习笔记

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

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

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

    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等,并进行版本...

    ESB应用JMS_Router

    【ESB应用JMS_Router】是JBoss ESB中的一个重要功能,用于实现基于Java Message Service (JMS) 的消息路由和转发。JMS Router允许系统根据预定义的规则将消息从一个消息队列转发到另一个队列,从而实现消息在不同...

    jboss esb 实例

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

    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程序员指南

    10. 第八章 Web Services编排 53 10.1. WS-BPEL 53 11. 第九章 服务日程安排 54 11.1. 介绍 54 11.2. 简单日程表 54 11.3. Cron日程表 54 11.4. 日程监听器 55 11.5. 配置举例 55 11.6. Quartz日程调度器的属性配置 ...

    JBoss ESB 例子

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

    JBOSSESB学习小结

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

    JBoss ESB 开发指南

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

Global site tag (gtag.js) - Google Analytics