@PostConstruct and autowiring a MessageChannel

北城以北 提交于 2019-12-12 17:12:11

问题


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");  
    }
}

And application.yml

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

I also have the following dependencies:

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

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

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

Thanks in advance.


回答1:


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

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



来源:https://stackoverflow.com/questions/44268331/postconstruct-and-autowiring-a-messagechannel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!