public interface Nats extends Closeable
This class is fully thread-safe.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this Nats instance.
|
boolean |
isClosed()
Indicates if this client has been closed.
|
boolean |
isConnected()
Indicates if this client is connected to a NATS server.
|
void |
publish(String subject)
Publishes a message with an empty body to the specified subject.
|
Registration |
publish(String subject,
long period,
TimeUnit unit)
Publishes a message with an empty body to the specified subject on a recurring basis according to the specified
period.
|
void |
publish(String subject,
String body)
Publishes a message with the provided body to the specified subject.
|
Registration |
publish(String subject,
String body,
long period,
TimeUnit unit)
Publishes a message with the provided body to the specified subject on a recurring basis according to the
specified period.
|
void |
publish(String subject,
String body,
String replyTo)
Publishes a message with the provided body to the specified subject.
|
Registration |
publish(String subject,
String body,
String replyTo,
long period,
TimeUnit unit)
Publishes a message with the provided body to the specified subject on a recurring basis according to the
specified period.
|
Request |
request(String subject,
long timeout,
TimeUnit unit,
MessageHandler... messageHandlers)
Sends a request on the given subject.
|
Request |
request(String subject,
String message,
long timeout,
TimeUnit unit,
Integer maxReplies,
MessageHandler... messageHandlers)
Sends a request body on the given subject and will except a specified number of replies.
|
Request |
request(String subject,
String message,
long timeout,
TimeUnit unit,
MessageHandler... messageHandlers)
Sends a request body on the given subject.
|
Subscription |
subscribe(String subject,
Integer maxMessages,
MessageHandler... messageHandlers)
Subscribes to the specified subject and will automatically unsubscribe after the specified number of messages
arrives.
|
Subscription |
subscribe(String subject,
MessageHandler... messageHandlers)
Subscribes to the specified subject.
|
Subscription |
subscribe(String subject,
String queueGroup,
Integer maxMessages,
MessageHandler... messageHandlers)
Subscribes to the specified subject within a specific queue group and will automatically unsubscribe after the
specified number of messages arrives.
|
Subscription |
subscribe(String subject,
String queueGroup,
MessageHandler... messageHandlers)
Subscribes to the specified subject within a specific queue group.
|
boolean isConnected()
false after the
close() method has been invoked. If the connection to the server is lost, the client will automatically
try to reconnect to a server unless, of course, this feature has been disabled.true if the client is connected to a NATS server, false otherwise.boolean isClosed()
true if close() was called (explicitly or implicitly due to an error), false
otherwise.void close()
close in interface AutoCloseableclose in interface Closeablevoid publish(String subject)
Nats instance is not currently
connected to a NATS server, the message will be queued up to be published once a connection is established.subject - the subject to which the message will be publishedvoid publish(String subject, String body)
Nats instance is not
currently connected to a NATS server, the message will be queued up to be published once a connection is
established.subject - the subject to which the message will be publishedbody - the message body to publishvoid publish(String subject, String body, String replyTo)
Nats instance is not currently connected to a NATS server, the
message will be queued up to be published once a connection is established.subject - the subject to which the message will be publishedbody - the message body to publishreplyTo - the subject replies to this body should be sent to.Registration publish(String subject, long period, TimeUnit unit)
Nats instance is not currently connected to a NATS server, the message will not be sent
or queued up.subject - the subject to which the message will be publishedperiod - the period between successive publishesunit - the time unit of the period parameterRegistration object that can be used to cancel repeatedly publishing the message.Registration publish(String subject, String body, long period, TimeUnit unit)
Nats instance is not currently connected to a NATS server, the message will
not be sent or queued up.subject - the subject to which the message will be publishedbody - the message body to publishperiod - the period between successive publishesunit - the time unit of the period parameterRegistration object that can be used to cancel recurring publishing.Registration publish(String subject, String body, String replyTo, long period, TimeUnit unit)
Nats instance is not currently connected to a NATS server, the message will not be sent or queued up.subject - the subject to which the message will be publishedbody - the message body to publishreplyTo - the subject replies to this body should be sent to.period - the period between successive publishesunit - the time unit of the period parameterRegistration object that can be used to cancel recurring publishing.Subscription subscribe(String subject, MessageHandler... messageHandlers)
subject - the subject to subscribe tomessageHandlers - the MessageHandlers to listen for incoming messages.Subscription object used for interacting with the subscriptionsubscribe(String, String, Integer,MessageHandler...)Subscription subscribe(String subject, String queueGroup, MessageHandler... messageHandlers)
subject - the subject to subscribe toqueueGroup - the queue group the subscription participates inmessageHandlers - the MessageHandlers to listen for incoming messages.Subscription object used for interacting with the subscriptionsubscribe(String, String, Integer,MessageHandler...)Subscription subscribe(String subject, Integer maxMessages, MessageHandler... messageHandlers)
subject - the subject to subscribe tomaxMessages - the maximum number of messages received by the subscriptionmessageHandlers - the MessageHandlers to listen for incoming messagesSubscription object used for interacting with the subscriptionsubscribe(String, String, Integer,MessageHandler...)Subscription subscribe(String subject, String queueGroup, Integer maxMessages, MessageHandler... messageHandlers)
The subject may contain wild cards. "*" matches any token, at any level of the subject. For example:
"foo.*.baz" matches "foo.bar.baz, foo.a.baz, etc.
"*.bar" matches "foo.bar", "baz.bar", etc.
"*.bar.*" matches "foo.bar.baz", "foo.bar.foo", etc.
">" matches any length of the tail of a subject and can only be the last token. For examples,
'foo.>' will match 'foo.bar', 'foo.bar.baz', 'foo.foo.bar.bax.22'. A subject of
simply ">" will match all messages.
All subscriptions with the same queueGroup will form a queue group. Each body will be delivered to
only one subscriber per queue group.
subject - the subject to subscribe toqueueGroup - the queue group the subscription participates inmaxMessages - the maximum number of messages received by the subscriptionmessageHandlers - the MessageHandlers to listen for incoming messages.Subscription object used for interacting with the subscriptionRequest request(String subject, long timeout, TimeUnit unit, MessageHandler... messageHandlers)
subject - the subject to send the request ontimeout - the amount of time to wait for repliesunit - the unit of time to waitmessageHandlers - the MessageHandlers to listen for incoming messages.Request instance associated with the request.request(String, String, long, java.util.concurrent.TimeUnit, Integer, MessageHandler...)Request request(String subject, String message, long timeout, TimeUnit unit, MessageHandler... messageHandlers)
subject - the subject to send the request onmessage - the content of the requesttimeout - the amount of time to wait for repliesunit - the unit of time to waitmessageHandlers - the MessageHandlers to listen for incoming messages.Request instance associated with the request.request(String, String, long, java.util.concurrent.TimeUnit, Integer, MessageHandler...)Request request(String subject, String message, long timeout, TimeUnit unit, Integer maxReplies, MessageHandler... messageHandlers)
Invoking this method is roughly equivalent to the following:
String replyTo = Nats.createInbox();
Subscription subscription = nats.subscribe(replyTo, maxReplies);
Publication publication = nats.publish(subject, body, replyTo);
that returns a combination of Subscription and Publication as a Request object.
subject - the subject to send the request onmessage - the content of the requesttimeout - the amount of time to wait for repliesunit - the unit of time to waitmaxReplies - the maximum number of replies that the request will accept before automatically closing,
null for unlimited repliesmessageHandlers - the MessageHandlers to listen for incoming messages.Request instance associated with the request.Copyright © 2015. All Rights Reserved.