繁体   English   中英

Apache Camel:为什么我不能将bean文本发送到jms

[英]Apache Camel: Why I can't send bean text to jms

我有一个非常简单的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队列。

要将消息插入骆驼路线,您需要将其发送到路线中的使用者(在本例中为direct:start 在此执行此操作最简单的方法是使用ProducerTemplate 启动上下文后:

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

尽管如果最终您只想将bean.getMessage()的内容发送到JMS队列(这就是您要在此处执行的操作),则可以执行此操作,然后从您的setBody()删除setBody()调用。路线:

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

有关ProducerTemplate的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM