java消息服务(简称JMS)是用于访问企业消息系统的开发商的API,可以概括为应用程序A发送一条消息到消息服务器的某个目的地,然后消息服务器将消息发送给监听这个目的地的程序B,JMS支持两种消息传递模型,点对点(简称PTP)和发布/订阅(简称pub/sub),PTP规定一条消息只能发送给一个接收方,pub/sub允许一条消息发送给多个接收方,以下是个JMS的小例子
1.首先我们需要配置消息到达的目的地址,命名规范为*-service.xml,每个应用服务器的配置都不同,我用的是JBoss,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=liaokangQueue">
<attribute name="JNDIName">queue/liaokangQueue</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=liaokangTopic">
<attribute name="JNDIName">topic/liaokangTopic</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
</server>
将其部署在server\default\deploy目录下
2.新建一个java项目,并将jboss的client目录下的jar文件全部引入,新建一个类来向目的地发送消息,代码如下,这是Queue消息
package com.lamp.app;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueSender {
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
QueueConnection conn = factory.createQueueConnection();
QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) ctx.lookup("queue/liaokangQueue");
MessageProducer producer = session.createProducer(destination);
producer.send(session.createTextMessage("欢迎来到消息驱动的世界,这是queue消息"));
session.close();
conn.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
当然不要忘记在src目录下新建JNDI的配置文件jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
然后采用消息驱动bean来接受消息
package com.lamp.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/liaokangQueue")
})
public class MessageDriverBean implements MessageListener{
public void onMessage(Message message) {
TextMessage tmsg = (TextMessage)message;
try {
System.out.println(tmsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
以下是Topic消息的发送
Topic消息发送类
package com.lamp.app;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TopicSender {
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
TopicConnectionFactory factory = (TopicConnectionFactory) ctx
.lookup("TopicConnectionFactory");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = conn.createTopicSession(false,
TopicSession.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) ctx
.lookup("topic/liaokangTopic");
MessageProducer producer = session.createProducer(destination);
producer.send(session.createTextMessage("欢迎来到消息驱动的世界,这是topic消息"));
session.close();
conn.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
topic消息类型接收类
package com.lamp.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/liaokangTopic")
})
public class TopicReceiver implements MessageListener {
public void onMessage(Message message) {
TextMessage tmsg = (TextMessage)message;
try {
System.out.println(tmsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
不同Queue的是你可以新建多个消息接受类来接受同一个目的地Topic类型的消息
最后将工程打包成jar,部署到JBossserver\default\deploy目录下,在发送方类中运行main方法