Apache Camel发送消息JMS消费者接收消息



让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发送的消息的JMS消费者?如果是这种情况,我该怎么做?

Camel不仅允许您向主题发送消息,还可以非常容易地读取主题中的消息并将其发送到您的一个POJO。

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

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

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

最新更新