Apache Camel发送消息JMS Consumer接收消息

时间:2014-11-03 13:45:46

标签: java jms apache-camel activemq

让Apache Camel从文件夹到ActiveMQ主题的简单消息路由:

//Create context to create endpoint, routes, processor within context scope
        CamelContext context = new DefaultCamelContext();


        //Create endpoint route
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception 
            {
                from("file:data/outbox").to("activemq:topic:Vadim_Topic");
                //from("activemq:topic:TEST").to.to("file:data/outbox");
            }

        });

        context.start();
            Thread.sleep(5000);
        context.stop();
    }

主题消费者的JMS实施:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        try {

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

            //connection.setClientID("12345");

            connection.start();
            Topic topic = session.createTopic("Vadim_Topic");
            MessageConsumer messageConsumer = session.createConsumer(topic);

            MessageListener messageListener = new MessageListener() {

                public void onMessage(Message message) {

                    TextMessage textMessage = (TextMessage) message;
                    try {
                        System.out.println("Received message: " + textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            };

            messageConsumer.setMessageListener(messageListener);

        } catch (Exception e) {
            e.printStackTrace();
        }

无法理解为什么我的消费者无法接收Camel路由发送的消息? 我想问题是我需要在Camel发送的消息上订阅我的JMS Consumer吗? 如果是这种情况我该怎么做?

1 个答案:

答案 0 :(得分:0)

Camel不仅允许您向主题发送消息,还可以非常轻松地从主题中读取消息并将其发送给您的某个POJO。

从主题中读取并将消息发送到POJO的路径如下所示:

from("activemq:topic:Vadim_Topic").bean(ExampleBean.class);

Camel将根据收到的消息类型以及可用的方法签名确定在POJO上调用哪种方法。有关在骆驼路线中使用POJO的详细信息,请参阅此页面:https://camel.apache.org/bean.html