@PostConstruct并自动装配MessageChannel [英] @PostConstruct and autowiring a MessageChannel

查看:44
本文介绍了@PostConstruct并自动装配MessageChannel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Cloud Stream上遇到问题.事实是,我有一个可在创建Kafka后立即将其写入Kafka(使用@PostConstruct注释的方法)的Bean,因此我自动连接了适当的MessageChannel并在application.yml中设置了目标和绑定器属性.它是这样的:

I'm having a problem with Spring Cloud Stream. The thing is that I have a bean that will write to Kafka as soon as it's created (method annotated with @PostConstruct), so I autowire the appropriate MessageChannel and set the destination and binder properties in application.yml. It goes like this:

@Component
@RequiredArgsConstructor
public class Sender
{
    private final MessageChannel output;

    @PostConstruct
    public void start()
    {
       output.send(new GenericMessage("Hello world");  
    }
}

和application.yml

And application.yml

spring:
    cloud:
        stream:
            bindings:
              output:
                destination: someData
                binder: kafka

我还具有以下依赖性:

- spring-cloud-stream-reactive
- reactor-core
- spring-cloud-starter-stream-kafka

项目本身正在启动,但是尝试在 start()方法中写入 output 时遇到以下异常:

The project itself is starting, but I'm getting the following exception when trying to write to the output in start() method:

Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

是因为Kafka活页夹尚未设法绑定频道还是什么?如果是这样,还有什么替代方法.

Is it because Kafka binder didn't manage to bind the channel yet or what? If so, what is the alternative way to this.

谢谢.

推荐答案

您不能通过 @PostConstruct 执行此操作.太早了.其他组件可能尚未初始化.

You can't do that from the @PostConstruct. It's too early. Other components might be initialized yet.

您必须将发送逻辑移至 SmartLifecycle.start()实现.

You have to move your sending logic to the SmartLifecycle.start() implementation.

这篇关于@PostConstruct并自动装配MessageChannel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆