我通过Spring Integration File Support教程开发了一个代码,其中我从特定位置轮询文件并进一步处理它们。出于轮询目的,我使用了Spring Integration 的入站和出站通道适配器,因此我的bean.xml与以下内容相同:
<?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:integration="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<file:inbound-channel-adapter id="filesIn"
directory="file:${java.io.tmpdir}/input">
<integration:poller id="poller" fixed-rate="60000" />
</file:inbound-channel-adapter>
<integration:service-activator
input-channel="filesIn" output-channel="filesOut" ref="handler" />
<file:outbound-channel-adapter id="filesOut"
directory="file:${java.io.tmpdir}/archive" delete-source-files="true">
</file:outbound-channel-adapter>
<bean id="handler" class="com.m.c.Handler" />
</beans>
现在我的主要课程是:
@SpringBootConfiguration
@EnableScheduling
public class App{
private static final Log LOGGER = LogFactory.getLog(App.class);
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
@Scheduled(fixedDelay = 60000)
public static void display() throws InvalidFormatException, IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("/spring/integration/bean.xml", App.class);
File inDir = (File) new DirectFieldAccessor(context.getBean(FileReadingMessageSource.class)).getPropertyValue("directory");
LiteralExpression expression = (LiteralExpression) new DirectFieldAccessor(context.getBean(FileWritingMessageHandler.class)).getPropertyValue("destinationDirectoryExpression");
File outDir = new File(expression.getValue());
LOGGER.info("Input directory is: " + inDir.getAbsolutePath());
LOGGER.info("Archive directory is: " + outDir.getAbsolutePath());
LOGGER.info("===================================================");
}
}
并且有一个处理文件的处理程序类,这对我来说很好用。
现在的问题是我想为 SFTP 远程服务器创建相同的机制,并从该位置轮询文件,并将处理后的文件放在不同文件夹中的相同 SFTP 位置。
所以我相应地配置了我的 bean.xml 并为 SFTP 编写了入站和出站通道适配器。当我来到 service-activator 部分时,我发现我需要为 Inbound 和 Outbound 通道适配器配置单独的通道,并在 service-activator 中提供它们的 id,我在简单的文件轮询器中没有看到它工作正常。那么为什么入站和出站通道适配器需要单独的通道呢?如果需要它们,我们如何为计划文件轮询服务实现相同的功能?