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>("orders.*").collect { msg -> try { processOrder(msg.payload) msg.ack().getOrThrow() } catch (e: Exception) { if (msg.delivered >= 3) { msg.term().getOrThrow() // Terminal failure after 3 attempts } else { msg.nak(delay = 5.seconds).getOrThrow() // Retry with backoff } } }
-
-
Field Summary
Fields Modifier and Type Field Description private final Tpayloadprivate final Stringsubjectprivate final Stringstreamprivate final Stringconsumerprivate final Longdeliveredprivate final LongstreamSequenceprivate final LongconsumerSequenceprivate final Instanttimestampprivate final Headersheadersprivate final LongpendingCount
-
Method Summary
Modifier and Type Method Description final TgetPayload()The decoded message payload final StringgetSubject()The NATS subject the message was published to final StringgetStream()The JetStream stream name final StringgetConsumer()The consumer name (null for ephemeral consumers without explicit names) final LonggetDelivered()Number of times this message has been delivered final LonggetStreamSequence()Sequence number in the stream final LonggetConsumerSequence()Sequence number for this consumer final InstantgetTimestamp()When the message was originally published final HeadersgetHeaders()Optional NATS headers attached to the message final LonggetPendingCount()Number of messages pending for this consumer (0 if unknown) final Result<Unit>ack()Acknowledge successful processing of the messageTells JetStream that the message has been successfully processed and should not be redelivered. final Result<Unit>nak(Duration delay)Negatively acknowledge the message (request redelivery)Tells JetStream that processing failed and the message should be redelivered. final Result<Unit>term()Terminate processing of the messageTells JetStream that the message cannot be processed and should not be redelivered, regardless of maxDeliver setting. final Result<Unit>inProgress()Signal that processing is still in progressExtends the acknowledgment deadline for long-running message processing. final BooleanisRedelivered()Check if this message is a redelivery final LongremainingDeliveries(Long maxDeliver)Calculate remaining delivery attempts final BooleanhasPending()Check if this message has more pending messages after it StringtoString()-
-
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
-
getStreamSequence
final Long getStreamSequence()
Sequence number in the stream
-
getConsumerSequence
final Long getConsumerSequence()
Sequence number for this consumer
-
getTimestamp
final Instant getTimestamp()
When the message was originally published
-
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 Result<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.
This operation is non-blocking and thread-safe. The NATS client queues the acknowledgment message internally, similar to publish(). 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).
- Returns:
Result indicating success or failure of the acknowledgment
-
nak
final Result<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.
This operation is non-blocking and thread-safe. The NATS client queues the NAK message internally. No I/O dispatcher needed.
- Parameters:
delay- Optional delay before redelivery (null for immediate redelivery)- Returns:
Result indicating success or failure of the negative acknowledgment
-
term
final Result<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.
This operation is non-blocking and thread-safe. The NATS client queues the termination message internally. No I/O dispatcher needed.
- Returns:
Result indicating success or failure of the termination
-
inProgress
final Result<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.
This operation is non-blocking and thread-safe. The NATS client queues the in-progress signal internally. No I/O dispatcher needed.
- Returns:
Result indicating success or failure of the in-progress signal
-
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
-
-
-
-