ACTIVEMQ-发布者订阅者hello world示例

时间:2012-01-26 09:11:47

标签: java jms activemq jmstemplate

有两个程序:订阅者和发布者...... 订阅者能够将消息放入主题并成功发送消息。 当我在浏览器上检查activemq服务器时,它显示1 msg排队。但是当我运行消费者代码时,它没有收到消息

这是生产者代码:

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

运行此代码后,控制台的输出为:

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'

这是消费者代码:

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

    }
}    

运行此代码后,它不会显示任何内容。 有人可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:16)

您的问题是您的消费者正在运行,然后立即关闭。

尝试将此添加到您的消费者中:

    consumer.setMessageListener(listner);

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

    connection.close();

这将等到你在停止之前按下一个键。

需要考虑的其他事项:

  • 使用finally块进行关闭
  • Java命名约定鼓励在类的第一个字母上使用大写

答案 1 :(得分:12)

主要问题(除了快速关闭的应用程序)是您发送到主题。主题不保留消息,因此如果您运行生成然后运行使用者的应用程序,则消费者将不会收到任何内容,因为它在发送消息时未订阅该主题。如果您修复了关闭问题,然后在一个终端中运行使用者然后运行生产者,那么您应该看到消费者收到的消息。如果您想要保留邮件,那么您需要使用一个将保留邮件的队列,直到有人使用它为止。

答案 2 :(得分:3)

您的制作人类是正确的。它运行顺利。

但是,消费者不正确&你必须修改它。

  • 首先,在创建连接对象后添加 setClientID(" any_string_value");

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

  • 其次,使用createDurableSubscriber()方法而不是createConsumer()来通过主题传输消息。

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

以下是经过修改的 comsumer 类:

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

    }
}

现在,您的代码将成功运行。

答案 3 :(得分:2)

只是一些:

  • 使用队列而不是主题。当没有消费者可用时,主题中的消息将被丢弃,它们不会持久存在。
  • 在设置消息侦听器后添加connection.start()。当所有消费者/生产者都得到适当的设置时,你应该开始建立联系。
  • 再等一段时间再关闭连接。

该主题可能是您最重要的失败原因。