【问题标题】:JMS Topic Publish/SubscriberJMS 主题发布/订阅者
【发布时间】:2016-06-17 15:21:29
【问题描述】:

目前我已经开始使用 ActiveMQ 处理 JMS 主题。我通过 JAVA 代码(如下所述)创建了发布者和持久订阅者,并且我也在订阅者端收到了消息。

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);
    }
}

我对以下Topic有一些疑问,

我如何使用 Java JMS 检查有多少订阅者积极在主题中寻找消息?

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

我们可以选择删除主题中发布的消息吗?

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

【问题讨论】:

    标签: java jms activemq jms-topic


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 2013-08-04
      • 2018-08-14
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多