Class JetStreamMessage

  • All Implemented Interfaces:

    
    public final class JetStreamMessage<T extends Object>
    
                        

    Wrapper for JetStream messages with acknowledgment controls

    Provides type-safe access to message payload and metadata, along with coroutine-friendly acknowledgment operations.

    Example usage:

    js.subscribe<OrderEvent>(stream = "ORDERS", filterSubject = "orders.*").collect { msg ->
        try {
            processOrder(msg.payload)
            msg.ack()
        } catch (e: Exception) {
            if (msg.delivered >= 3) {
                msg.term() // Terminal failure after 3 attempts
            } else {
                msg.nak(delay = 5.seconds) // Retry with backoff
            }
        }
    }
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final T getPayload() The decoded message payload
      final String getSubject() The NATS subject the message was published to
      final String getStream() The JetStream stream name
      final String getConsumer() The consumer name (null for ephemeral consumers without explicit names)
      final Long getDelivered() Number of times this message has been delivered
      final Long getStreamSequence() Sequence number in the stream
      final Long getConsumerSequence() Sequence number for this consumer
      final Instant getTimestamp() When the message was originally published
      final Headers getHeaders() Optional NATS headers attached to the message
      final Long getPendingCount() Number of messages pending for this consumer (0 if unknown)
      final Unit ack() Acknowledge successful processing of the messageTells JetStream that the message has been successfully processed and should not be redelivered.
      final Unit nak(Duration delay) Negatively acknowledge the message (request redelivery)Tells JetStream that processing failed and the message should be redelivered.
      final Unit term() Terminate processing of the messageTells JetStream that the message cannot be processed and should not be redelivered, regardless of maxDeliver setting.
      final Unit inProgress() Signal that processing is still in progressExtends the acknowledgment deadline for long-running message processing.
      final Boolean isRedelivered() Check if this message is a redelivery
      final Long remainingDeliveries(Long maxDeliver) Calculate remaining delivery attempts
      final Boolean hasPending() Check if this message has more pending messages after it
      String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JetStreamMessage

        JetStreamMessage(T payload, String subject, String stream, String consumer, Long delivered, Long streamSequence, Long consumerSequence, Instant timestamp, Headers headers, Long pendingCount, Message msg)
    • Method Detail

      • getPayload

         final T getPayload()

        The decoded message payload

      • getSubject

         final String getSubject()

        The NATS subject the message was published to

      • getConsumer

         final String getConsumer()

        The consumer name (null for ephemeral consumers without explicit names)

      • getDelivered

         final Long getDelivered()

        Number of times this message has been delivered

      • getHeaders

         final Headers getHeaders()

        Optional NATS headers attached to the message

      • getPendingCount

         final Long getPendingCount()

        Number of messages pending for this consumer (0 if unknown)

      • ack

         final Unit ack()

        Acknowledge successful processing of the message

        Tells JetStream that the message has been successfully processed and should not be redelivered. This is the standard acknowledgment for successful processing.

        Thread-safe, and it does not wait for the server: the acknowledgement goes onto the connection's outgoing queue exactly as a publish does - including what happens when that queue is full, see eu.vstoyanov.natsy.NatsKlient.publishBytes. No I/O dispatcher needed. This operation is also idempotent - calling it multiple times on the same message is safe (though subsequent calls will have no effect).

      • nak

         final Unit nak(Duration delay)

        Negatively acknowledge the message (request redelivery)

        Tells JetStream that processing failed and the message should be redelivered. Optionally specify a delay before redelivery to implement backoff strategies.

        If the message has already been delivered the maximum number of times (as configured by maxDeliver), it will not be redelivered again.

        Thread-safe, and it does not wait for the server: the negative acknowledgement goes onto the connection's outgoing queue exactly as a publish does - including what happens when that queue is full, see eu.vstoyanov.natsy.NatsKlient.publishBytes. No I/O dispatcher needed.

        Parameters:
        delay - Optional delay before redelivery (null for immediate redelivery)
      • term

         final Unit term()

        Terminate processing of the message

        Tells JetStream that the message cannot be processed and should not be redelivered, regardless of maxDeliver setting. Use this for messages that are malformed or will never be processable.

        This is different from ack() in that it indicates failure rather than success, which can be useful for monitoring and alerting.

        Thread-safe, and it does not wait for the server: the termination goes onto the connection's outgoing queue exactly as a publish does - including what happens when that queue is full, see eu.vstoyanov.natsy.NatsKlient.publishBytes. No I/O dispatcher needed.

      • inProgress

         final Unit inProgress()

        Signal that processing is still in progress

        Extends the acknowledgment deadline for long-running message processing. Call this periodically during processing to prevent the message from being redelivered due to ackWait timeout.

        Typically used in a loop for operations that take longer than the configured ackWait duration.

        Thread-safe, and it does not wait for the server: the in-progress signal goes onto the connection's outgoing queue exactly as a publish does - including what happens when that queue is full, see eu.vstoyanov.natsy.NatsKlient.publishBytes. No I/O dispatcher needed.

      • isRedelivered

         final Boolean isRedelivered()

        Check if this message is a redelivery

        Returns:

        true if the message has been delivered more than once

      • remainingDeliveries

         final Long remainingDeliveries(Long maxDeliver)

        Calculate remaining delivery attempts

        Parameters:
        maxDeliver - The maximum delivery count configured for the consumer
        Returns:

        Number of remaining delivery attempts, or -1 if unlimited

      • hasPending

         final Boolean hasPending()

        Check if this message has more pending messages after it

        Returns:

        true if there are messages pending for delivery