camel:检查文件是否“符合条件”进行处理。如果文件不符合继续移动到下一个文件的资格

时间:2014-03-18 01:39:26

标签: apache-camel

文件处理策略检查数据库是否在处理文件XYZ之前执行了特定的预处理条件/步骤。

文件XYZ来自驼峰扫描的根目录的不同子目录。

如果其中一个文件不合格,则驼峰应移至下一个。不确定我应该使用哪个标志,以便它不会再次检查当前文件而不是移动到下一个文件。

我的文件处理策略:

 public class MigrFileProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
private static final Logger log = LoggerFactory.getLogger(MigrFileProcessStrategy.class);

@Autowired
DefaultMigrationProcessor defaultMigrationProcessor;

@Override
public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
//check if HIST table has entry for this filename, if yes all preprocessing done, file is ready.
boolean readyForProcessing = false;
String fileAbsPath = file.getAbsoluteFilePath();
File inMigrFile = new File(fileAbsPath);
readyForProcessing = defaultMigrationProcessor.isFileReadyForProcessing(inMigrFile);
if (!readyForProcessing) {
    String msg = String.format("\n####Process?:%b  File:%s", readyForProcessing, fileAbsPath);
    log.info(msg);
}
return readyForProcessing;
}

}

我的配置:

<bean id="migrFilesToCopy" class="org.apache.camel.component.file.MigrFileFilter">
<!-- Filter for migr files that need to be copied-->
    <property name="caseSensitive" value="true" />
    <property name="excludes" value="**/**/*.migratedLnk, **/_inProgress/**, **/_done/**, **/_failed/**" />
</bean>


    <endpoint id="endpoint_migrFilesToCopy"
        uri="file://#{migrationProcessor.migrRootDir.toFile()}?processStrategy=#migrFileProcessStrategy&amp;directoryMustExist=true&amp;idempotent=true&amp;recursive=true&amp;delete=true&amp;initialDelay=1000&amp;delay=5000&amp;readLock=changed&amp;readLockTimeout=100000&amp;readLockCheckInterval=1000&amp;moveFailed=_failed&amp;maxMessagesPerPoll=10&amp;filter=#migrFilesToCopy" />






    <route id="chMigrate" autoStartup="true">
        <from uri="ref:endpoint_migrFilesToCopy" />
        <pipeline>
            <log message="Procesing file: ${header.CamelFileName}" />
            <!--threads executorServiceRef="migrThreadPool" -->
            <bean ref="migrationProcessor" method="createMetadata" /><!-- MetaDataVo -->
            <bean ref="migrationProcessor" method="createCopyObj" /><!-- CacheCopyObj -->
            <bean ref="migrationProcessor" method="migrate" />
            <!--/threads -->
        </pipeline>
    </route>

通过扩展ant过滤器解决:

公共类MigrFileFilter扩展AntPathMatcherGenericFileFilter实现GenericFileFilter {     private static final Logger log = LoggerFactory.getLogger(MigrFileFilter.class);     @Autowired     DefaultMigrationProcessor defaultMigrationProcessor;     public MigrFileFilter(){     超();     }

@Override
public boolean accept(GenericFile<T> file) {
String fileAbsPath = file.getAbsoluteFilePath();
File inMigrFile = new File(fileAbsPath);
boolean readyForProcessing = false;

if (Files.isDirectory(inMigrFile.toPath())) {
    readyForProcessing = true; //To recursivly process directories.
} else {
    boolean validFilePatten = super.accept(file);
    boolean preprocessDataExist = false;
    if (validFilePatten) {
    preprocessDataExist = defaultMigrationProcessor.isFileReadyForProcessing(inMigrFile);
    }
    readyForProcessing = (validFilePatten && preprocessDataExist);
}
return readyForProcessing;
}

1 个答案:

答案 0 :(得分:1)

使用过滤器过滤掉不需要的文件

您可以实现自定义实现,如果要包含该文件,则只返回true | false。

请参阅过滤使用org.apache.camel.component.file.GenericFileFilter