简体   繁体   中英

How can understand if event is sent successfully to consumers?

I have a main service that publishes event using Spring Stream Kafka and I have 2 different services that consume this event. These services should consume event in order to complete the process.

Order Service(Publisher-OrderEvent) -- Stock Service(Listener-OrderEvent) -- Payment Service(Listener-OrderEvent)

(Stock check and payment should completed to complete the order)

How can understand if this event is sent successfully to these two services from order service? I need to know if one of them is down or not. If it is down/one of the service cannot receieved the event I need to cancel order in order service. Do you guys have any example implementation for this case?

Thank you in advance.

Option 1: With metrics & records-consumed-total

From the consumer side, monitor this attribute/ metric with the client id :

kafka.consumer:type=consumer-fetch-manager-metrics,client-id="{client-id}"

Look inside the records-consumed-total attribute it will increase once received.


Option 2: With callback

You can also use the call back to verify it was sent

"... Fully non-blocking usage can make use of the Callback parameter to provide a callback that will be invoked when the request is complete . "

 ProducerRecord<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("the-topic", key, value);
 producer.send(myRecord,
               new Callback() {
                   public void onCompletion(RecordMetadata metadata, Exception e) {
                       if(e != null) {
                          e.printStackTrace();
                       } else {
                          //... your code message was received!!
                          System.out.println("The offset of the record we just sent is: " + metadata.offset());
                       }
                   }
               });

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