How did consumer handle the messages without start a new Thread in NATS-JetStream?

72 views Asked by At

Recently I've been learning NATS-JetStream, and I came across some sample code on GitHub. Here is the simple java code of the NATS client:

This code primarily accomplishes the following task:

  1. declare a MessageHandler and a CountDownLatch.

  2. create a consumer (MessageConsumer) before calling the latch.await() method.

As far as I know, the latch.await() method will block the current Main Thread and put it into a block state.

My first question

Given that the await() method of CountDownLatch blocks the current thread,

How can the handler process message when new messages arrive in the already blocked current thread?

My second question

If I want to run a program that never stops, is it a good idea to use CountDownLatch and not invoke CountDownLatch.countDown(), allowing the consumer to continuously listen to the NATS subject/stream?

I tried running the sample code and wrote my own program to publish some messages to JetStream.

After publishing the first message, the consumer program successfully received the message.

However, when I published the second message, the consumer displayed a "severe: heartbeatAlarm warning" message.

0

There are 0 answers