简体   繁体   English

JMS主题发布/订阅者

[英]JMS Topic Publish/Subscriber

Currently I have started to work on JMS Topic with ActiveMQ . 目前我已经开始使用ActiveMQ处理JMS主题。 I have created Publisher and Durable Subscribers through JAVA code (mentioned below) and I received the messages in subscribers side also. 我通过JAVA代码创建了Publisher和Durable Subscribers (如下所述),我也收到了订阅方的消息。

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

I have some doubts in Topic those are follows, 我在主题中有一些疑问,

How may I check that how many subscribers actively looking for a message in Topic using Java JMS? 我如何检查有多少订阅者使用Java JMS在主题中主动查找消息?

How can I get those active durable subscribers list from Topic? 如何从Topic获得那些活跃的持久订阅者列表?

Do we have any option to delete a posted message in Topic? 我们是否有任何选项可以删除主题中的已发布消息?

Help me in these contexts. 在这些情况下帮助我。
Thanks in advance. 提前致谢。

In Publish/Subscribe messaging pattern, a publisher will be unaware of any subscribers. 在发布/订阅消息传递模式中,发布者将不知道任何订阅者。 Publisher will publish messages to a topic hosted on a broker and the broker will in-turn distribute those messages to any subscribers registered for that topic. 发布者将消息发布到代理上托管的主题,然后代理会将这些消息分发给为该主题注册的任何订阅者。 If there are no subscribers for a topic, then the message will be simply discarded. 如果主题没有订阅者,则将简单地丢弃该消息。

JMS specification does not define any API that can get the details you are looking for. JMS规范没有定义任何可以获取您要查找的详细信息的API。 Such APIs will be JMS provider specific, Active MQ in your case. 在您的情况下,此类API将是特定于JMS提供程序的Active MQ。 This link might be useful: http://activemq.apache.org/advisory-message.html 此链接可能很有用: http//activemq.apache.org/advisory-message.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM