java – JMS主题发布/订阅者

目前我已经开始使用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在主题中主动查找消息?

如何从Topic获得那些活跃的持久订阅者列表?

我们是否有任何选项可以删除主题中的已发布消息?

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

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

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

点赞