首页 > 解决方案 > 当我们在 @InboundChannelAdapter 中使用自定义 pollerMetadata 时,PollerMetadata 为空

问题描述

我实现了用于提取数据的自定义消息源。当我运行我的应用程序 beanPollerMetadata之前没有时间初始化@InboundChannelAdapter并抛出异常:

原因:java.lang.IllegalArgumentException:没有为通道适配器“statusFlow.org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0”定义轮询器,并且上下文中没有可用的默认轮询器。

我的代码:

@Bean("myTrigger")
public PeriodicTrigger periodicTrigger() {
    PeriodicTrigger trigger = new PeriodicTrigger(2000);
    trigger.setFixedRate(true);
    return trigger;
}

@Bean("myPollerMetadata")
public PollerMetadata currentRackStatusLandingFlowPollerMetadata(@Qualifier("myTrigger") PeriodicTrigger trigger) {
    PollerMetadata metadata = new PollerMetadata();
    metadata.setTrigger(trigger);
    metadata.setMaxMessagesPerPoll(1);
    return metadata;
}

@InboundChannelAdapter(poller = @Poller(value = "myPollerMetadata"))
public MessageSource<List<Status>> statusSource() {
    return () -> new DefaultMessageBuilderFactory()
            .withPayload(statusService.getCurrentStatuses())
            .build();
}

我的问题:

如何设置一个PollerMetadatafor @InboundChannelAdapter

标签: spring-bootspring-integration

解决方案


根据你的例外,这个故事不是关于那个@InboundChannelAdapter,而是类似的IntegrationFlows.from(statusSource())

在这种情况下,您需要在 DSL 定义上配置轮询器:

@Bean
IntegrationFlow yourFlow(PollerMetadata myPollerMetadata) {
    return IntegrationFlows.from(statusSource(), e -> e.poller(myPollerMetadata))

注释对Java DSL 和定义@InboundChannelAdapter没有意义。IntegrationFlow


推荐阅读