简体   繁体   中英

Spring Cloud Stream Application Exits Before Supplier Finishes

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?

Do you have a specific reason to use the EmittorProcessor ? I think this use-case can be solved by using StreamBridge . For eg

@Autowired StreamBridge streamBridge;


@Override
public void run(ApplicationArguments arg0) {
    // ... create Message<GenericRecord> producerRecord
    this.streamBridge.send("process-out-0", producerRecord);
}

Then provide configuration for: spring.cloud.stream.source: process You can find more details on StreamBridge in the ref docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM