ActiveMQ,主题不反弹消息

时间:2014-01-03 11:04:18

标签: jms activemq publish-subscribe

Imho以下代码应该创建一个新消息,这个消息将被重新获取。但输出为零。为什么呢?

public static void main(String[] args) throws JMSException, NamingException {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
        props.setProperty("topic.MyTopic", "FOO.BAR");

        // create a new intial context, which loads from jndi.properties file
        Context ctx = new InitialContext(props);
        // lookup the connection factory
        TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
        // create a new TopicConnection for pub/sub messaging
        TopicConnection conn = factory.createTopicConnection();
        // lookup an existing topic
        Topic mytopic = (Topic) ctx.lookup("MyTopic");
        // create a new TopicSession for the client
        TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
        // create a new publisher to produce messages
        TopicPublisher publisher = session.createPublisher(mytopic);
        // create a new subscriber to receive messages
        TopicSubscriber subscriber = session.createSubscriber(mytopic);
        subscriber.setMessageListener(new MessageListener() {
            public void onMessage(Message msg) {
                try {
                    TextMessage textMessage = (TextMessage) msg;
                    String txt = textMessage.getText();
                    System.out.println(txt);
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        TextMessage message = session.createTextMessage();
        message.setText("Kebap: Pommes");
        publisher.publish(message);
    }

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。来自ActiveMQ网站的例子并不是那么好......但它们也为我的问题提供了答案。

http://activemq.apache.org/i-am-not-receiving-any-messages-what-is-wrong.html