JMS主题发布/订阅者

时间:2016-03-04 02:10:27

标签: java jms activemq jms-topic

目前,我已经开始使用 ActiveMQ 处理JMS主题。我通过JAVA代码(如下所述)创建了Publisher和Durable Subscribers ,我也收到了订阅方的消息。

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检查主动多少订阅者在主题中查找消息?

如何从主题中获取这些活跃的持久订阅者列表?

我们是否可以选择在主题中删除已发布的消息?

在这些情况下帮助我 提前谢谢。

1 个答案:

答案 0 :(得分:2)

在发布/订阅消息传递模式中,发布者将不知道任何订阅者。发布者将消息发布到代理上托管的主题,然后代理会将这些消息分发给为该主题注册的任何订阅者。如果主题没有订阅者,则将简单地丢弃该消息。

JMS规范没有定义任何可以获取您要查找的详细信息的API。在您的情况下,此类API将是特定于JMS提供程序的Active MQ。此链接可能很有用:http://activemq.apache.org/advisory-message.html