filespring-integrationfile-move

Spring Integration + file reading message source _ Inbound Channel Adapter + Outbound Gateway


For File reading message source Inbound Adapter and transformer with annotations is configured as below

@Bean
@InboundChannelAdapter(autoStartup = "false", value = "incomingchannel", poller = @Poller("custompoller"))
    public MessageSource<File> fileReadingMessageSource() {

}

@Transformer(inputChannel = "incomingchannel", outputChannel = "jobLaunchChannel")
    public JobLaunchRequest toRequest(Message<File> message) throws Exception {

}

Now I want to change the Transformer to refer to a reply channel of outbound gateway i.e. which moves the files from one directory to another directory i.e. move the file from incomingchannel directory to a different directory and the process or transform he file or perform some validations

<file:outbound-gateway id="mover" request-channel="incomingchannel" reply-channel="newdirectory" directory="<<path to new directory file to be moved" delete-source-files="true"/>

Anyone has converted above XML configuration to annotation configurations or any ideas?

After annotation configurations I will have to change the transformer input channel to refer to newdirectory channel i.e. which is a reply channel of messaging gateway...

Thanks in advance for any help ot suggestions regarding this

--- Update 1 after trying out the snippet provided in link by Artem

@Bean
@ServiceActivator(inputChannel = "incomingchannel")
public MessageHandler fileWritingMessageHandler() {
    FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(newdirectorypath));
    handler.setFileExistsMode(FileExistsMode.APPEND);
    handler.setDeleteSourceFiles(true);
    return handler;
}

@MessagingGateway(defaultRequestChannel = "incomingchannel", defaultReplyChannel = "newdirectorychannel")
public interface MyGateway {

    void writeToFile(@Header(FileHeaders.FILENAME) String fileName, @Header(FileHeaders.FILENAME) File directory,
            String data);

}

But there are two problems encountered

  1. Inbound Adapter is trying to poll the directory also as file (Recursive Directory scanner is used) - How to ensure that directory is not polled as a file

  2. nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available, failedMessage=GenericMessage [payload=C


Solution

  • Ok. Since it looks like you would like to place the FileWritingMessageHandler after @InboundChannelAdapter and before @Transformer, so this should like:

    @Bean
    @InboundChannelAdapter(autoStartup = "false", value = "incomingchannel", poller = @Poller("custompoller"))
        public MessageSource<File> fileReadingMessageSource() {
    
    }
    
    @Bean
    @ServiceActivator(inputChannel = "incomingchannel")
    public MessageHandler fileWritingMessageHandler() {
        FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(newdirectorypath));
        handler.setFileExistsMode(FileExistsMode.APPEND);
        handler.setDeleteSourceFiles(true);
        handler.setOutputChannelName("jobLaunchTransfromerCannel");
        return handler;
    }
    
    @Transformer(inputChannel = "jobLaunchTransfromerCannel", outputChannel = "jobLaunchChannel")
        public JobLaunchRequest toRequest(Message<File> message) throws Exception {
    
    }
    

    This way an @InboundChannelAdapter sends a File into a FileWritingMessageHandler for its logic, which produces a result file for the next in flow @Transformer to convert a result file into a JobLaunchRequest. And only after that a message is going to be sent to the jobLaunchChannel to file a Spring Batch Job.