[英]How to implement an event-driven consumer in Apache Camel and get JMS Message
[英]Apache Camel send message JMS Consumer receives message
使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实现,如果Topic Consumer:
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 Consumer吗? 如果是这种情况,我该怎么办?
Camel不仅允许您将消息发送到主题,还可以非常轻松地从主题中读取消息并将其发送到您的POJO之一。
从您的主题读取并将消息发送到POJO的路由如下所示:
from("activemq:topic:Vadim_Topic").bean(ExampleBean.class);
Camel会根据收到的消息类型以及可用的方法签名来确定在POJO上调用哪种方法。 有关在骆驼路线中使用POJO的详细信息,请参见此页面: https : //camel.apache.org/bean.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.