4
votes

Currently I have started to work on JMS Topic with ActiveMQ. I have created Publisher and Durable Subscribers through JAVA code (mentioned below) and I received the messages in subscribers side also.

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

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?

How can I get those active durable subscribers list from Topic?

Do we have any option to delete a posted message in Topic?

Help me in these contexts.
Thanks in advance.

1

1 Answers

2
votes

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. Such APIs will be JMS provider specific, Active MQ in your case. This link might be useful:http://activemq.apache.org/advisory-message.html