java Spring Integration:通过注释的入站通道适配器配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38380851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:21:25  来源:igfitidea点击:

Spring Integration : Inbound Channel Adapter configuration via annotations

javaspring-integration

提问by Anish

How can I configure the inbound channel adapter via annotations instead of the regular configuration file? I was able to define the bean for the session factory though as under:

如何通过注释而不是常规配置文件配置入站通道适配器?我能够为会话工厂定义 bean,但如下所示:

@Bean
public DefaultFtpSessionFactory ftpSessionFactory() {
        DefaultFtpSessionFactory ftpSessionFactory = new  
       DefaultFtpSessionFactory();
        ftpSessionFactory.setHost(host);
        ftpSessionFactory.setPort(port);
        ftpSessionFactory.setUsername(username);
        ftpSessionFactory.setPassword(password);
        return ftpSessionFactory;
    }

How can I configure the inbound channel adapter given as under via annotations?

如何配置通过注释给出的入站通道适配器?

<int-ftp:inbound-channel-adapter id="ftpInbound"
                                 channel="ftpChannel"
                                 session-factory="ftpSessionFactory"
                                 filename-pattern="*.xml"
                                 auto-create-local-directory="true"
                                 delete-remote-files="false"
                                 remote-directory="/"
                                 local-directory="ftp-inbound"
                                 local-filter="acceptOnceFilter">

    <int:poller fixed-delay="60000" max-messages-per-poll="-1">
        <int:transactional synchronization-factory="syncFactory" />
    </int:poller>

</int-ftp:inbound-channel-adapter>

@Artem Bilan The modified code is as under

@Artem Bilan 修改后的代码如下

@EnableIntegration
@Configuration
public class FtpConfiguration {
    @Value("${ftp.host}")
    private String host;
    @Value("${ftp.port}")
    private Integer port;
    @Value("${ftp.username}")
    private String username;
    @Value("${ftp.password}")
    private String password;
    @Value("${ftp.fixed.delay}")
    private Integer fixedDelay;
    @Value("${ftp.local.directory}")
    private String localDirectory;

    private final static Logger LOGGER =   LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@Bean
public SessionFactory<FTPFile> ftpSessionFactory() {
    DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory();
    sessionFactory.setHost(host);
    sessionFactory.setPort(port);
    sessionFactory.setUsername(username);
    sessionFactory.setPassword(password);
    return new CachingSessionFactory<FTPFile>(sessionFactory);
}

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory("/");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml"));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(value = "ftpChannel",
        poller = @Poller(fixedDelay = "60000", maxMessagesPerPoll = "-1"))
public MessageSource<File> ftpMessageSource() {
    FtpInboundFileSynchronizingMessageSource source =
            new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(localDirectory));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

}

}

While running this,I get an exception as under No bean named 'ftpChannel' is defined

运行此程序时,我收到一个异常,因为在 No bean named 'ftpChannel' is defined 下

Please note that 'channel' keyword in not available while wiring the Inbound channel adapter,its 'value' instead.

请注意,在连接入站通道适配器时,“通道”关键字不可用,而是使用它的“值”。

I tried wiring the channel with PollableChannel,that also went in vain though. It is as under:

我尝试用 PollableChannel 连接通道,但这也徒劳无功。如下:

@Bean
public MessageChannel ftpChannel() {
    return new PollableChannel() {
        @Override
        public Message<?> receive() {
            return this.receive();
        }

        @Override
        public Message<?> receive(long l) {
            return null;
        }

        @Override
        public boolean send(Message<?> message) {
            return false;
        }

        @Override
        public boolean send(Message<?> message, long l) {
            return false;
        }
    };
}

I got an error "failed to send message within timeout: -1".Am I doing something wrong still?

我收到错误消息“未能在超时内发送消息:-1”。我还是做错了什么吗?

What I'm looking for is to wire up all the beans on application start-up, and then expose some method to start polling the server,process them and then delete them from local,something like this

我正在寻找的是在应用程序启动时连接所有 bean,然后公开一些方法来开始轮询服务器,处理它们,然后从本地删除它们,就像这样

public void startPollingTheServer() {
  getPollableChannel().receive();
}

where getPollableChannel() gives me the bean I had wired for Polling.

getPollableChannel() 在哪里给我我为轮询连接的 bean。

采纳答案by Artem Bilan

There is an @InboundChannelAdapterfor you.

有一个@InboundChannelAdapter适合你。

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory("/");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml"));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "ftpChannel")
public MessageSource<File> ftpMessageSource() {
    FtpInboundFileSynchronizingMessageSource source =
            new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
    source.setLocalDirectory(new File("ftp-inbound"));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

Plus take a look into the Reference Manual.

另外看看参考手册

Also pay attention, please, for Java DSL for Spring Integration, where the same might look like:

还请注意,对于Java DSL for Spring Integration,它可能看起来像:

@Bean
public IntegrationFlow ftpInboundFlow() {
    return IntegrationFlows
            .from(s -> s.ftp(this.ftpSessionFactory)
                            .preserveTimestamp(true)
                            .remoteDirectory("ftpSource")
                            .regexFilter(".*\.txt$")
                            .localFilename(f -> f.toUpperCase() + ".a")
                            .localDirectory(this.ftpServer.getTargetLocalDirectory()),
                    e -> e.id("ftpInboundAdapter").autoStartup(false))
            .channel(MessageChannels.queue("ftpInboundResultChannel"))
            .get();
}