Crowie
Crowie

Reputation: 3272

How to use Spring Integration transformer on file contents and then archive the file

I'm a new user to Spring Integration and want to use it to process some files. My problem is that I do not know use a message transformer and subsequently archive the file (provided no exceptions occur).

The question arises because we don't know if a class responsible for processing the contents of a file should receive a file or just the contents of the file.

The opinion offered to me is that it should receive the entire file and then return the file so that it can be archived.

Is there a better way? Specifically I do not know if using file-to-string-transformer means that I only have the file contents and not the file so that it can be archived.

It seems my question is a little like as explained in this question here.

Upvotes: 3

Views: 3398

Answers (2)

Ither
Ither

Reputation: 2568

There is an example in the basic group of "official" examples of Spring Integration that probably you should see. The GitHub repository is this and the example that fits you is this. It demostrates binary and text file copy and processing, using File Inbound Channel Adapter, File Outbound Channel Adapter and File-to-Bytes Transformer. Hope it helps.

Upvotes: 1

Gary Russell
Gary Russell

Reputation: 174564

The FileToStringTransformer makes the original file available in a message header...

        Message<?> transformedMessage = MessageBuilder.withPayload(result)
                .copyHeaders(message.getHeaders())
                .setHeaderIfAbsent(FileHeaders.ORIGINAL_FILE, file)
                .setHeaderIfAbsent(FileHeaders.FILENAME, file.getName())
                .build();

The literal header name is file_originalFile.

So, after your service has processed the content, you can transform the output with a simple transformer

<transformer ... expression="headers.file_originalFile" />

or simply archive it directly with something like...

<service-activator ... expression="headers.file_originalFile.renameTo('/foo/' + headers.file_originalFile.name)" />

Upvotes: 3

Related Questions