问题
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