开发消息驱动bean(Queue/Topic)
1.配置目标地址(cmcc-service.xml),Jboss使用一个XML文件配置队列地址,文件的取名格式应遵守*-service.xml
2.编写服务器消息的发送者(QueueSender,TopicSender)
3.编写客户端接收者(QueueMessageDriverBean,TopicMessageDriverBean1,TopicMessageDriverBean2)
4.把QueueSender\TopicSender\QueueMessageDriverBean\TopicMessageDriverBean1\TopicMessageDriverBean2这几个java文件打成jar(MessageDriverBean.jar)包发布到jboss下,jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\deploy下
/*
QueueSender是PTP的发送者
TopicSender是pub/sub的发送者
QueueMessageDriverBean 是PTP的接收者,点对点(point-to-point,简称 PTP)
TopicMessageDriverBean1、TopicMessageDriverBean2是 pub/sub的接收者,发布/订阅(publish/subscribe,简称 pub/sub)
其名称由“queue”+“/”+目标地址名称组成。另外在任何队列或主题被部署之前,应用服务器必须先部署Destination Manager Mbean
,所以我们通过
*/
cmcc-service.xml
name="jboss.mq.destination:service=Queue,name=cmccQueue">
name="jboss.mq.destination:service=Topic,name=cmccTopic">
QueueSender.java
package cn.cmcc.app;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
public class QueueSender {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
//这个name到http://localhost:8080/jmx-console中的JMX MBean View选项中的list()方法--》Global JNDI Namespace下查找
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
QueueConnection conn = factory.createQueueConnection();
QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
//这个name就是cmcc-service.xml中的JNDIName节点配置的名字
Destination destination = (Destination ) ctx.lookup("queue/cmccQueue");
MessageProducer producer = session.createProducer(destination);
TextMessage msg = session.createTextMessage("您好,这是我的第一个消息驱动queue Bean");
producer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
TopicSender.java
package cn.cmcc.app;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
public class TopicSender {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
//这个name到http://localhost:8080/jmx-console中的JMX MBean View选项中的list()方法--》Global JNDI Namespace下查找
TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
//这个name就是cmcc-service.xml中的JNDIName节点配置的名字
Destination destination = (Destination ) ctx.lookup("topic/cmccTopic");
MessageProducer producer = session.createProducer(destination);
TextMessage msg = session.createTextMessage("您好,这是我的第一个消息驱动Topic Bean");
producer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
QueueMessageDriverBean.java
package cn.cmcc.message;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="queue/cmccQueue"), //这个propertyValue就是cmcc-service.xml中的JNDIName节点配置的名字
@ActivationConfigProperty(propertyName="acknowledgeMode",
propertyValue="Auto-acknowledge")
})
public class QueueMessageDriverBean implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage msg=(TextMessage)message;
System.out.println(msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
TopicMessageDriverBean1.java
package cn.cmcc.message;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="topic/cmccTopic"), //这个就是cmcc-service.xml中的JNDIName节点配置的名字
@ActivationConfigProperty(propertyName="acknowledgeMode",
propertyValue="Auto-acknowledge")
})
public class TopicMessageDriverBean1 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage msg=(TextMessage)message;
System.out.println(msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
TopicMessageDriverBean2.java
package cn.cmcc.message;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="topic/cmccTopic"), //这个就是cmcc-service.xml中的JNDIName节点配置的名字
@ActivationConfigProperty(propertyName="acknowledgeMode",
propertyValue="Auto-acknowledge")
})
public class TopicMessageDriverBean2 implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage msg=(TextMessage)message;
System.out.println(msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
输出结果是
运行QueueSender: cn.cmcc.message.QueueMessageDriverBean您好,这是我的第一个消息驱动queue Bean
运行TopicSender:
cn.cmcc.message.TopicMessageDriverBean1您好,这是我的第一个消息驱动Topic Bean
cn.cmcc.message.TopicMessageDriverBean2您好,这是我的第一个消息驱动Topic Bean