java - 如果文件名模式与 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
或至少不能按我的预期工作。
解决方案
你不能这样做,因为文件不被接受,因此没有真正被 Camel 处理,除非你在消费者上打开桥接错误处理程序。
因此,您可能希望在检查文件名之后使用过滤器 EIP 模式,然后在失败时抛出异常等。
推荐阅读
- html - 在 mat-grid-list 中使用 rowspan 和 colspan 元素未正确显示
- python - 模块 tensorflow 没有属性 contrib
- python - 使用 python 从单个 youtube 播放列表链接中提取单个链接
- c++ - 如何读取ORC文件列数据
- java - 如何将菜单项与列表视图绑定
- wordpress - 按下提交按钮几秒钟后自动刷新
- python - 无法从pdf文件中提取文本
- python - Python Django 编辑 HTML 行和更新数据库
- python-3.x - 如何使用鼠标和键盘检测在 pygame 中编辑数独网格?
- node.js - Node WS 可以处理的最大客户端数量是多少?