考虑到每个轮询的最大消息数和Cron,FTP入站通道适配器的轮询工作原理
我有一个UC,我需要从ftp位置选择文件并将其放置到服务器位置 我使用ftp入站通道适配器(SpringIntegration-2.0.4)来实现它。 下面是我的xml中的配置考虑到每个轮询的最大消息数和Cron,FTP入站通道适配器的轮询工作原理,ftp,spring-integration,Ftp,Spring Integration,我有一个UC,我需要从ftp位置选择文件并将其放置到服务器位置 我使用ftp入站通道适配器(SpringIntegration-2.0.4)来实现它。 下面是我的xml中的配置 <bean id="ftpAASessionFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory"> <property name="host" value="${f
<bean id="ftpAASessionFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="${ftp.session.host}" />
<property name="port" value="${ftp.session.port}" />
<property name="username" value="${ftp.session.username}" />
<property name="password" value="${ftp.session.password}" />
<property name="clientMode" value="0" />
<property name="fileType" value="0" />
</bean>
<ftp:inbound-channel-adapter id="ftpAAInbound"
channel="ftpChannel" session-factory="ftpAASessionFactory" charset="UTF-8"
auto-create-local-directory="false" delete-remote-files="true"
remote-directory="${ftp.source.location}" local-directory="file://${ftp.target.location}"
>
<int:poller max-messages-per-poll="5" cron="0 */2 * ? * *">
</int:poller>
</ftp:inbound-channel-adapter>
<int:channel id="ftpChannel">
<int:queue />
<int:interceptors>
<int:wire-tap channel="debugLogger" />
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="debugLogger"
level="DEBUG" log-full-message="true" />
<int:logging-channel-adapter id="errorLogger"
level="ERROR" log-full-message="true" />
….该日志仅显示4个文件;它看起来像是为每个轮询配置了3条消息,它清楚地显示了3个文件是在12:38、1和12:40发送的(没有找到第五个文件) 编辑 下面是实现所需结果的另一种方法(使用出站网关)。这个版本使用JavaDSL 如果您喜欢使用XML配置,中提供了类似的流(XML格式)-您必须在
ls
网关和拆分器之间插入文件限制器
@SpringBootApplication
public class So42528316Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So42528316Application.class, args);
try {
context.getBean(So42528316Application.class).runDemo();
}
finally {
context.close();
}
}
@Autowired
private FileGateway fetchAndProcess;
private void runDemo() throws Exception {
Collection<Boolean> rmResults = this.fetchAndProcess.processFilesAt("so42528316");
while (rmResults != null) {
System.out.println("Processed " + rmResults.size() + " files");
Thread.sleep(10_000);
rmResults = this.fetchAndProcess.processFilesAt("so42528316");
}
System.out.println("No more files available");
}
@MessagingGateway(defaultRequestChannel = "flow.input", defaultReplyTimeout = "0")
public interface FileGateway {
Collection<Boolean> processFilesAt(String pattern);
}
@Bean
public DefaultFtpSessionFactory sessionFactory() {
DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
factory.setHost("10.0.0.3");
factory.setUsername("ftptest");
factory.setPassword("ftptest");
factory.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
return factory;
}
@Bean
public IntegrationFlow flow() {
return f -> f
.handle(Ftp.outboundGateway(sessionFactory(), "ls", "payload"))
.handle("so42528316Application", "limitFiles")
.split()
.handle(Ftp.outboundGateway(sessionFactory(), "get",
"payload.remoteDirectory + '/' + payload.filename")
.localDirectory(new File("/tmp", "so42528316")))
.handle("so42528316Application", "process")
.handle(Ftp.outboundGateway(sessionFactory(), "rm",
"headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']"))
.aggregate();
}
public List<FtpFileInfo> limitFiles(List<FtpFileInfo> files) {
// Add any logic you want here, e.g. check if file already on local disk.
if (files.size() == 0) {
return null;
}
else if (files.size() > 2) {
System.out.println("Reducing fetch list from " + files.size() + " to 2");
return files.stream().limit(2).collect(Collectors.toList());
}
else {
return files;
}
}
public String process(File file) {
System.out.println("Processing " + file);
file.delete();
return file.getName();
}
}
谢谢你的调查。我已经更新了正确的日志。你能再查一下吗。让我知道为什么它会在第一次轮询时传输所有6个文件(通过检查目标文件夹位置进行验证),但对于处理(将其放入FtpChannel-创建有效负载),它只考虑max-message-per-poll中配置的5个文件。哦,现在我理解了你的问题,对不起-是的,立即检索所有可用文件,并根据
max messages per poll
发出消息。在即将发布的5.0版本中,有一个新属性max fetch size
-5.0尚未发布,但该功能已在里程碑版本中发布(M1、M2;M3将于下周发布)。谢谢你的意见。在不升级到更高版本(5.0 M1,M2)的情况下,是否有其他方法可以实现这一点?请您解释/共享指向任何相关文档的链接,我可以从中了解立即检索所有文件和根据每次轮询的最大消息发送消息之间的基本区别。基本上,这个入站通道适配器是如何工作的。可能会有更好的文档记录,但适配器有两个组件,一个远程文件同步器和一个文件读取消息源。如果源在轮询时没有找到新的本地文件,它将调用同步器获取所有可用的远程文件(5.0中的新功能允许您限制获取)。然后,文件源根据轮询器规范轮询本地目录。有关替代解决方案,请参见“编辑我的答案”。
Poll resulted in Message: null
@SpringBootApplication
public class So42528316Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So42528316Application.class, args);
try {
context.getBean(So42528316Application.class).runDemo();
}
finally {
context.close();
}
}
@Autowired
private FileGateway fetchAndProcess;
private void runDemo() throws Exception {
Collection<Boolean> rmResults = this.fetchAndProcess.processFilesAt("so42528316");
while (rmResults != null) {
System.out.println("Processed " + rmResults.size() + " files");
Thread.sleep(10_000);
rmResults = this.fetchAndProcess.processFilesAt("so42528316");
}
System.out.println("No more files available");
}
@MessagingGateway(defaultRequestChannel = "flow.input", defaultReplyTimeout = "0")
public interface FileGateway {
Collection<Boolean> processFilesAt(String pattern);
}
@Bean
public DefaultFtpSessionFactory sessionFactory() {
DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
factory.setHost("10.0.0.3");
factory.setUsername("ftptest");
factory.setPassword("ftptest");
factory.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
return factory;
}
@Bean
public IntegrationFlow flow() {
return f -> f
.handle(Ftp.outboundGateway(sessionFactory(), "ls", "payload"))
.handle("so42528316Application", "limitFiles")
.split()
.handle(Ftp.outboundGateway(sessionFactory(), "get",
"payload.remoteDirectory + '/' + payload.filename")
.localDirectory(new File("/tmp", "so42528316")))
.handle("so42528316Application", "process")
.handle(Ftp.outboundGateway(sessionFactory(), "rm",
"headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']"))
.aggregate();
}
public List<FtpFileInfo> limitFiles(List<FtpFileInfo> files) {
// Add any logic you want here, e.g. check if file already on local disk.
if (files.size() == 0) {
return null;
}
else if (files.size() > 2) {
System.out.println("Reducing fetch list from " + files.size() + " to 2");
return files.stream().limit(2).collect(Collectors.toList());
}
else {
return files;
}
}
public String process(File file) {
System.out.println("Processing " + file);
file.delete();
return file.getName();
}
}
Reducing fetch list from 3 to 2
Processing /tmp/so42528316/bar.txt
Processing /tmp/so42528316/baz.txt
Processed 2 files
Processing /tmp/so42528316/foo.txt
Processed 1 files
No more files available