JMS(Java Message Service) 即Java消息服务。它提供标准的产生、发送、接收消息的接口简化企业应用的开发。它支持两种消息通信模型:点到点(point-to-point)(P2P)模型和发布/订阅(Pub/Sub)模型。P2P模型规定了一个消息只能有一个接收者;Pub/Sub 模型允许一个消息可以有多个接收者。
对于点到点模型,消息生产者产生一个消息后,把这个消息发送到一个Queue(队列)中,然后消息接收者再从这个Queue中读取,一旦这个消息被一个接收者读取之后,它就在这个Queue中消失了,所以一个消息只能被一个接收者消费。与点到点模型不同,发布/订阅模型中,消息生产者产生一个消息后,把这个消息发送到一个Topic中,这个Topic可以同时有多个接收者在监听,当一个消息到达这个Topic之后,所有消息接收者都会收到这个消息。
topic模式实现过程:
1.在WebLogic中配置JMS服务,新建一个JMS服务器,然后在JMS模板中新建工厂和主题(即为topic)。
2.在Eclipse中基于WebLogic环境新建dynamic web app,并创建接收与发送的servlet。
3.生产者:TopicSender.java,代码如下:
package testTopic;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Servlet implementation class TopicSender
*/
@WebServlet("/TopicSender")
public class TopicSender extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TopicSender() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//JMS 广播接收,应该首先运行接收方程序,然后再广播发送消息。
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
System.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
TopicConnection connection = null;
TopicSession session = null;
Context ctx = null;
try {
ctx = new InitialContext();
// 1.通过JNDI查询ConnectionFactory。JMS中连接工厂分QueueConnectionFactory和
// TopicConnectionFactory两种,Weblogic不区分这两种类型。
TopicConnectionFactory factory = (TopicConnectionFactory) ctx
.lookup("jms/MyMDB-Factory");
// 2.通过工厂建立连接connection
connection = factory.createTopicConnection();
// 3.创建session
session = connection.createTopicSession(false,
TopicSession.AUTO_ACKNOWLEDGE);
// 4.创建Topic,必须新建一个Topic JNDI。
Topic topic = (Topic) ctx.lookup("jms/MyTopic");
// 5.创建TopicPublisher
TopicPublisher tp = session.createPublisher(topic);
// 构造消息体
TextMessage tm = session.createTextMessage();
tm.setText("test");
tm.setStringProperty("user", "嘿嘿,JMS");
tp.publish(tm);
// 必须关闭连接
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
4.消费者:TopicReciver.java,代码如下,
package testTopic;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jms.JMSException;
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;
import javax.naming.NamingException;
/**
* Servlet implementation class TopicReciver
*/
@WebServlet("/TopicReciver")
public class TopicReciver extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TopicReciver() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
System.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
TopicConnection connection = null;
TopicSession session = null;
Context ctx = null;
try {
ctx = new InitialContext();
//1.通过JNDI查询ConnectionFactory
TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("jms/MyMDB-Factory");
//2.通过工厂建立连接connection
connection = factory.createTopicConnection();
//3.创建session
session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
//4.创建Topic
Topic topic = (Topic) ctx.lookup("jms/MyTopic");
//5.创建订阅者
TopicSubscriber ts = session.createSubscriber(topic);
connection.start();
//构造消息体
Message message = ts.receive();
if(message instanceof TextMessage){
TextMessage tm = (TextMessage) message ;
String user = tm.getStringProperty("user");
System.out.println(tm.getText() + ", user : "+user);
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
先访问接收者,对topic进行监听,在访问发送者,就会在控制台打印出消息。