I have a "task" application that is short lived and produces messages to Kafka based on statuses from a database. I'm using spring cloud stream to produce the messages using the below format of my application. I followed this format from the Spring Cloud Stream documentation to send arbitrary data to the output binding.
private EmitterProcessor<Message<GenericRecord>> processor;
@Override
public void run(ApplicationArguments arg0) {
// ... create Message<GenericRecord> producerRecord
this.processor.onNext(producerRecord);
}
@Bean
public Supplier<Flux<Message<GenericRecord>>> supplier() {
return () -> this.processor;
}
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
ctx.close();
}
The application runs, creates the records, runs onNext(), and then exits. I then look to see if any messages have been published but there are none on the topic. I then added a Thread.sleep(10000) after each message is produced and the messages end up on the topic.
After looking at the documentation for Reactor I didn't seen any clear ways to accomplish this. Is there a way to wait for the EmitterProcessor to finish publishing the messages before the Spring application exits?