Vitalie
Vitalie

Reputation: 413

How to remove file from local dir after moving in to GCP Storage using Spring Integration

I have this code and it successfully moves files from local dir to GCP Storage bucket when they are put there but I need to remove them after moving to GCP.

@Bean
@InboundChannelAdapter(channel = "new-file-channel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> streamingAdapter(Storage gcs) {
    FileReadingMessageSource sourceReader = new FileReadingMessageSource();
    sourceReader.setDirectory(new File(INPUT_DIR));
    sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
    return sourceReader;
}

@Bean
@ServiceActivator(inputChannel = "new-file-channel")
public MessageHandler outboundChannelAdapter(Storage gcs) {
    GcsMessageHandler outboundChannelAdapter = new GcsMessageHandler(new GcsSessionFactory(gcs));
    outboundChannelAdapter.setRemoteDirectoryExpression(new ValueExpression<>(this.gcsReadBucket));
    return outboundChannelAdapter;
}

Upvotes: 2

Views: 199

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121507

You need to add to that @ServiceActivator(inputChannel = "new-file-channel") an adviceChain with the reference to the ExpressionEvaluatingRequestHandlerAdvice which will perform onSuccessExpression to remove that local file.

Something like this:

    @Bean
    public Advice removeFileAdvice() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpressionString("payload.delete()");
        advice.setSuccessChannel(myHandlerSuccessChannel());
        return advice;
    }

It is going to work because your payload is a java.io.File and it has that delete() method.

Than you configure as I said in the beginning:

@ServiceActivator(inputChannel = "new-file-channel", adviceChain="removeFileAdvice")

Upvotes: 3

Related Questions