续上篇介绍了第二个ESB应用,本文介绍第三个ESB应用——Hello World Notification。
说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com,谢谢合作。
1 概述
该实例主要演示了Notifier的使用,当ESB接收到来自客户端的消息后,自动向指定的地址发送通知,这里的地址可以是控制台,文件,消息队列或者消息主题。
在下面的例子中,需要定义两个消息队列,一个接收来自客户端的消息,第二个用作ESB内部Message通道。此外,还有一个消息主题,用于接收ESB发来的通知。
2 新建ESB工程
操作过程略。
3 ESB配置
3.1 创建消息队列和消息主题
如概述中所描述,这里将创建两个消息队列和一消息主题。在esbcontent文件夹下创建文件jbm-queue-service.xml用于配置消息队列,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.esb.helloworld.destination:service=Queue,name=helloworldNofityEsb" 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.helloworld.destination:service=Queue,name=helloworldNofityGw" 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.TopicService" name="jboss.esb.helloworld.destination:service=Topic,name=helloworldTopic" xmbean-dd="xmdesc/Topic-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer </depends> <attribute name="SecurityConfig"> <security> <role name="guest" read="true" write="true" create="true" /> </security> </attribute> </mbean> </server>
3.2 定义Provider
这里将定义一个JMS Provider,同时定义两个消息通道,一个用于监听客户端发来的消息,另一个用作ESB消息通道。内容如下:
<jms-provider connection-factory="ConnectionFactory" name="JBossMQ"> <jms-bus busid="helloworldNotifyChanel"> <jms-message-filter dest-name="queue/helloworldNofityGw" dest-type="QUEUE" /> </jms-bus> <jms-bus busid="helloworldNotifyEsbChanel"> <jms-message-filter dest-name="queue/helloworldNofityEsb" dest-type="QUEUE" /> </jms-bus> </jms-provider>
3.3 定义Service
<service category="HelloWorldNotify" description="Hello World Notify" name="HelloWorldNotifyService"> <listeners /> <actions / </service>
3.4 定义Listener
这里定义两个listener,分别用于监听客户端消息和ESB内部消息。其中,前者用于监听客户端消息,因此需要将其is-gateway属性设置为true。
<jms-listener busidref="helloworldNotifyChanel" is-gateway="true" name="helloWorldNotifier" /> <jms-listener busidref="helloworldNotifyEsbChanel" name="helloWorldNotifierEsb" />
3.5 配置Action
<actions mep="OneWay"> <action class="org.jboss.soa.esb.actions.Notifier" name="notificationAction"> <property name="okMethod" value="notifyOK" /> <property name="notification-details"> <NotificationList type="ok"> <target class="NotifyConsole" /> <target class="NotifyFiles"> <file URI="file:///F:\Workspace\Eclipse-JEE\helloworldtopicnitify\test.txt" append="true" /> </target> <target class="NotifyTopics"> <topic jndiName="topic/helloworldTopic" /> </target> </NotificationList> </property> </action> </actions>
配置说明:这里的action调用JBoss ESB提供的Notifier类来实现处理逻辑。这里的通知目的地有两个,一个是文件系统,另一个是消息主题topic/helloworldTopic。其中,对于文件系统通知,append属性表示是否在接收到来自客户端的消息后,将消息内容追加到文件的末尾。
3.6 配置部署文件
部署依赖文件deployment.xml内容如下:
<jbossesb-deployment> <depends>jboss.esb.helloworld.destination:service=Queue,name=helloworldNofityGw </depends> <depends>jboss.esb.helloworld.destination:service=Topic,name=helloworldTopic </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.HelloWorldNotifyClient.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.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: HelloWorldNotifyClient</p>
* <p>Description: </p>
* @author Afa
* @date 2010-9-9
* @version 1.0
*/
public class HelloWorldNotifyClient
{
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/helloworldRequest");
queue = (Queue) context.lookup("queue/helloworldNofityGw");
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 static void main(String[] args) throws Exception
{
HelloWorldNotifyClient client = new HelloWorldNotifyClient();
client.setupConnection();
client.sendAMessage("Llu, miss you, notify, afa");
client.stop();
}
}
4.3 接收消息的客户端
/***********************************************************************
* <p>Project Name: helloworldclient</p>
* <p>File Name: com.thu.afa.esb.jbossesb.client.HelloWorldNotifyReceiverClient.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.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* <p>Class Name: HelloWorldNotifyReceiverClient</p>
* <p>Description: </p>
* @author Afa
* @date 2010-9-9
* @version 1.0
*/
public class HelloWorldNotifyReceiverClient
{
private TopicConnection connection;
private TopicSession session;
private Topic topic;
private TopicConnectionFactory factory;
public void setup() 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://localhost:1099");
InitialContext context = new InitialContext(properties);
factory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
topic = (Topic) context.lookup("topic/helloworldTopic");
connection = factory.createTopicConnection("guest", "guest");
connection.setClientID("clientid");
session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
connection.start();
}
public void receiveMessage() throws Exception
{
TopicSubscriber subscriber = session.createSubscriber(topic);
Message message = subscriber.receive();
if(message instanceof TextMessage)
{
TextMessage textMessage = (TextMessage) message;
System.out.println("Notification Infomation: " + textMessage.getText());
}
subscriber.close();
}
public void stop() throws Exception
{
if(connection != null) connection.stop();
if(session != null) session.close();
if(connection != null) connection.close();
}
/**
* <p>Title: </p>
* <p>Method Name: main</p>
* <p>Description: </p>
* @author: Afa
* @date: 2010-9-9
* @param args
*/
public static void main(String[] args) throws Exception
{
HelloWorldNotifyReceiverClient client = new HelloWorldNotifyReceiverClient();
client.setup();
client.receiveMessage();
client.stop();
}
}
首先运行接收消息的客户端,然后运行发送消息的客户端,即可在F:\Workspace\Eclipse-JEE\helloworldtopicnitify\test.txt文件中看到客户端发来的消息内容。另一方面,由于已经运行了接收消息的客户端,所以可以同时在该客户端的控制台看到通知信息,如下图所示,其中,文本文件中有三条来自客户端的消息(执行了三次发消息操作),这是在配置ESB时由append属性指定的。
上述便是ESB第三个应用实例。如有问题,欢迎指正。
-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Jan 3rd, 2011
-----------------------------------------------------