续上篇介绍了第七个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程序控制台看到如下输出:
上述便是ESB第八个应用实例。如有问题,欢迎指正。
-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Mar 11st, 2011
-----------------------------------------------------