public interface Subscription extends Consumer
nextMessage()
or the subscription can be owned by a Dispatcher. When a subscription is owned by a dispatcher
it cannot be used to get messages or unsubscribe, those operations must be performed on the dispatcher.
Subscriptions support the concept of auto-unsubscribe. This concept is a bit tricky, as it involves the client library, the server and the Subscriptions history. If told to unsubscribe after 5 messages, a subscription will stop receiving messages when one of the following occurs:
In the case of a reconnect, the remaining message count, as maintained by the subscription, will be sent to the server. So if you unsubscribe with a max of 5, then disconnect after 2, the new server will be told to unsubscribe after 3.
The other, possibly confusing case, is that unsubscribe is based on total messages. So if you make a subscription and receive 5 messages on it, then say unsubscribe with a maximum of 5, the subscription will immediately stop handling messages.
DEFAULT_MAX_BYTES, DEFAULT_MAX_MESSAGES
Modifier and Type | Method and Description |
---|---|
Dispatcher |
getDispatcher() |
java.lang.String |
getQueueName() |
java.lang.String |
getSubject() |
Message |
nextMessage(java.time.Duration timeout)
Read the next message for a subscription, or block until one is available.
|
void |
unsubscribe()
Unsubscribe this subscription and stop listening for messages.
|
Subscription |
unsubscribe(int after)
Unsubscribe this subscription and stop listening for messages, after the
specified number of messages.
|
clearDroppedCount, drain, getDeliveredCount, getDroppedCount, getPendingByteCount, getPendingByteLimit, getPendingMessageCount, getPendingMessageLimit, isActive, setPendingLimits
java.lang.String getSubject()
java.lang.String getQueueName()
Dispatcher getDispatcher()
Message nextMessage(java.time.Duration timeout) throws java.lang.InterruptedException, java.lang.IllegalStateException
Will return null if the calls times out.
Use a timeout of 0 to wait indefinitely. This could still be interrupted if the subscription is unsubscribed or the client connection is closed.
timeout
- the maximum time to waitjava.lang.IllegalStateException
- if the subscription belongs to a dispatcher, or is not activejava.lang.InterruptedException
- if one occurs while waiting for the messagevoid unsubscribe()
Stops messages to the subscription locally and notifies the server.
java.lang.IllegalStateException
- if the subscription belongs to a dispatcher, or is not activeSubscription unsubscribe(int after)
If the subscription has already received after
messages, it will not receive
more. The provided limit is a lifetime total for the subscription, with the caveat
that if the subscription already received more than after
when unsubscribe is called
the client will not travel back in time to stop them.
Supports chaining so that you can do things like:
nc = Nats.connect() m = nc.subscribe("hello").unsubscribe(1).nextMessage(Duration.ZERO);
after
- the number of messages to accept before unsubscribingjava.lang.IllegalStateException
- if the subscription belongs to a dispatcher, or is not active