Search code examples
javaxmlspring-integrationspring-integration-sftp

Spring-integration-sftp: how to get logging


I have the following code who fetches files from a directory on a sftp server and moves them locally. First I list the files, then I get them and then I do a rm with 3 different outbound gateways. How do I get logging from the 'loggingChannel'. Also I would like logging from the 'getChannel' and 'removeChannel'. Thanks in advance!

Here I get the xml config and call the method from the gateway interface.

AppOutboundGateway.java

public class AppOutboundGateway {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
                    "classpath:SftpOutboundGateway.xml");
    
            ToSftpFlowGateway sftpFlowGateway = ctx.getBean(ToSftpFlowGateway.class);
    
            try {
                sftpFlowGateway.lsGetAndRmFiles("/files/");
            } catch (NestedIOException e) {
                System.out.println("Directory not found");
            }
        }
    
    }

This is the gateway interface with one method to get a list from the files and remove them after.

ToSftpFlowGateway.java

public interface ToSftpFlowGateway
{
    List<Boolean> lsGetAndRmFiles(String dir) throws NestedIOException;
}

This is my xml configuration. The factory is mady in web.xml which I will not share.

SftpOutboundGateway.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
       xsi:schemaLocation="http://www.springframework.org/schema/integration
                           http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
                           http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration/sftp 
                           https://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd">

    <import resource="web.xml"/>

    <int:gateway id="gw" service-interface="ToSftpFlowGateway"
                 default-request-channel="inputChannel" default-reply-timeout="1000"/>

    <int:channel id="inputChannel">
    </int:channel>

    <int-sftp:outbound-gateway id="gatewayLS"
                               auto-startup="true"
                               session-factory="sftpSessionFactory"
                               request-channel="inputChannel"
                               command="ls"
                               expression="payload"
                               reply-channel="toSplitter">
    </int-sftp:outbound-gateway>

    <int:splitter input-channel="toSplitter" output-channel="getChannel" delimiters="/"/>

    <int:channel id="getChannel" >
    </int:channel>

    <int-sftp:outbound-gateway id="gatewayGET"
                               local-directory-expression="'C:/temp/'"
                               auto-create-local-directory="true"
                               session-factory="sftpSessionFactory"
                               request-channel="getChannel"
                               reply-channel="removeChannel"    
                               command="get"
                               mode="IGNORE"
                               expression="payload.remoteDirectory + '/' + payload.filename">
    </int-sftp:outbound-gateway>

    <int:channel id="removeChannel">
    </int:channel>

    <int-sftp:outbound-gateway id="gatewayRM"
                               request-channel="removeChannel"
                               session-factory="sftpSessionFactory"
                               expression="headers['file_remoteDirectory'] + headers['file_remoteFile']"
                               reply-channel="loggingChannel"
                               command="rm">
    </int-sftp:outbound-gateway>
    
    <int:logging-channel-adapter id="loggingChannel"/>
    
</beans>

Solution

  • What you are asking for is typical usage of the Wire Tap EI pattern:

    https://www.enterpriseintegrationpatterns.com/patterns/messaging/WireTap.html

    https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-wiretap

    So, what you need is just a configuration of that wire-tap for channels to intercept and forward to your loggingChannel:

    <int:wire-tap pattern="getChannel, removeChannel" channel="loggingChannel"/>