worm3r
worm3r

Reputation: 17

How to upload multiple files using sftp in spring batch

I have 8 files that I want to upload to an FTP server using sftp in the spring batch. I am not able to configure the Tasklet for that can anyone tell me how to do that. Also, the file name should remain the same as it was in local. I am new to Spring so please help.

@Configuration
public class FTPSonfigurations {
    
    
    @Bean
    public DefaultSftpSessionFactory gimmeFactory(){
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost("");
        factory.setUser("");
        factory.setPassword("");
        return factory;
    }
    
    @Bean
    @ServiceActivator(inputChannel = "uploadfile")
    SftpMessageHandler uploadHandler(DefaultSftpSessionFactory factory){
        SftpMessageHandler messageHandler = new SftpMessageHandler(factory);
        messageHandler.setRemoteDirectoryExpression(new LiteralExpression("/upload/ "));
        return messageHandler;
    }
    
}

@MessagingGateway
public interface UploadMessagingGateway {

    @Gateway(requestChannel = "uploadfile")
    public void uploadFile(File file);
    
}

public class MyTasklet implements Tasklet {

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    
        //What to do here???
        return null;
    }

}

Upvotes: 1

Views: 1809

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

Just auto wire the gateway into the tasklet and call it.

@Autowired
UploadMessagingGateway gw;

...

    gw.uploadFile(file);

Upvotes: 1

Related Questions