2016-08-22 31 views
0

我正在尝试使用一组已配置的RSS-feeds对RSS阅读器进行编码。我认为一个好方法就是通过编码原型来解决这个问题 - @Bean并用配置中找到的每个RSS提要调用它。spring-integration-dsl:使Feed-Flow工作

但是,我想我在应用程序启动时忽略了一点,但没有任何反应。我的意思是豆创建为我所期待,但没有记录在handle() - 方法发生:

@Component 
public class HomeServerRunner implements ApplicationRunner { 

    private static final Logger logger = LoggerFactory.getLogger(HomeServerRunner.class); 

    @Autowired 
    private Configuration configuration; 

    @Autowired 
    private FeedConfigurator feedConfigurator; 

    @Override 
    public void run(ApplicationArguments args) throws Exception { 
     List<IntegrationFlow> feedFlows = configuration.getRssFeeds() 
      .entrySet() 
      .stream() 
      .peek(entry -> System.out.println(entry.getKey())) 
      .map(entry -> feedConfigurator.feedFlow(entry.getKey(), entry.getValue())) 
      .collect(Collectors.toList()); 
     // this one appears in the log-file and looks good 
     logger.info("Flows: " + feedFlows); 
    } 

} 

@Configuration 
public class FeedConfigurator { 

    private static final Logger logger = LoggerFactory.getLogger(FeedConfigurator.class); 

    @Bean 
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
    public IntegrationFlow feedFlow(String name, FeedConfiguration configuration) { 
     return IntegrationFlows 
       .from(Feed 
         .inboundAdapter(configuration.getSource(), getElementName(name, "adapter")) 
         .feedFetcher(new HttpClientFeedFetcher()), 
         spec -> spec.poller(Pollers.fixedRate(configuration.getInterval()))) 
       .channel(MessageChannels.direct(getElementName(name, "in"))) 
       .enrichHeaders(spec -> spec.header("feedSource", configuration)) 
       .channel(getElementName(name, "handle")) 
     // 
     // it would be nice if the following would show something: 
     // 
       .handle(m -> logger.debug("Payload: " + m.getPayload())) 
       .get(); 
    } 

    private String getElementName(String name, String postfix) { 
     name = "feedChannel" + StringUtils.capitalize(name); 
     if (!StringUtils.isEmpty(postfix)) { 
      name += "." + postfix; 
     } 
     return name; 
    } 

} 

缺少了什么吗?似乎我需要以某种方式“启动”流程。

回答

0

原型bean需要在某处“使用” - 如果您没有任何地方引用它,则不会创建实例。

此外,您不能在该范围中放置一个IntegrationFlow@Bean - 它会在内部生成一堆不在该范围内的bean。

请参阅答案to this questionits follow-up您可以使用一种技术创建具有不同属性的多个适配器。

或者,即将推出的1.2 version of the DSL有一个动态注册流程的机制。

+0

是的,1.2工作正常。凉!谢谢。 – sjngm