如何从spring集成文件中读取嵌套的txt文件

pobjuy32  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(445)

我有如下配置文件:-

@EnableBinding(Source.class)
@Configuration
@EnableIntegrationManagement
public class FileSourceConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileSourceConfig.class);

    private FileSourceProperties properties;

    Source source;

    public FileSourceConfig(FileSourceProperties properties, Source source) {
        this.properties = properties;
        this.source = source;
    }

    @Bean
    public DynamicRegexPatternFilter getFilter(){
        return new DynamicRegexPatternFilter();
    }

    @Bean
    public MessageChannel linesChannel() {
        return new DirectChannel();
    }

    /* To poll the file for every given TimeUnit.SECONDS */
    @Bean(name = { "defaultPoller", PollerMetadata.DEFAULT_POLLER })
    public PollerMetadata defaultPoller() {
        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(new PeriodicTrigger(properties.getPollPeriod(), TimeUnit.SECONDS));
        return pollerMetadata;
    }

    @Bean
    public IntegrationFlow fileInboundChannelFlow() {
        FileInboundChannelAdapterSpec messageSourceSpec = Files
                .inboundAdapter(Paths.get(this.properties.getDirectory()).toFile());

        messageSourceSpec = messageSourceSpec.filter(getFilter());

        //messageSourceSpec.regexFilter(this.properties.getFilenameRegex());
        messageSourceSpec.preventDuplicates(this.properties.isPreventDuplicates());

        //Setting random UUID as messagekey
        IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(messageSourceSpec)
                .split(new FileSplitter(true, true))
                .enrichHeaders(h -> h.headerExpression(KafkaHeaders.MESSAGE_KEY,"T(java.util.UUID).randomUUID().toString()"));

        return flowBuilder.<Object, Class<?>>route(Object::getClass,
                m -> m.channelMapping(FileSplitter.FileMarker.class, "markers.input").channelMapping(String.class,
                        "lines.input"))
                .get();
    }

    @Bean
    public IntegrationFlow lines() {
        return f -> f.headerFilter("file_originalFile") .channel(source.output());
    }

    @Bean
    public IntegrationFlow logErrors() {
        return f -> f.log(LoggingHandler.Level.ERROR, "error", m -> "Error in sending message :"+m.getPayload());
    }

    @Bean
    public IntegrationFlow markers() {
        return f -> f.log().<FileSplitter.FileMarker>filter(m -> m.getMark().equals(FileSplitter.FileMarker.Mark.END))
                .handle(m -> m.getHeaders(), e -> e.id("archive").advice(afterAdvice()));
    }}

有人能建议如何从inbound/a/a.txt和inblond/b/b.txt读取文件吗
请查找筛选器代码below:-

public class DynamicRegexPatternFilter extends AbstractFileListFilter<File> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicRegexPatternFilter.class);

    @Autowired
    private FileSourceProperties properties;

    @Override
    public boolean accept(File file) {
        String[] quaterRange = {"[0][0-3]", "[0][4-6]", "[0][7-9]", "[1][0-2]"};
        String fileNameRegex = this.properties.getFilenameRegex();
        //logic here

        return Pattern.compile(fileNameRegex)
                .matcher(file.getName())
                .matches();
    }
tjvv9vkg

tjvv9vkg1#

看到了吗 RecursiveDirectoryScanner . 默认情况下 FileReadingMessageSource 带有一个 DefaultDirectoryScanner . 所以,您只需要配置 messageSourceSpec 有了这个 RecursiveDirectoryScanner :

FileInboundChannelAdapterSpec messageSourceSpec =
                Files.inboundAdapter(Paths.get(this.properties.getDirectory()).toFile())
                        .scanner(new RecursiveDirectoryScanner());

另见文件:https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#directory-扫描和轮询

相关问题