How can I read files from two different directory using @InboundChannelAdapter (Spring Integration with java configuration)

余生颓废 提交于 2019-12-11 21:11:36

问题


I'm trying to implement below scenario:

I have a trigger file and a data file which is kept in different directories. Only if I receive a trigger file, I should be able to access the data file, then do the splitting and further processing logic. Also, the case is there will be a single trigger file but multiple data files. So after fetching the trigger file, I should be able to process all the data files.

Below is the code for I used but it is fetching form only one directory

private static final Logger LOGGER = LoggerFactory.getLogger(DatastreamApplication.class);
private static final String DATA_DIRECTORY_PATH = "dataDirectoryLocation";

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    new SpringApplicationBuilder(DatastreamApplication.class).web(false).run(args);
}

@Bean
@InboundChannelAdapter(channel = "fileInputChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> sftpMessageSource() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setDirectory(new File(DATA_DIRECTORY_PATH));
    source.setFilter(new AcceptOnceFileListFilter<>());
    return source;
}

@Splitter(inputChannel = "fileInputChannel")
@Bean
public FileSplitter fileSplitter() {
   FileSplitter fileSplitter = new FileSplitter();
   fileSplitter.setOutputChannelName("chunkingChannel");
   return fileSplitter;
}

@ServiceActivator(inputChannel = "chunkingChannel")
@Bean
public AggregatingMessageHandler  chunker() {
    AggregatingMessageHandler aggregator = new AggregatingMessageHandler(new DefaultAggregatingMessageGroupProcessor());
    aggregator.setReleaseStrategy(new MessageCountReleaseStrategy(1000));
    aggregator.setExpireGroupsUponCompletion(true);
    aggregator.setGroupTimeoutExpression(new ValueExpression<>(100L));
    aggregator.setSendPartialResultOnExpiry(true);
    aggregator.setOutputChannelName("processFileChannel");
    return aggregator;
}

@Bean
@ServiceActivator(inputChannel = "processFileChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            List<String> strings = (List<String>) message.getPayload();
            System.out.println( "List Size :  "+ strings.size() + " for List " + strings.toString());
        }

    };
}

回答1:


It sounds like a better solution for this scenario might be to use the inbound adapter to find the trigger file(s) and then use a custom service (invoked by a service activator) to return a list of the files in the data directory.

EDIT

@SpringBootApplication
public class So52231415Application {

    public static void main(String[] args) {
        new File("/tmp/bar/baz1.txt").delete();
        new File("/tmp/bar/baz2.txt").delete();
        new File("/tmp/foo/baz.trigger").delete();
        SpringApplication.run(So52231415Application.class, args);
    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from(Files.inboundAdapter(new File("/tmp/foo"))
                    .filterExpression("name.endsWith('.trigger')"), e -> e.poller(Pollers.fixedDelay(5_000)))
                .<File, List<File>>transform(f -> {
                    File[] files = new File("/tmp/bar").listFiles();
                    String prefix = f.getName().substring(0, f.getName().lastIndexOf('.'));
                    return Arrays.stream(files)
                        .filter(ff -> ff.getName().startsWith(prefix))
                        .collect(Collectors.toList());
                })
                .split()
                .headerFilter(FileHeaders.ORIGINAL_FILE, FileHeaders.FILENAME, FileHeaders.RELATIVE_PATH)
                .split(new FileSplitter())
                .handle(System.out::println)
                .get();
    }

    @Bean
    public ApplicationRunner runner() {
        return args -> {
            FileOutputStream fos = new FileOutputStream(new File("/tmp/bar/baz1.txt"));
            fos.write("one\ntwo\nthree\n".getBytes());
            fos.close();
            fos = new FileOutputStream(new File("/tmp/bar/baz2.txt"));
            fos.write("four\nfive\nsix\n".getBytes());
            fos.close();
            fos = new FileOutputStream(new File("/tmp/foo/baz.trigger"));
            fos.write("\n".getBytes());
            fos.close();
        };
    }

}

and

GenericMessage [payload=one, headers={sequenceNumber=1, sequenceDetails=[[a06d78dc-8a29-34aa-d8c1-64468edded5b, 1, 2]], sequenceSize=0, file_name=baz1.txt, correlationId=6581c159-352a-b943-d325-db58761b573d, file_originalFile=/tmp/bar/baz1.txt, id=6663d227-ee87-c997-6c67-01b4e50f7b6a, timestamp=1538584697084}]
GenericMessage [payload=two, headers={sequenceNumber=2, sequenceDetails=[[a06d78dc-8a29-34aa-d8c1-64468edded5b, 1, 2]], sequenceSize=0, file_name=baz1.txt, correlationId=6581c159-352a-b943-d325-db58761b573d, file_originalFile=/tmp/bar/baz1.txt, id=d48caf6e-5f3c-76cc-98a8-63800ee1acb8, timestamp=1538584697084}]
GenericMessage [payload=three, headers={sequenceNumber=3, sequenceDetails=[[a06d78dc-8a29-34aa-d8c1-64468edded5b, 1, 2]], sequenceSize=0, file_name=baz1.txt, correlationId=6581c159-352a-b943-d325-db58761b573d, file_originalFile=/tmp/bar/baz1.txt, id=8486ed52-d6a3-6eaf-20de-555b18ea0d75, timestamp=1538584697084}]
GenericMessage [payload=four, headers={sequenceNumber=1, sequenceDetails=[[a06d78dc-8a29-34aa-d8c1-64468edded5b, 2, 2]], sequenceSize=0, file_name=baz2.txt, correlationId=5842bfd7-f25c-9faf-e2f8-7b2a2badf869, file_originalFile=/tmp/bar/baz2.txt, id=3d301ccf-2b80-d4a4-4c59-5f9e38180fe5, timestamp=1538584697087}]
GenericMessage [payload=five, headers={sequenceNumber=2, sequenceDetails=[[a06d78dc-8a29-34aa-d8c1-64468edded5b, 2, 2]], sequenceSize=0, file_name=baz2.txt, correlationId=5842bfd7-f25c-9faf-e2f8-7b2a2badf869, file_originalFile=/tmp/bar/baz2.txt, id=4e43f02c-d64e-0d11-18a4-3ab8b6e7d383, timestamp=1538584697087}]


来源:https://stackoverflow.com/questions/52231415/how-can-i-read-files-from-two-different-directory-using-inboundchanneladapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!