DurableSubscriber 持久化的订阅者 (4 jboss3)

持久化订阅者有如下几个特点:

1.必须使用授权用户连接

2.无须加载监听器就能接收消息,只要消息已经published.

在JBOSS中需要从jbossmq-state.xml文件中拿授权用户和密码;该文件需要从example/conf目录

copy to deploy/conf directory and reboot jboss server.

3.下面从2个场景中说明使用durable subscriber的好处.

首先使用JMSPublisherClient.java 发布一个普通的文本消息,

再使用JMSSubscriberClient.java普通的订阅但不带监听器的订阅订阅消息,发现再等待5秒之后

没有收到消息. 此时使用JMSDurableSubscriberClient.java 持久化的订阅器订阅消息,发现

前面published的消息已经收到.

注意:

  
   john
   needle
   DurableSubscriberExample
  

这个是用户信息的片断,存在jbossmq-state.xml文件中,id标识cliend id ,

当然在建立tsession.createDurableSubscriber(topic,"My identify");需要提供一个"My identify"

这样的一个描述.可以任意指定.

JMSDurableSubscriberClient.java

=======

package ejb.example;


import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
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.jms.TopicSubscriber;
import javax.naming.Context;

import test.util.Utils;

/**
 * This is a Topic client for jms
 * use server is jboss
 *
 * @author Administrator
 *
 */
public class JMSDurableSubscriberClient {
 Topic    topic;
 TopicConnectionFactory tfactory;
 TopicConnection  tconn;
 TopicPublisher   publisher;
 TopicSubscriber  subscriber;
 TopicSession   tsession;
 
 Context    ctx;
 TextMessage   msg;
 
 public void init() throws Throwable{
  try {
   ctx = Utils.getJbossContext();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public void subscriberTopic() throws Throwable{
  topic   = (Topic)ctx.lookup("topic/testTopic");
  tfactory   = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
  /*
   * user/password come from jbossmq-state.xml file in direcotry deploy/conf
   * first u must copy the  example/conf/jbossmq-state.xml file to  deploy/conf and root jboss
   */
  tconn   = tfactory.createTopicConnection("john","needle");
  tconn.start();

  tsession  = tconn.createTopicSession(
    false,//不需要事务
    QueueSession.AUTO_ACKNOWLEDGE);//自动接收消息
  
  subscriber = tsession.createDurableSubscriber(topic,"My identify");
  msg   = (TextMessage)subscriber.receive(5000);
  System.out.println("============msg:"+msg);
  
  subscriber.close();
  tconn.stop();
  tsession.close();
  tconn.close();
 }
 
 
 class MyTopicListener implements MessageListener{
  public void onMessage(Message msg){
   System.out.println("=========MyTopicListener");
   System.out.println(msg.getClass().toString());
   System.out.println(msg instanceof TextMessage);   
   System.out.println(msg);
   System.out.println("=========MyTopicListener");
  }
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) throws Throwable{
  
  JMSDurableSubscriberClient me = new JMSDurableSubscriberClient();
  me.init();
  me.subscriberTopic();
 }
}

JMSPublisherClient.java

====

package ejb.example;


import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
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.jms.TopicSubscriber;
import javax.naming.Context;

import test.util.Utils;

/**
 * This is a Topic client for jms
 * use server is jboss
 *
 * @author Administrator
 *
 */
public class JMSPublisherClient {
 Topic    topic;
 TopicConnectionFactory tfactory;
 TopicConnection  tconn;
 TopicPublisher   publisher;
 TopicSubscriber  subscriber;
 TopicSession   tsession;
 
 Context    ctx;
 TextMessage   msg;
 
 public void init() throws Throwable{
  try {
   ctx = Utils.getJbossContext();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public void publisherTopic() throws Throwable{
  topic   = (Topic)ctx.lookup("topic/testTopic");
  tfactory   = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
  tconn   = tfactory.createTopicConnection();
  tconn.start();

  tsession  = tconn.createTopicSession(
    false,//不需要事务
    QueueSession.AUTO_ACKNOWLEDGE);//自动接收消息
  
  publisher  = tsession.createPublisher(topic);
  msg   = tsession.createTextMessage();
  msg.setText("Hello world!");
  publisher.publish(msg);
  
  publisher.close();
  tconn.stop();
  tsession.close();
  tconn.close();
 }

 
 class MyTopicListener implements MessageListener{
  public void onMessage(Message msg){
   System.out.println("=========MyTopicListener");
   System.out.println(msg.getClass().toString());
   System.out.println(msg instanceof TextMessage);   
   System.out.println(msg);
   System.out.println("=========MyTopicListener");
  }
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) throws Throwable{
  JMSPublisherClient me = new JMSPublisherClient();
  me.init();
  me.publisherTopic();
 }
}

JMSSubscriberClient.java

====

package ejb.example;


import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
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.jms.TopicSubscriber;
import javax.naming.Context;

import test.util.Utils;

/**
 * This is a Topic client for jms
 * use server is jboss
 *
 * @author Administrator
 *
 */
public class JMSSubscriberClient {
 Topic    topic;
 TopicConnectionFactory tfactory;
 TopicConnection  tconn;
 TopicPublisher   publisher;
 TopicSubscriber  subscriber;
 TopicSession   tsession;
 
 Context    ctx;
 TextMessage   msg;
 
 public void init() throws Throwable{
  try {
   ctx = Utils.getJbossContext();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public void subscriberTopic() throws Throwable{
  topic   = (Topic)ctx.lookup("topic/testTopic");
  tfactory   = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
  tconn   = tfactory.createTopicConnection();
  tconn.start();

  tsession  = tconn.createTopicSession(
    false,//不需要事务
    QueueSession.AUTO_ACKNOWLEDGE);//自动接收消息
  
  subscriber = tsession.createSubscriber(topic);
  msg   = (TextMessage)subscriber.receive(5000);
  System.out.println("============msg:"+msg);
  
  subscriber.close();
  tconn.stop();
  tsession.close();
  tconn.close();
 }
 
 
 class MyTopicListener implements MessageListener{
  public void onMessage(Message msg){
   System.out.println("=========MyTopicListener");
   System.out.println(msg.getClass().toString());
   System.out.println(msg instanceof TextMessage);   
   System.out.println(msg);
   System.out.println("=========MyTopicListener");
  }
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) throws Throwable{
  
  JMSSubscriberClient me = new JMSSubscriberClient();
  me.init();
  me.subscriberTopic();
 }
}

你可能感兴趣的:(JMS,夹子)