![](/img/trans.png)
[英]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.