【问题标题】:Apache Camel: Why I can't send bean text to jmsApache Camel:为什么我不能将 bean 文本发送到 jms
【发布时间】:2015-01-20 19:34:56
【问题描述】:

我有一个非常简单的 pojo 类:

public class MessageBean {

    String text;

    public String getMessage()
    {
        return text;
    }

}

还有骆驼路线:

public static void main(String[] args) {

        final MessageBean bean = new MessageBean();
        bean.text = "This is the text";

        CamelContext context = new DefaultCamelContext();
        ConnectionFactory conFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

        context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(conFactory));

        try {
            context.addRoutes(new RouteBuilder() {

                @Override
                public void configure() throws Exception {

                    from("direct:hello").process(new Processor() {

                        public void process(Exchange exchange) throws Exception {

                            exchange.getIn().setBody(bean);
                        }
                    }).setBody(simple("${body.message}")).to("jms:queue:Test.Queue");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            context.start();
            Thread.sleep(5000);
            context.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

我不明白为什么我不能将文本从 bean 变量 text 发送到 activemq 队列??

当我尝试从文件夹发送 txt 文件时,它会正确发送到 jms 中的队列。

【问题讨论】:

    标签: java jms apache-camel


    【解决方案1】:

    要将消息插入骆驼路由,您需要将其发送到作为您路由中消费者的端点,在本例中为direct:start。最简单的方法是使用ProducerTemplate。启动上下文后:

     ProducerTemplate template = context.createProducerTemplate();
     template.sendBody("direct:start", bean);
    

    虽然如果您最终只是想将 bean.getMessage() 的内容发送到您的 JMS 队列(这就是您在此处尝试执行的操作),您可以改为执行此操作并从中删除 setBody() 调用你的路线:

     template.sendBody("direct:start", bean.getMessage());
    

    More information on ProducerTemplate

    【讨论】:

    • 你只需要使用ProducerTemplate踢路由发送消息(甚至可以是空消息)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多