Search code examples
spring-integrationspring-integration-sftp

How to use Spring integration sftp outbound gateway with java config


I'm new to Spring integration sftp.Now,I want to download files from multiple directories.then it seems that SFTP Outbound Gateway is my chocie,but I only find examples using XML config.How can this be done using Java config? My configuration class:

@Configuration
public class SftpConfig {
    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory(){
        DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();
        ...
        return new CachingSessionFactory<LsEntry>(defaultSftpSessionFactory);
    }
    @Bean
    @InboundChannelAdapter(channel = "sftpChannel",autoStartup = "false", poller = @Poller(fixedDelay = "5000"))
    public MessageSource<File> sftpMessageSource() {
    ...
    }
    @Bean(name = "lsGateway")
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handlerLs(){
        SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(),"ls","payload");
        sftpOutboundGateway.setLocalDirectory(new File("sftp-gateway"));
        return sftpOutboundGateway;
    }
}

@MessagingGateway
public interface OutboundGatewayOption {
    @Gateway(requestChannel = "sftpChannel")
    public List<Boolean> lsGetAndRmFiles(String dir);

}

but when I start the app,nothing happens,where is wrong?

----update--- My test class

@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Configuration
@EnableIntegration
@Slf4j
@MessageEndpoint
public class SftpConfigTest{
    @Autowired
    private OutboundGatewayOption gatewayOption;
    @Test
    public void test(){
        gatewayOption.lsGetAndRmFiles("/");
    }
}

Solution

  • It's not clear what you mean by "nothing happens". It appears you have two ways to trigger the LS request - a messaging gateway (which you have to call) and an inbound channel adapter, which has autoStartup = "false" - it won't invoke the gateway until it is started.

    Also, when using that as the trigger, you will need an output channel on the SFTP gateway (where to send the result). When invoking the gateway by calling the messaging gateway, the result will be returned to the gateway (since the SFTP gateway has no output channel).