首页 > 解决方案 > 如果文件名模式与 Camel 不匹配,如何将文件移动到错误目录

问题描述

如果文件名模式与提供的正则表达式不匹配,我想将文件移动到错误目录。我有一个RegexFileFilter<T>实现GenericFileFilter<T>. 这是接受方法。

    public boolean accept(GenericFile<T> file) {
        Matcher matcher = getFileNameRegexp().matcher(file.getFileName());
        boolean matches = matcher.matches();
        if (!matches) {
            log.error("File named [{}] is not imported because it doesn't matches the expected pattern: [{}].", file.getFileName(), getFileNameRegexp().pattern());
        }
        return matches;
    }

问题是如果匹配 =false调度程序重新启动文件时将再次获取该文件。我试图在记录错误后抛出异常

throw new PatternSyntaxException("File name doesn't match expected pattern ", getFileNameRegexp().toString(), -1);

PatternSyntaxException并创建了配置,如果抛出异常,应该将文件移动到 errorLocation

onException(PatternSyntaxException.class)
                    .useOriginalMessage()
                    .handled(true)
                    .to(getFTPErrorLocation());

问题是,当我抛出异常时,Camel lib 会吃掉该异常并排除引发该异常的文件。有没有办法绕过它或者使用排除来移动文件的其他方式?

我也尝试过添加 URI 格式 &scheduler=spring&scheduler.cron=0+0/1+*+*+*+?&readLock=rename&move=processed&disconnect=true&filter=#fileFilter&exclude=.*(\.GL1025\.).*&move=error ,但没有帮助。有什么建议么?

编辑

这是我的路线部分。

    public void configure() {
        addFileAlreadyProcessedExceptionHandler();
        addEmptyFileExceptionHandler();
        addFailedFileTransformationExceptionHandler();
        addFailedFileNamePatternExceptionHandler();
        addTransactionProcessExceptionHandler();
        addFileDownloadRoute();
        from(getLocalFileSystemDownloadLocation())
                .onCompletion()
                .bean(CreditCardImportLogServiceBean.class)
                .process(fileImportStatisticProcessor)
                .end()
                .transacted()
                .routeId(getRouteId())
                .bean(CreditCardStatusServiceBean.class)
                .to(xsltProcess)
                .process(transactionCounterProcessor)
                .split(xPathBuilder)
                .convertBodyTo(String.class, "ISO-8859-1")
                .unmarshal(jaxb)
                .bean(ImportLogProcessor.class)
                .bean(CurrencyCodeServiceBean.class)
                .bean(CCTransactionConverterProcessor.class)
                .bean(CreditCardTypeMapperServiceBean.class)
                .bean(CreditCardImporterServiceBean.class);
    }

    protected void addFileDownloadRoute() {
        from(getFtpConfiguration())
                .routeId(getDownloadRouteId())
                .transacted()
                .to(getLocalFileSystemDownloadLocation());
    }

    protected void addFailedFileNamePatternExceptionHandler() {
        onException(PatternSyntaxException.class)
                .useOriginalMessage()
                .handled(true)
                .to(getFTPErrorLocation());
    }

所有其他异常都正常工作,唯一不起作用的部分是addFailedFileNamePatternExceptionHandler或至少不能按我的预期工作。

标签: javaapache-camel

解决方案


你不能这样做,因为文件不被接受,因此没有真正被 Camel 处理,除非你在消费者上打开桥接错误处理程序。

因此,您可能希望在检查文件名之后使用过滤器 EIP 模式,然后在失败时抛出异常等。


推荐阅读