如果文件名模式与提供的正则表达式不匹配,我想将文件移动到错误目录。我有一个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;
}
问题是 if matches = false
当调度程序重新启动时,该文件将再次被获取。我尝试在记录错误后抛出异常
throw new PatternSyntaxException("File name doesn't match expected pattern ", getFileNameRegexp().toString(), -1);
并创建了配置,如果出现异常,应将文件移动到错误位置 PatternSyntaxException
被抛出
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
或者至少没有按照我的预期工作。
最佳答案
你不能这样做,因为文件不被接受,因此不会被 Camel 真正处理,除非你在消费者上打开桥接错误处理程序。
因此,您可能想在之后使用过滤器 EIP 模式,检查文件名,然后在失败时抛出异常等。
关于java - 如果文件名模式与 Camel 不匹配,如何将文件移动到错误目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169566/