2016-03-04 160 views
2

目前我已開始使用JMS主題與ActiveMQ。我有創建發佈者和持久訂閱者通過JAVA代碼(下面提到),我也收到訂閱方的消息。JMS主題發佈/訂閱者

Publisher.Java

public static void createConnectionAndSendMessage(String ipAddress) 
    { 
     try 
     { 
      ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://"+ipAddress+":61617"); 

      Connection connection = factory.createConnection(); 
      connection.start(); 

      Session topicSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
      Topic topic = topicSession.createTopic("Test-Topic"); 

      MessageProducer producer = topicSession.createProducer(topic); 
      producer.setDeliveryMode(DeliveryMode.PERSISTENT); 

      ObjectMessage message = topicSession.createObjectMessage(); 

      TopicTO topicTO = new TopicTO(); 
      topicTO.setId(i); 
      topicTO.setName("Sample"); 

      message.setStringProperty("s_id", "Sample"); 
      message.setObject((Serializable) topicTO);     

      producer.send(message); 
      System.out.println("message sent successfully"); 
     } 
    } 
    catch(JMSException e) 
    { 
     System.out.println("error :" + e); 
    } 
} 

Subscriber.java

public void createConnectionAndReceiveMessage(String clientId, String ipAddress) 
    { 
     try 
     { 
      ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://"+ipAddress+":61617"); 
      Connection connection = connectionFactory.createConnection(); 
      connection.setClientID(clientId); 
      connection.start();    
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
      Topic topic = session.createTopic("Test-Topic"); 

      String selector = "s_id = 'Sample'"; 
      System.out.println("selector : '"+selector+"'...."); 
      TopicSubscriber consumer = session.createDurableSubscriber(topic, "Sub1", selector, true); 

      consumer.setMessageListener(new TopicMessageListener());    

    } 
    catch(Exception e) 
    { 
     System.out.println("error :" + e); 
    } 
} 

我有些疑惑的主題那些如下,

如何可能我檢查多少訂戶積極尋找主題中使用Java JMS的消息?

如何從主題中獲得那些活動的持久訂閱者列表?

我們是否有任何選項可以刪除主題中發佈的消息?

在這些背景下幫助我。
在此先感謝。

回答

2

在發佈/訂閱消息傳遞模式中,發佈者將不知道任何訂閱者。發佈者將發佈消息給代理託管的主題,代理將輪流將這些消息分發給爲該主題註冊的任何訂閱者。如果某個主題沒有訂閱者,那麼該消息將被簡單地丟棄。

JMS規範沒有定義任何可以獲取您要查找的細節的API。這些API將是JMS提供程序特定的,Active MQ是您的情況。此鏈接可能有用:http://activemq.apache.org/advisory-message.html