【问题标题】:Apache Camel send message JMS Consumer receives messageApache Camel 发送消息 JMS Consumer 接收消息
【发布时间】:2014-12-30 04:37:23
【问题描述】:

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

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

不明白为什么我的消费者收不到骆驼路由发送的消息? 我想问题是我需要订阅我的 JMS 消费者对骆驼发送的消息吗? 如果是这种情况,我该怎么做?

【问题讨论】:

  • 确定文件读取成功并发送到主题?

标签: java jms apache-camel activemq


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多