微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何从 Spring 集成文件中读取嵌套的 txt 文件

我有如下配置文件:-

@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 和 inblund/b/b.txt 读取文件

请在下面找到过滤器代码:-

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();
    }

解决方法

参见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-scanning-and-polling

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?