Apache的骆驼:为什么我不能发送豆文字JMS
问题描述:
我有很简单的POJO类:Apache的骆驼:为什么我不能发送豆文字JMS
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()
电话:
template.sendBody("direct:start", bean.getMessage());
你只需要使用ProducerTemplate发送消息踢的路线(它甚至可能是一个空的消息)。 – 2014-11-23 02:04:34