1

我正在使用轮询消费者模式从给定的只读目录中读取所有文件并进行处理。是否有忽略幂等性的选项?

我知道用 noop=true & idempotent=false 定义的路由会导致整个系统崩溃(无限循环),但池消费者模式是一次性操作,在给定时刻触发。

4

2 回答 2

3

camel file2 端点不会使系统崩溃,因为它默认每秒轮询 2 次。它将从当时该文件夹中的所有文件创建一条消息。(好吧,有一些选项可以忽略最近修改的文件,但其中大部分是)。例如,文件在管道中每秒发送 2 次。

默认情况下,幂等性处于关闭状态,除非传递“noop”以将文件基本上保留在输入文件夹中。但是,每次端点轮询时,所有文件都会通过管道。我通常在处理后移动文件(如果我有多个消费者,则在使用 preMove 之前移动文件)以避免这些重复并仍然避免幂等存储的复杂性。

选择 noop 时,无法禁用幂等标志:

    FileConsumer result = newFileConsumer(processor, operations);

    if (isDelete() && getMove() != null) {
        throw new IllegalArgumentException("You cannot set both delete=true and move options");
    }

    // if noop=true then idempotent should also be configured
    if (isNoop() && !isIdempotentSet()) {
        log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
        setIdempotent(true);
    }

在这种情况下,创建消费者后,幂等标志会被重置。

可以做的是创建忽略所有更新的 IdempotentRepository ( http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/IdempotentRepository.html ) 的简单实现并告诉端点它以前从未见过这个文件。

    package com.company;

    import org.apache.camel.spi.IdempotentRepository;

    public class DummyIdempotentRepository implements IdempotentRepository {
        @Override
        public boolean add(Object key) {
            return true;
        }

        @Override
        public boolean contains(Object key) {
            return false;
        }

        @Override
        public boolean remove(Object key) {
            return true;
        }

        @Override
        public boolean confirm(Object key) {
            return true;
        }

        @Override
        public void start() throws Exception {

        }

        @Override
        public void stop() throws Exception {

        }
    }

像这样的事情应该这样做。

于 2015-05-20T14:59:56.567 回答
0

如果要读取特定事件pollEnrich的文件,请使用文件使用者:

from("direct:readFile")
    .setHeader("CamelFileName", simple("${body}"))
    .pollEnrich("file:///base/folder?fileName=${body}&noop=true", 500)
    .process(...)
于 2015-05-20T15:45:29.487 回答