ACTIVEMQ- publisher subscriber hello world example
Asked Answered
R

4

21

There are two programs: subscriber and publisher... Subscriber is able to put the message onto the topic and the message is sent successfully. When I check the activemq server on my browser it shows 1 msg enqueued . But when I run the consumer code, it is not receiving the message

Here is the producer code:

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class producer {

    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    public static void main(String[] args) throws JMSException {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        // JMS messages are sent and received using a Session. We will
        // create here a non-transactional session object. If you want
        // to use transactions you should set the first parameter to 'true'
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic("testt");

        MessageProducer producer = session.createProducer(topic);

        // We will send a small text message saying 'Hello'

        TextMessage message = session.createTextMessage();

        message.setText("HELLO JMS WORLD");
        // Here we are sending the message!
        producer.send(message);
        System.out.println("Sent message '" + message.getText() + "'");

        connection.close();
    }
}

After I run this code the output at the console is:

26 Jan, 2012 2:30:04 PM org.apache.activemq.transport.failover.FailoverTransport doReconnect
INFO: Successfully connected to tcp://localhost:61616
Sent message 'HELLO JMS WORLD'

And here is the consumer code:

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class consumer {
    // URL of the JMS server
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the topic from which we will receive messages from = " testt"

    public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic("testt");

        MessageConsumer consumer = session.createConsumer(topic);

        MessageListener listner = new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        System.out.println("Received message"
                                + textMessage.getText() + "'");
                    }
                } catch (JMSException e) {
                    System.out.println("Caught:" + e);
                    e.printStackTrace();
                }
            }
        };

        consumer.setMessageListener(listner);
        connection.close();

    }
}    

After I run this code it doesnt show anything. Can someone help to me to overcome this problem?

Rentsch answered 26/1, 2012 at 9:11 Comment(1)
guess you are closing the connection too soon. Before your consumer starts consuming, the connection is closed and main method is over!Urgency
Y
17

Your issue is that your consumer is running and then shutting down immediately.

Try adding this into your consumer:

    consumer.setMessageListener(listner);

    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }

    connection.close();

This will wait until you hit a key before stopping.

Other things to consider:

  • Use a finally block for the close
  • Java naming conventions encourage using uppercase for the first letter of a class
Yuonneyup answered 26/1, 2012 at 10:59 Comment(1)
no sir the above code that you mentioned also is not working :( please HELP... badly stuck in the project !Rentsch
P
13

The main problem (besides the app closing down to quickly) is that you are sending to a Topic. Topics don't retain messages so if you run your application that produces and then run the consumer, the consumer won't receive anything because it was not subscribed to the topic at the time the message was sent. If you fix the shutdown issue and then run the consumer in one terminal and then run the producer you should then see the message received by your consumer. If you want message retention then you need to use a Queue which will hold onto the message until someone consumes it.

Prism answered 26/1, 2012 at 22:6 Comment(1)
sorry friends the bug got fixed.. i was executing publisher module first then the subscriber... however it should be subscriber module first then the publisher... thanks for the suggestions :)Rentsch
S
3

Your producer class is correct. It runs smoothly.

But, your consumer is incorrect & you have to modify it.

  • First, add setClientID("any_string_value") after creating connection object;

    eg: Connection connection = connectionFactory.createConnection(); // need to setClientID value, any string value you wish connection.setClientID("12345");

  • secondly, use createDurableSubscriber() method instead of createConsumer() for transmitting message via topic.

    MessageConsumer consumer = session.createDurableSubscriber(topic,"SUB1234");

Here is the modified comsumer class:

package mq.test;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class consumer {
    // URL of the JMS server
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the topic from which we will receive messages from = " testt"

    public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();

        // need to setClientID value, any string value you wish
        connection.setClientID("12345");

        try{
        connection.start();
        }catch(Exception e){
            System.err.println("NOT CONNECTED!!!");
        }
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic("test_data");

        //need to use createDurableSubscriber() method instead of createConsumer() for topic
        // MessageConsumer consumer = session.createConsumer(topic);
        MessageConsumer consumer = session.createDurableSubscriber(topic,
                "SUB1234");

        MessageListener listner = new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        System.out.println("Received message"
                                + textMessage.getText() + "'");
                    }
                } catch (JMSException e) {
                    System.out.println("Caught:" + e);
                    e.printStackTrace();
                }
            }
        };

        consumer.setMessageListener(listner);
        //connection.close();

    }
}

Now, your code will run successfully.

Spiegelman answered 27/8, 2014 at 15:32 Comment(0)
C
2

just some:

  • work with a queue not a topic. messages in topics will be discarded when no consumer is available, they are NOT persistend.
  • add connection.start() after setting the message listener. you should start a connection when all consumers/producers are properly set up.
  • wait some time before before closing the connection again.

the topic will probably be your most important source of failure.

Ciliate answered 28/1, 2012 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.