SourcePollingChannelAdapter 创建

SourcePollingChannelAdapter create

我使用 spring 和 spring 集成。我需要为 Feed 解析创建 SourcePollingChannelAdapter 动态,并在 Spring 上下文中注册。

      QueueChannel channel = (QueueChannel) context.getBean("rssFeedChannel");
      SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
      adapter.setApplicationContext(context);
      adapter.setBeanName("adapter.1");
      FeedEntryMessageSource source = new FeedEntryMessageSource(new URL("https://spring.io/blog.atom"), "news");
      source.setApplicationContext(context);
      source.setBeanName("source");
      adapter.setSource(source);
      adapter.setOutputChannel(channel);
      adapter.setTrigger(new PeriodicTrigger(1000));
      adapter.start();

我的应用程序配置:

<int:poller default="true" fixed-rate="5000"/>

    <int:channel id="rssFeedChannel">
        <int:queue capacity="40"/>
    </int:channel>

    <file:outbound-channel-adapter id="file" mode="APPEND" charset="UTF-8" directory="/tmp/si" filename-generator-expression="'SpringBlog'"/>
    <!-- With this work -->
    <!--<feed:inbound-channel-adapter id="news" channel="rssFeedChannel" url="https://spring.io/blog.atom">-->
        <!--<int:poller fixed-rate="5000"/>-->
    <!--</feed:inbound-channel-adapter>-->

    <int:transformer input-channel="rssFeedChannel" expression="payload.title + ' @ ' + payload.link + '#{systemProperties['line.separator']}'" output-channel="file"/>

但没有任何内容写入文件。请帮忙解决bug。

  1. 你也必须把FeedEntryMessageSource做成bean。 applicationContext注入.

  2. 您忘记调用 adapter.afterPropertiesSet()。顺便说一句,FeedEntryMessageSource 实例也是如此。

另一方面,请分享您对采用这种手动方式的原因的看法。为什么不依赖标准的控制反转原则?