Apache Camel读取MongoDB Collection - 不处理任何行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Camel读取MongoDB Collection - 不处理任何行相关的知识,希望对你有一定的参考价值。
我从MongoDB读取以下Camel设置并写入文件,但它不起作用:
public static void main(String args[]) throws Exception {
// create CamelContext
SimpleRegistry sr = new SimpleRegistry();
Mongo mongo = new Mongo("localhost", 27017);
sr.put("mdb", mongo);
CamelContext context = new DefaultCamelContext(sr);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("mongodb:mdb?database=demo&collection=person").to("file:data/outbox");
}
});
// start the route and let it do its work
context.start();
Thread.sleep(10000l);
}
在MongoDB REPL shell中有37条记录从db.person.find({})
返回。但是,当我运行我的应用程序时,我在日志中得到了这个并处理了0条记录:
[ main] DefaultExecutorServiceManager DEBUG Created new ThreadPool for source: Consumer[mongodb://mdb?collection=person&database=demo] with name: mongodb://mdb?collection=person&database=demo. -> org.apache.camel.util.concurrent.RejectableThreadPoolExecutor@47d9a273[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0][mongodb://mdb?collection=person&database=demo]
[ main] MongoDbTailingProcess INFO Starting MongoDB Tailable Cursor consumer, binding to collection: db: DB{name='demo'}, col: person
[ main] MongoDbTailableCursorConsumer DEBUG Stopping consumer: Consumer[mongodb://mdb?collection=person&database=demo]
[ main] SharedProducerServicePool DEBUG Stopping service pool: org.apache.camel.impl.SharedProducerServicePool@1bd4fdd
[ main] GenericFileProducer DEBUG Stopping producer: Producer[file://data/outbox]
[ main] DefaultManagementAgent DEBUG Unregistered MBean with ObjectName: org.apache.camel:context=camel-1,type=producers,name=GenericFileProducer(0x7205765b)
[ main] MongoDbTailingProcess INFO Stopping MongoDB Tailable Cursor consumer, bound to collection: db: DB{name='demo'}, col: person
看起来光标打开,什么都不做,然后关闭。如何让它读取我的数据?
找到答案:
使用tailable游标只有一个必要条件:集合必须是“上限集合”
我只需要在Mongo中创建我的集合,如下所示:
db.createCollection('tc', {capped: true, size: 100})
这允许它从集合tc
中读取数据
另外,我无法直接输出到文件,因为Camel无法在Mongo对象和File之间进行转换,所以我必须这样做:
- 对于
String
:
from("mongodb:mdb?database=demo&collection=tc").convertBodyTo(String.class).to("file:data/outbox");
- 对于
Map
:
from("mongodb:mdb?database=demo&collection=tc").convertBodyTo(Map.class).to("file:data/outbox");
以上是关于Apache Camel读取MongoDB Collection - 不处理任何行的主要内容,如果未能解决你的问题,请参考以下文章
apache-camel - 在spring xml中读取java常量
RabbitMQ - Apache Camel 读取消息如何处理失败的消息
如何读取HL7文件并使用Apache Camel,Hapi和Spring(Java配置)解析它
如何在Apache Camel Aggregator关联中加入多个标头