I need to upload an image to FTP server. So I've created integration configuration with SessionFactory, MessageHandler, and MessageGateway for uploading files to FTP server:
@Configuration
@IntegrationComponentScan
public class FtpConfiguration {
@Bean
public SessionFactory<FTPFile> ftpSessionFactory() {
DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory();
//setup
return new CachingSessionFactory<>(defaultFtpSessionFactory);
}
@Bean
@ServiceActivator(inputChannel = "toFtpChannel")
public MessageHandler handler() {
FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory());
handler.setAutoCreateDirectory(true);
handler.setRemoteDirectoryExpression(new LiteralExpression(""));
handler.setFileNameGenerator(message -> (String) message.getHeaders().get("filename"));
return handler;
}
//to show you that I've tried both
/*@Bean
public IntegrationFlow ftpOutboundFlow() {
return IntegrationFlows.from("toFtpChannel")
.handle(Ftp.outboundAdapter(ftpSessionFactory(), FileExistsMode.REPLACE)
.useTemporaryFileName(false)
.fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
.remoteDirectory("")
).get();
}*/
@MessagingGateway
public interface UploadGateway {
@Gateway(requestChannel = "toFtpChannel")
void upload(@Payload byte[] file, @Header("filename") String filename, @Header("path") String path);
}
}
Successfully build an application. And then I'm trying to upload some file:
@Autowired
UploadGateway uploadGateway;
@Override
public void uploadImage(byte[] scanBytes, String filename, String path) {
try {
uploadGateway.upload(scanBytes, filename, path);
} catch (Exception e) {
log.error("WRONG", e);
}
}
And then it says: "No bean named 'toFtpChannel' available" I've tried nearly every tutorial, what do I do wrong?
dependencies:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version>RELEASE</version>
</dependency>