spring integration - SFTP make sure upload is successfully

2.6k views Asked by At

How can we make sure whether the file is successfully uploaded to SFTP server. I want to make sure that SFTP upload is successful then only I want apply other logics. Here is my code to upload.

 @Bean
 public DefaultSftpSessionFactory sftpSessionFactory() throws IOException {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(config.getSftpHost());
    factory.setPort(Integer.parseInt(config.getSftpPort()));
    factory.setUser(config.getSftpUser());
    factory.setAllowUnknownKeys(true);
    factory.setTimeout(10000);
    factory.setPrivateKey(new ClassPathResource(config.getPrivateKey()));
   } 
@Bean
   @ServiceActivator(inputChannel = "toSftpChannel")
   public SftpMessageHandler uploadHandler() throws IOException {
    SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(config.getSftpRemoteDirectory()));
    handler.setFileNameGenerator(new FileNameGenerator() {
    @Override
    public String generateFileName(Message<?> message) {
       if (message.getPayload() instanceof File) {
           return ((File) message.getPayload()).getName();
        } else {
            throw new IllegalArgumentException("File expected as payload.");
        }
      }
    });
   return handler;
  }
 @MessagingGateway
   public interface UploadGateway {
   @Gateway(requestChannel = "toSftpChannel")
    void upload(File file);
   }

I see the logs are looking good. Verified that files are getting uploaded to SFTP server. But somehow I am not able to make sure that files are getting uploaded on server. Please advise how to add code to make sure files are uploaded successfully.

  jsch Connecting to info.xyz.com port 22
  jsch Connection established
  jsch Remote version string: SSH-.0-Internal_SFTP__100
  jsch Local version string: SSH-.0-JSCH-0.1.4
  jsch CheckCiphers: aes6-ctr,aes19-ctr,aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,des-ctr,arcfour,arcfour18,arcfour6
  jsch CheckKexes: diffie-hellman-group14-sha1,ecdh-sha-nistp6,ecdh-sha-nistp84,ecdh-sha-nistp1
  jsch CheckSignatures: ecdsa-sha-nistp6,ecdsa-sha-nistp84,ecdsa-sha-nistp1
  jsch SSH_MSG_KEXINIT sent
  jsch SSH_MSG_KEXINIT received
  jsch kex: server: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha6,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1
  jsch kex: server: ssh-rsa
  jsch kex: server: aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,aes6-ctr,aes19-ctr,des-cbc,blowfish-cbc,arcfour,arcfour18,arcfour6
  jsch kex: server: aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,aes6-ctr,aes19-ctr,des-cbc,blowfish-cbc,arcfour,arcfour18,arcfour6
  jsch kex: server: hmac-sha-6,hmac-sha1-96,hmac-sha1,hmac-sha-1,hmac-md,hmac-sha-1-96,hmac-sha-6-96,hmac-md-96,hmac-sha6,[email protected]
  jsch kex: server: hmac-sha6,hmac-sha-6,hmac-sha1-96,hmac-sha1,hmac-sha-1,hmac-md,hmac-sha-1-96,hmac-sha-6-96,hmac-md-96,[email protected]
  jsch kex: server: none
  jsch kex: server: none
  jsch kex: server: 
  jsch kex: server: 
  jsch kex: client: ecdh-sha-nistp6,ecdh-sha-nistp84,ecdh-sha-nistp1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha6,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
  jsch kex: client: ssh-rsa,ssh-dss,ecdsa-sha-nistp6,ecdsa-sha-nistp84,ecdsa-sha-nistp1
  jsch kex: client: aes18-ctr,aes18-cbc,des-ctr,des-cbc,blowfish-cbc,aes19- 
 ctr,aes19-cbc,aes6-ctr,aes6-cbc
  jsch kex: client: aes18-ctr,aes18-cbc,des-ctr,des-cbc,blowfish-cbc,aes19- 
   ctr,aes19-cbc,aes6-ctr,aes6-cbc
  jsch kex: client: hmac-md,hmac-sha1,hmac-sha-6,hmac-sha1-96,hmac-md-96
  jsch kex: client: hmac-md,hmac-sha1,hmac-sha-6,hmac-sha1-96,hmac-md-96
  jsch kex: client: none
  jsch kex: client: none
  jsch kex: client: 
  jsch kex: client: 
  jsch kex: server->client aes1-ctr hmac-md none
  jsch kex: client->server aes1-ctr hmac-md none
  jsch SSH_MSG_KEXDH_INIT sent
  jsch expecting SSH_MSG_KEXDH_REPLY
  jsch ssh_rsa_verify: signature true
  jsch Host 'info.xyz.com' is known and matches the RSA host key
  jsch SSH_MSG_NEWKEYS sent
  jsch SSH_MSG_NEWKEYS received
  jsch SSH_MSG_SERVICE_REQUEST sent
  jsch SSH_MSG_SERVICE_ACCEPT received
  jsch Authentications that can continue: publickeykeyboard-interactivepassword
  jsch Next authentication method: publickey
  jsch Authentication succeeded (publickey).
  jsch Disconnecting from info.xyz.com port 22
2

There are 2 answers

2
Gary Russell On

It's not clear what you mean; if the upload fails, an exception will be thrown.

You can add a retry advice to the SftpMessageHandler to retry.

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/messaging-endpoints.html#message-handler-advice-chain

0
hh PAT On

Based on Gary suggestion on adding retry advice to retry. I am getting below exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'toSftpChannel' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46)
    at org.springframework.integration.gateway.MessagingGatewaySupport.getRequestChannel(MessagingGatewaySupport.java:387)
    at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:422)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.sendOrSendAndReceive(GatewayProxyFactoryBean.java:568)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:489)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:464)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:453)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy79.upload(Unknown Source)

And my sftp Config looks below

    @Bean
    public DefaultSftpSessionFactory sftpSessionFactory() throws IOException {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        //session config here, port, host, private key
      
  }
@Bean
    @ServiceActivator(inputChannel = "toSftpChannel", adviceChain="retryAdvice")
    public SftpMessageHandler uploadHandler() throws IOException {
        final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(config.getSftpRemoteDirectory()));
        //handler.setChmod(0600);   

        handler.setFileNameGenerator(new FileNameGenerator() {
            @Override
            public String generateFileName(Message<?> message) {
                if (message.getPayload() instanceof File) {
                    logger.info("Files request payload : " + message.getPayload().toString());
                    return ((File) message.getPayload()).getName();
                } else {
                    throw new IllegalArgumentException("File expected as payload.");
                }
            }
        });

        return handler;
    }

    @MessagingGateway
    public interface UploadGateway {
        @Gateway(requestChannel = "toSftpChannel")
        void upload(File file);
    }

    @Bean
    public SimpleRetryPolicy retryPolicy(){
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        return retryPolicy;
    }

    @Bean
    public FixedBackOffPolicy fixedBackOffPolicy(){
        FixedBackOffPolicy p = new FixedBackOffPolicy();
        p.setBackOffPeriod(1000);
        return p;
    }

    @Bean
    public RequestHandlerRetryAdvice retryAdvice(SimpleRetryPolicy retryPolicy, 
     FixedBackOffPolicy fixedBackOffPolicy){
        RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
        retryAdvice.setRetryTemplate(retryTemplate);
        return retryAdvice;
    }