Pregunta

Imho the following code should create a new message, which is immedeatelly fetched again. But the output is zero. Why?

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);
    }
¿Fue útil?

Solución

Okay, I found the problem. The example from the ActiveMQ website isn't that good ... but they also provide an answer for my problem.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top