使用 Java DSL 成功 ftp 传输后移动文件
Move file after successful ftp transfer using Java DSL
我已经使用 spring-integration java dsl 定义了一个流来 ftp 传输文件,处理它,然后将它传输回 "archive" 目录,最后将其移动到本地存档目录中。
这是 "quite easy" 作为:
@Bean
public IntegrationFlow ftpInboundFlow() {
if (ftpProperties.getEnabled() == false) {
return null;
}
logger.trace("Starting ftp flow");
return IntegrationFlows
.from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
.filter(compositeWithAcceptOnceFilter()) .remoteDirectory(ftpProperties.getRemoteDirectory())
.localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
.handle(new GenericHandler<File>() {
@Override
@Transactional
public Object handle(File payload, Map<String, Object> headers) {
logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
return payload;
}
}) /* Fine GenericHandler */
.handleWithAdapter(a -> a.ftp(ftpSessionFactory())
.remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))
.get();
}
如果我附加
.handleWithAdapter(a -> a.file("'" + ftpProperties.getLocalArchiveDirectory() + "'")
.autoCreateDirectory(true).deleteSourceFiles(true))
在 ftp 适配器配置之后,bean 初始化器回复以下错误消息:
Caused by: org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler@80bfa9d) is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow.
我该如何解决?
对于 XML 配置,我们有一个单向组件的约定,例如 adapter
,例如<int-ftp:outbound-channel-adapter>
或 <int-file:outbound-channel-adapter>
。对于 Java DSL,我们使用像 Gateway
后缀这样的约定来生成 request/reply
端点的工厂方法。否则它是一种方式,就像你的方式 a.ftp(ftpSessionFactory())
.
在 XML 配置的另一端,我们没有任何选择继续下游流定义,因为 <outbound-channel-adapter>
.[=20 上没有 output-channel
=]
使用 Java DSL,我们没有太多选择来防止像您这样的错误。但是我在那里做了如此全面的异常消息,它应该会跟随您以不同的方式进行流程定义。
您的答案是 .publishSubscribeChannel()
,当您可以订阅多个端点以接受相同的消息以为其制定不同的逻辑时。
请找到我的 article,我逐行解释了大部分 SI Java DSL 功能。
作为解决方法,我重构了我的代码以放弃文件适配器:
@Bean
public IntegrationFlow ftpInboundFlow() {
return IntegrationFlows
.from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
.filter(compositeWithAcceptOnceFilter()) .remoteDirectory(ftpProperties.getRemoteDirectory())
.localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
.handle(new GenericHandler<File>() {
@Override
@Transactional
public Object handle(File payload, Map<String, Object> headers) {
logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
/* handle file content here ... */
/* ... then move it to archive */
File dstFile = new File("archive", payload);
FileUtils.moveFile(payload, dstFile);
return dstFile;
}
}) /* Fine GenericHandler */
/* Archive the remote file */
.handleWithAdapter(a -> a.ftp(ftpSessionFactory())
.remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))
.get();
}
我已经使用 spring-integration java dsl 定义了一个流来 ftp 传输文件,处理它,然后将它传输回 "archive" 目录,最后将其移动到本地存档目录中。 这是 "quite easy" 作为:
@Bean
public IntegrationFlow ftpInboundFlow() {
if (ftpProperties.getEnabled() == false) {
return null;
}
logger.trace("Starting ftp flow");
return IntegrationFlows
.from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
.filter(compositeWithAcceptOnceFilter()) .remoteDirectory(ftpProperties.getRemoteDirectory())
.localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
.handle(new GenericHandler<File>() {
@Override
@Transactional
public Object handle(File payload, Map<String, Object> headers) {
logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
return payload;
}
}) /* Fine GenericHandler */
.handleWithAdapter(a -> a.ftp(ftpSessionFactory())
.remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))
.get();
}
如果我附加
.handleWithAdapter(a -> a.file("'" + ftpProperties.getLocalArchiveDirectory() + "'")
.autoCreateDirectory(true).deleteSourceFiles(true))
在 ftp 适配器配置之后,bean 初始化器回复以下错误消息:
Caused by: org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler@80bfa9d) is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow.
我该如何解决?
对于 XML 配置,我们有一个单向组件的约定,例如 adapter
,例如<int-ftp:outbound-channel-adapter>
或 <int-file:outbound-channel-adapter>
。对于 Java DSL,我们使用像 Gateway
后缀这样的约定来生成 request/reply
端点的工厂方法。否则它是一种方式,就像你的方式 a.ftp(ftpSessionFactory())
.
在 XML 配置的另一端,我们没有任何选择继续下游流定义,因为 <outbound-channel-adapter>
.[=20 上没有 output-channel
=]
使用 Java DSL,我们没有太多选择来防止像您这样的错误。但是我在那里做了如此全面的异常消息,它应该会跟随您以不同的方式进行流程定义。
您的答案是 .publishSubscribeChannel()
,当您可以订阅多个端点以接受相同的消息以为其制定不同的逻辑时。
请找到我的 article,我逐行解释了大部分 SI Java DSL 功能。
作为解决方法,我重构了我的代码以放弃文件适配器:
@Bean
public IntegrationFlow ftpInboundFlow() {
return IntegrationFlows
.from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
.filter(compositeWithAcceptOnceFilter()) .remoteDirectory(ftpProperties.getRemoteDirectory())
.localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
.handle(new GenericHandler<File>() {
@Override
@Transactional
public Object handle(File payload, Map<String, Object> headers) {
logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
/* handle file content here ... */
/* ... then move it to archive */
File dstFile = new File("archive", payload);
FileUtils.moveFile(payload, dstFile);
return dstFile;
}
}) /* Fine GenericHandler */
/* Archive the remote file */
.handleWithAdapter(a -> a.ftp(ftpSessionFactory())
.remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))
.get();
}