We have a requirement for the FTP client to download a file whose name and directory is provided at run-time. So, the FTP client may be asked to download file1.txt from foo1/foo2 directory path on the remote server.
We do have a solution using Spring Integration FTP outbound gateway. With this solution to make it dynamic:
- the ApplicationContext for the gateway is created
- the gateway properties get set using file name and remote directory path
- the file is downloaded
- the ApplicationContext is closed.
What we're not happy about is that the ApplicationContext is created and closed every time which obviously affects performance. Is there a way to dynamically pass the file name and the directory path to the gateway without reloading the Appplication Context every time?
Your help will be greatly appreciated.
Here's the main code and configuration:
package com.cvc.ipcdservice.ftp;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;
public class DynamicFtpClient {
private static final Logger LOGGER = LoggerFactory
.getLogger(DynamicFtpClient.class);
public void download(final FtpMetaData ftpMetaData) {
final ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "/META-INF/spring/integration/FtpOutboundGateway-context.xml" },
false);
setEnvironment(ctx, ftpMetaData);
ctx.refresh();
final ToFtpFlowGateway toFtpFlow = ctx.getBean(ToFtpFlowGateway.class);
// execute the flow (mget to download from FTP server)
final List<Boolean> downloadResults = toFtpFlow.mGetFiles("/");
LOGGER.info(
"Completed downloading from remote FTP server. ftpMetaData:{}, downloadResults.size:{} ",
ftpMetaData, downloadResults.size());
ctx.close();
}
/**
* Populate {@code ConfigurableApplicationContext} with Provider-specific
* FTP properties.
*
* @param ctx
* @param customer
*/
private void setEnvironment(final ConfigurableApplicationContext ctx,
final FtpMetaData ftpMetaData) {
final StandardEnvironment env = new StandardEnvironment();
final Properties props = new Properties();
// populate properties for customer
props.setProperty("ftp.host", ftpMetaData.getHost());
props.setProperty("ftp.port", ftpMetaData.getPort());
props.setProperty("ftp.userid", ftpMetaData.getUserName());
props.setProperty("ftp.password", ftpMetaData.getPassword());
// props.setProperty("remote.directory", "/");
// WARNING: the file name pattern has to be surrounded by single-quotes
props.setProperty("ftp.remote.filename.pattern",
"'" + ftpMetaData.getFileNamePattern() + "'");
props.setProperty("ftp.local.dir", ftpMetaData.getLocalDirectory());
final PropertiesPropertySource pps = new PropertiesPropertySource(
"ftpprops", props);
env.getPropertySources().addLast(pps);
ctx.setEnvironment(env);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<int:gateway id="gw" service-interface="com.cvc.ipcdservice.ftp.ToFtpFlowGateway"
default-request-channel="inbound"/>
<bean id="ftpSessionFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="${ftp.host}"/>
<property name="port" value="${ftp.port}"/>
<property name="username" value="${ftp.userid}"/>
<property name="password" value="${ftp.password}"/>
</bean>
<int-ftp:outbound-gateway id="gatewayGET"
local-directory="${ftp.local.dir}"
session-factory="ftpSessionFactory"
request-channel="inbound"
command="mget"
command-options="-P"
expression="${ftp.remote.filename.pattern}"/>
</beans>