Apache Camel读取MongoDB Collection - 不处理任何行

时间:2018-02-28 01:33:23

标签: mongodb apache-camel

我从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中有db.person.find({})返回的55条记录。但是,当我运行我的应用程序时,我在日志中得到了这个并处理了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

看起来光标已打开,什么都不做,然后关闭。如何让它读取我的数据?

1 个答案:

答案 0 :(得分:0)

找到答案:

  
    

使用tailable游标只有一个必备条件:集合必须是"上限集合"

  

来自The documentation here

我只需要在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");