service=Queue,name=jmsrouterRequestEsb"xmbean-dd="xmdesc/Queue-xmbean.xml">
10
11jboss.messaging:
service=ServerPeer
12
13jboss.messaging:
service=PostOffice
14
15service=Queue,name=jmsrouterRouteTo"xmbean-dd="xmdesc/Queue-xmbean.xml">
16
17jboss.messaging:
service=ServerPeer
18
19jboss.messaging:
service=PostOffice
20
21
22
23
24
25
26service=Queue,name=jmsrouterReplyTo"xmbean-dd="xmdesc/Queue-xmbean.xml">
27
28jboss.messaging:
service=ServerPeer
29
30jboss.messaging:
service=PostOffice
31
32
3.2定义Provider
这里将定义两个JMSProvider,分别用于监听外部消息和ESB消息,内容如下:
Xml代码
33
3435name="JBossMessaging">
36
3738dest-type="QUEUE"/>
39
40
4142dest-type="QUEUE"/>
43
44
45
3.3定义Service
Xml代码
4647name="JmsRouterService">
48
4950
3.4定义Listener
这里定义两个listener,分别用于监听外部消息和ESB消息,内容如下:
Xml代码
51
5253name="jmsrouterGwListener"/>
5455name="jmsrouterEsbListener"/>
56
3.5配置Action
Xml代码
57
5859name="printMessage">
60
61
62
6364name="routerToReplyQueue">
65
6667value="ConnectionFactory"/>
68
69
70
71
72
7374value="propertyValue"/>
75
76
配置说明:
这里定义了两个action,第一个是JBossESB提供的action类,用于打印输出消息内容。
第二个action是一个JMS消息路由器,它将接收到的消息转发到消息队列queue/jmsrouterRouteTo,客户端可从该消息队列获取经转发后的消息。
JMSRouter的其他属性说明如下,摘自JBossESBAPI,英文比较简单,懒得翻译了。
unwrap:
'true'willextractthemessagepayloadfromtheMessageobjectbeforesending.false(default)willsendtheserializedMessageobject.
jndi-context-factory:
TheJNDIcontextfactorytouse.Defaultis"org.jnp.interfaces.NamingContextFactory"
jndi-URL:
TheJNDIURLtouse.Defaultis"127.0.0.1:
1099"
jndi-pkg-prefix:
TheJNDInamingpackageprefixestouse.Defaultis"org.jboss.naming:
org.jnp.interfaces".
connection-factory:
ThenameoftheConnectionFactorytouse.Defaultis"ConnectionFactory".
persistent:
TheJMSDeliveryMode.'true'or'false'.Defaultis"true".
priority:
TheJMSPrioritytobeused.Defaultis"javax.jms.Message.DEFAULT_PRIORITY"
time-to-live:
TheJMSTime-To-Livetobeused.Defaultis"javax.jms.Message.DEFAULT_TIME_TO_LIVE"
security-principal:
SecurityprincipalusewhencreatingtheJMSconnection.
security-credential:
ThesecuritycredentialstousewhencreatingtheJMSconnection.
property-strategy:
TheimplementationoftheJMSPropertiesSetterinterface,ifoverridingthedefault.
message-prop:
Propertiestobesetonthemessageareprefixedwith"message-prop-".
jndi-prefixes:
Acommaseparatedstringofprefixes.PropertiesthathavetheseprefixeswillbeaddedtotheJNDIenvironment.
org.xyz.propertyName:
Ajndienvironmentpropertythatwillbeaddedtothejndienvironmentiftheprefix'org.xyz'wasspecifiedinthejndi-prefixeslist.
3.6配置部署文件
部署依赖文件deployment.xml内容如下:
Xml代码
77
78jboss.esb.quickstart.destination:
service=Queue,name=jmsrouterRequestGw
79
80jboss.esb.quickstart.destination:
service=Queue,name=jmsrouterRequestEsb
81
82jboss.esb.quickstart.destination:
service=Queue,name=jmsrouterRouteTo
83
84
3.7部署ESB
将整个工程导出成一个ESB文件,并保存至JBossESBServer的部署目录下,启动JBossESBServer即可。
4ESB客户端
4.1新建Java工程
这里略去操作过程以及添加所需要的Jar包,具体操作过程可参考第一个ESB实例说明。
4.2客户端实现
Java代码
85/***********************************************************************
86*
ProjectName:
helloworldclient
87*
FileName:
com.thu.afa.esb.jbossesb.client.JmsRouterClient.java
88*
Copyright:
Copyright(c)2010
89*
Company:
90***********************************************************************/
91packagecom.thu.afa.esb.jbossesb.client;
92
93importjava.util.Calendar;
94importjava.util.Properties;
95
96importjavax.jms.Connection;
97importjavax.jms.ConnectionFactory;
98importjavax.jms.Destination;
99importjavax.jms.Message;
100importjavax.jms.MessageConsumer;
101importjavax.jms.MessageProducer;
102importjavax.jms.ObjectMessage;
103importjavax.jms.QueueSession;
104importjavax.jms.Session;
105importjavax.jms.TextMessage;
106importjavax.naming.Context;
107importjavax.naming.InitialContext;
108
109/**
110*
ClassName:
JmsRouterClient
111*
Description:
112*@authorAfa
113*@date2010-9-12
114*@version1.0
115*/
116publicclassJmsRouterClient
117{
118privateConnectionconnection;
119privateSessionsession;
120privateDestinationgatewayDestination;
121privateDestinationresponseDestination;
122privateDestinationreplyToDestination;
123privateStringcorrelationId;
124
125privateStringpropertyKey="MyProperty";
126
127publicvoidsetupConnection(Stringdestination)throwsException
128{
129Propertiesproperties=newProperties();
130properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
131properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:
org.jnp.interfaces");
132properties.put(Context.PROVIDER_URL,"jnp:
//127.0.0.1:
1099");
133InitialContextcontext=newInitialContext(properties);
134
135ConnectionFactoryfactory=(ConnectionFactory)context.lookup("ConnectionFactory");
136connection=factory.createConnection();
137gatewayDestination=(Destination)context.lookup("queue/jmsrouterRequestGw");
138responseDestination=(Destination)context.lookup(destination);
139replyToDestination=(Destination)context.lookup("queue/jmsrouterReplyTo");
140session=connection.createSession(false,QueueSession.AUTO_ACKNOWLEDGE);
141connection.start();
142
143System.out.println("ConnectionStarted");
144}
145
146publicvoidstop()throwsException
147{
148if(connection!
=null)connection.stop();
149if(session!
=null)session.close();
150if(connection!
=null)connection.close();
151}
152
153publicvoidsendAMessage(Stringtext)throwsException
154{
155correlationId="QuickstartId["+Calendar.getInstance().get(Calendar.SECOND)+"]";
156MessageProducerproducer=session.createProducer(gatewayDestination);
157ObjectMessageobjectMsg=session.createObjectMessage(text);
158objectMsg.setJMSCorrelationID(correlationId);
159objectMsg.setJMSReplyTo(replyToDestination);
160objectMsg.setStringProperty(propertyKey,"Mypropertyvalue");
161producer.send(objectMsg);
162System.out.println("SentmessagewithCorrelationID:
"+correlationId);
163System.out.println("");
164producer.close();
165}
166
167publicvoidreceiveMessage()throwsException
168{
169MessageConsumerconsumer=session.createConsumer(responseDestination,"JMSCorrelationID='"+correlationId+"'");
170Messagemessage=consumer.receive();
171System.out.println("Receivedfrom"+responseDestination+":
");
172System.out.println("\t[JMSMessageID:
"+message.getJMSMessageID()+"]");
173System.out.println("\t[JMSCorrelelationID:
"+message.getJMSCorrelat