簡體   English   中英

Spring Integration:通過注釋配置入站通道適配器

[英]Spring Integration : Inbound Channel Adapter configuration via annotations

如何通過注釋而不是常規配置文件配置入站通道適配器? 我能夠為會話工廠定義bean,但是在下面:

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

如何配置在via注釋下給出的入站通道適配器?

<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修改后的代碼如下

@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;
}

}

運行這個時,我得到一個例外,因為定義了名為'ftpChannel'的無bean

請注意,在為入站通道適配器連接時,“channel”關鍵字不可用,而是“值”。

我嘗試用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;
        }
    };
}

我收到錯誤“無法在超時內發送消息:-1”。我還做錯了什么?

我正在尋找的是在應用程序啟動時連接所有bean,然后公開一些方法來開始輪詢服務器,處理它們然后從本地刪除它們,像這樣的東西

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

其中getPollableChannel()為我提供了我為Polling連接的bean。

你有一個@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;
}

另外,請參閱參考手冊

另請注意, 對於Spring Integration的Java DSL ,其中可能看起來像:

@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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM