[原创]JMS使用topic模型初探_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2093 | 回复: 0   主题: [原创]JMS使用topic模型初探        下一篇 
tianyuan.liu
注册用户
等级:上尉
经验:766
发帖:33
精华:0
注册:1970-1-1
状态:离线
发送短消息息给tianyuan.liu 加好友    发送短消息息给tianyuan.liu 发消息
发表于: IP:您无权察看 2015-1-15 17:53:59 | [全部帖] [楼主帖] 楼主

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进行监听,在访问发送者,就会在控制台打印出消息。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论