public interface Dispatcher extends Consumer
MessageHandler
that
will be notified of incoming messages. The dispatcher also has 0 or more subscriptions associated with it.
This means that a group of subscriptions, or subjects, can be combined into a single callback thread. But,
multiple dispatchers can be created to handle different groups of subscriptions/subjects.
All messages to this dispatcher are delivered via a single thread. If the message handler is slow to handle any one message it will delay deliver of subsequent messages. Use separate dispatchers to handle the scenario of a set of messages that require a lot of work and a set of fast moving messages, or create other threads as necessary. The Dispatcher will only use one.
Dispatchers are created from the connection using createDispatcher()
and can be closed using closeDispatcher()
. Closing a dispatcher will
clean up the thread it is using for message deliver.
See the documentation on Consumer
for configuring behavior in a slow consumer situation.
DEFAULT_MAX_BYTES, DEFAULT_MAX_MESSAGES
Modifier and Type | Method and Description |
---|---|
Dispatcher |
subscribe(java.lang.String subject)
Create a subscription to the specified subject under the control of this
dispatcher.
|
Dispatcher |
subscribe(java.lang.String subject,
java.lang.String queue)
Create a subscription to the specified subject and queue under the control of
this dispatcher.
|
Dispatcher |
unsubscribe(java.lang.String subject)
Unsubscribe from the specified subject, the queue is implicit.
|
Dispatcher |
unsubscribe(java.lang.String subject,
int after)
Unsubscribe from the specified subject, the queue is implicit, after the
specified number of messages.
|
clearDroppedCount, drain, getDeliveredCount, getDroppedCount, getPendingByteCount, getPendingByteLimit, getPendingMessageCount, getPendingMessageLimit, isActive, setPendingLimits
Dispatcher subscribe(java.lang.String subject)
This call is a no-op if the dispatcher already has a subscription to the specified subject.
subject
- The subject to subscribe to.java.lang.IllegalStateException
- if the dispatcher was previously closedDispatcher subscribe(java.lang.String subject, java.lang.String queue)
This call is a no-op if the dispatcher already has a subscription to the specified subject (regardless of the queue name).
subject
- The subject to subscribe to.queue
- The queue group to join.java.lang.IllegalStateException
- if the dispatcher was previously closedDispatcher unsubscribe(java.lang.String subject)
Stops messages to the subscription locally and notifies the server.
subject
- The subject to unsubscribe from.java.lang.IllegalStateException
- if the dispatcher was previously closedDispatcher unsubscribe(java.lang.String subject, 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.
For example, to get a single asynchronous message, you might do:
nc = Nats.connect() d = nc.createDispatcher(myHandler); d.subscribe("hello").unsubscribe("hello", 1);
subject
- The subject to unsubscribe from.after
- The number of messages to accept before unsubscribingjava.lang.IllegalStateException
- if the dispatcher was previously closed