spring-integrationspring-integration-file

What message is sent to the reply channel of a File Outbound Gateway?


I'm working on a project where Spring Integration is used as a proxy to distribute HTTP requests to the relevant services and send back the answers to the client. Incoming HTTP requests are first preprocessed and sent to an HTTP outbound gateway. On its way back to the client, the HTTP response is first written to a file, using a file outbound gateway:

<!-- write the message payload to ./toto/ and forward it to the http channel -->
<int-file:outbound-gateway request-channel="outputFileChannel"
                           reply-channel="outputHttpChannel"
                           directory="./toto/"
                           filename-generator-expression="headers.file_name + '.json'"
                           mode="REPLACE"/>

<!-- process the message before returning it to the inbound reply channel -->
<int:chain input-channel="routingOutputHttpChannel"
           output-channel="inboundReplyChannel">
    ...
</int:chain>

The documentation for the file outbound gateway says that "after writing the file, it also sends it to the reply channel as the payload of a message". My understanding was that the file contents was sent as the message payload, but from what I've implemented, it is the name of the file written that is sent as payload. The filename is irrelevant to me at this point. Is there a way to directly send the file contents? If not, what should I do the read the contents of the file that was written? Plug a file inbound channel adapter (seems a bit redundant)?

Moreover, what's the proper way to handle the file not being written (so as to forge a specific HTTP response)? My guess would be catching a MessageDeliveryException...


Solution

  • The FileWritingMessageHandler sends a written java.io.File object back to the output channel. This not this gateway responsibility to give you back a content of the file. With the File in the payload you can use a FileToByteArrayTransformer or FileToStringTransformer as the next endpoint in your flow to get a content of that file. See <int-file:file-to-string-transformer> or <int-file:file-to-bytes-transformer> for an XML configuration, respectively.

    To catch an exception in that <int-file:outbound-gateway> and do something with the failure, you can use a <request-handler-advice-chain> sub-element and provide an ExpressionEvaluatingRequestHandlerAdvice: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#message-handler-advice-chain