I am trying to build a NATS consumer that reads the last message from all subjects and then closes the connection. I need to do this periodically. However, my current implementation reads only up to 500 messages. Even though there are more than 1500 messages.
How can I consume and also ensure it reads the last message from all subjects without this limitation?
My current code
val options = Options.builder() .server("nats://url:port") .authHandler(Nats.credentials("/Users/check/key.txt")) .build() val connection = Nats.connect(options) val js = connection.jetStream() val consumerConfig = ConsumerConfiguration .builder() .filterSubject("subject.>") .deliverPolicy(DeliverPolicy.LastPerSubject) .build() // start the consumer val streamContext: StreamContext = js.getStreamContext("STREAM_NAME") val consumerContext = streamContext.createOrUpdateConsumer(consumerConfig) val fetchConsumeOptions = FetchConsumeOptions.builder().noWait().build() try { consumerContext.fetch(fetchConsumeOptions).use { fetchConsumer -> var count = 0 while (true) { val msg = fetchConsumer.nextMessage() ?: break count++ println("\nMessage Received:") println(msg.subject) println(String(msg.data, StandardCharsets.UTF_8)) msg.ack() } fetchConsumer.stop() println(count) } } catch (e: Exception) { e.printStackTrace() }