我需要使用Camel路由浏览来自活动MQ的消息,而不消耗消息。
JMS队列中的消息将被读取(仅浏览而非使用)并移动到数据库,同时确保原始队列保持不变。
public class CamelStarter {
private static CamelContext camelContext;
public static void main(String[] args) throws Exception {
camelContext = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
camelContext.addComponent("jms", JmsComponent.jmsComponent(connectionFactory));
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("jms:queue:testQueue").to("browse:orderReceived") .to("jms:queue:testQueue1");
}
}
);
camelContext.start();
Thread.sleep(1000);
inspectReceivedOrders();
camelContext.stop();
}
public static void inspectReceivedOrders() {
BrowsableEndpoint browse = camelContext.getEndpoint("browse:orderReceived", BrowsableEndpoint.class);
List<Exchange> exchanges = browse.getExchanges();
System.out.println("Browsing queue: "+ browse.getEndpointUri() + " size: " + exchanges.size());
for (Exchange exchange : exchanges) {
String payload = exchange.getIn().getBody(String.class);
String msgId = exchange.getIn().getHeader("JMSMessageID", String.class);
System.out.println(msgId + "=" +payload);
}
2条答案
按热度按时间ilmyapht1#
据我所知,不可能在Camel中读取(而不消耗!)JMS消息...
我找到的唯一解决方法(在JEE应用程序中)是定义一个带有计时器的启动EJB,保存一个
QueueBrowser
,并将msg处理委托给Camel路由:jchrr9hc2#
Apache camel browse组件正是为此而设计的。请查看here文档。
由于您没有提供任何其他信息,因此无法再多说。
假设你有一条这样的路线
或
您所要做的就是像这样在中间放一个浏览端点
然后编写这样的类