Good evening.
We have a requirement to use ftp inbound channel adapter programmatically.
In other words it is required to read remote directories from database and set up and run a separate ftp inbound channel adaper for each remote directory.
First I tried decaratively that worked fine (which is as follows).
ftp.properties:
---------------
host=127.0.0.1
username=admin
password=password
===============================
ftp-inbound-context.xml:
-----------------------
<contextroperty-placeholder location="ftp.properties"/>
<context:component-scan base-package="com.test.si.ftp"/>
<int-ftp:inbound-channel-adapter id="ftpInbound"
channel="ftpChannel"
session-factory="ftpClientFactory"
filename-regex=".*\.txt$"
auto-create-local-directory="true"
delete-remote-files="false"
remote-directory="/"
local-directory="/var/output/">
<intoller fixed-rate="1000"/>
</int-ftp:inbound-channel-adapter>
<int:channel id="ftpChannel">
<int:queue/>
</int:channel>
====================================
FtpConfiguration.java:
----------------------
@Configuration
public class FtpConfiguration {
@Value("${host}")
private String host;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean
public DefaultFtpSessionFactory ftpClientFactory() {
DefaultFtpSessionFactory ftpSessionFactory =
new DefaultFtpSessionFactory();
ftpSessionFactory.setHost(host);
ftpSessionFactory.setPort(2121);
ftpSessionFactory.setUsername(username);
ftpSessionFactory.setPassword(password);
ftpSessionFactory.setClientMode(
FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
return ftpSessionFactory;
}
}
==================================================
Test class:
-----------
ApplicationContext context =
new ClassPathXmlApplicationContext("ftp-inbound-context.xml");
PollableChannel ftpChannel = context.getBean("ftpChannel", PollableChannel.class);
Message<?> message = ftpChannel.receive();
System.out.println("message: " + message);
================================================== ===
The above said ftp inbound channel adapter usage is programmatically done as follows (which didn't work):
FTPService.java:
----------------
public class FTPService implements Runnable {
@Override
public void run() {
while(true) {
String host = "127.0.0.1";
String username = "admin";
String password = "password";
DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory();
ftpSessionFactory.setHost(host);
ftpSessionFactory.setPort(2121);
ftpSessionFactory.setUsername(username);
ftpSessionFactory.setPassword(password);
ftpSessionFactory.setClientMode(FTPClient.PASSIVE_ LOCAL_DATA_CONNECTION_MODE);
FtpInboundFileSynchronizer ftpInboundFileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory);
ftpInboundFileSynchronizer.setDeleteRemoteFiles(fa lse);
Pattern pattern = Pattern.compile(".*\\.txt$");
FtpRegexPatternFileListFilter ftpRegexPatternFileListFilter = new FtpRegexPatternFileListFilter(pattern);
ftpInboundFileSynchronizer.setFilter(ftpRegexPatte rnFileListFilter);
ftpInboundFileSynchronizer.setRemoteDirectory("/");
FtpInboundFileSynchronizingMessageSource ftpInboundFileSynchronizingMessageSource = new FtpInboundFileSynchronizingMessageSource(ftpInboun dFileSynchronizer);
ftpInboundFileSynchronizingMessageSource.setAutoCr eateLocalDirectory(true);
ftpInboundFileSynchronizingMessageSource.setAutoSt artup(true);
ftpInboundFileSynchronizingMessageSource.setLocalD irectory(new File("/var/output"));
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
ftpInboundFileSynchronizingMessageSource.setTaskSc heduler(threadPoolTaskScheduler);
PollableChannel ftpChannel = new QueueChannel(100);
ftpInboundFileSynchronizingMessageSource.setOutput Channel(ftpChannel);
MessageHandler messageHandler = new DelayHandler(1000L);
ConsumerEndpointFactoryBean factoryBean = new ConsumerEndpointFactoryBean();
factoryBean.setHandler(messageHandler);
factoryBean.setInputChannel(ftpChannel);
factoryBean.setInputChannelName("ftpChannel");
factoryBean.start();
Message<?> message = ftpChannel.receive();
System.out.println("message: " + message);
}
}
public static void main(String[] args) {
Thread t = new Thread(new FTPService());
t.start();
}
}
=============================
Could you please let me know what I am missing while running programmatically.
I appreciate and thank you for your feedback.
Thanks
Venkat


roperty-placeholder location="ftp.properties"/>
Reply With Quote