首页 文章

为什么SFTP入站/出站通道适配器有单独的通道声明?为什么不为简单文件入站/出站通道适配器?

提问于
浏览
0

我已经开发了一个代码,通过 Spring Integration File Support 教程,我在其中从特定位置轮询文件并进一步处理它们 . 出于轮询目的,我使用了 Spring Integration's 入站和出站通道适配器,所以我的 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("===================================================");
        }
    }

并且有一个处理文件的Handler类,这对我来说很好 .

现在的问题是我想为SFTP远程服务器制作相同的机制,并从该位置轮询文件并将处理过的文件放在不同文件夹中的同一SFTP位置 .

所以我相应地配置了我的bean.xml,并为SFTP编写了入站和出站通道适配器 . 当我来到服务激活器部分时,我发现我需要为入站和出站通道适配器配置单独的通道,并在服务激活器中提供它们,我在简单文件轮询器中没有看到它正常工作 . 那么为什么入站和出站通道适配器需要单独的通道呢?如果需要它们,我们如何为Scheduled文件轮询服务实现相同的功能呢?

1 回答

  • -1

    你没有显示有问题的配置,所以我不确定你在问什么,但通道适配器上的 channel 是可选的;如果省略,则通道名称与 id 相同 . 但总有一个渠道 . 是否需要实际声明 Channels 取决于消费者方面 .

    如果在出站适配器上明确使用 channel ,则需要声明通道 . 在入站适配器上没有必要,因为服务激活器将自动声明其输入通道(如果不存在) .

相关问题