public abstract class NonBlockingClient extends Object
Example code for using the non-blocking client to send a message
NonBlockingClient.create("amqp://localhost", new NonBlockingClientAdapter() { public void onStarted(NonBlockingClient client, Void context) { SendOptions opts = SendOptions.builder().setQos(QOS.AT_LEAST_ONCE).build(); client.send("/kittens", "Hello kitty!", null, opts, new CompletionListener () { public void onSuccess(NonBlockingClient client, Void context) { client.stop(null, null); } public void onError(NonBlockingClient client, Void context, Exception exception) { client.stop(null, null); } }, null); } public void onDrain(NonBlockingClient client, Void context) {} }, null);
Example code for receiving messages published to the '/kittens' topic.
public static void main(String[] args) { NonBlockingClient client = NonBlockingClient.create("amqp://localhost", null, null); client.subscribe("/kittens", new DestinationAdapter() { public void onMessage(NonBlockingClient client, Void context, Delivery delivery) { switch (delivery.getType()) { case BYTES: BytesDelivery bd = (BytesDelivery)delivery; System.out.println(bd.getData()); break; case STRING: StringDelivery sd = (StringDelivery)delivery; System.out.println(sd.getData()); break; } } }, null, null); }
Modifier and Type | Field and Description |
---|---|
protected static ClientOptions |
defaultClientOptions |
protected static SendOptions |
defaultSendOptions |
protected static SubscribeOptions |
defaultSubscribeOptions |
Constructor and Description |
---|
NonBlockingClient() |
Modifier and Type | Method and Description |
---|---|
static <T> NonBlockingClient |
create(EndpointService endpointService,
CallbackService callbackService,
NetworkService networkService,
TimerService timerService,
com.google.gson.GsonBuilder gsonBuilder,
ClientOptions options,
NonBlockingClientListener<T> listener,
T context)
Creates a new instance of the
NonBlockingClient in starting state. |
static <T> NonBlockingClient |
create(String service,
ClientOptions options,
NonBlockingClientListener<T> listener,
T context)
Creates a new instance of the
NonBlockingClient in starting state. |
static <T> NonBlockingClient |
create(String service,
NonBlockingClientListener<T> listener,
T context)
Creates a new instance of the
NonBlockingClient in starting state. |
abstract String |
getId() |
abstract String |
getService() |
abstract ClientState |
getState() |
boolean |
send(String topic,
ByteBuffer data,
Map<String,Object> properties)
Send a message to the MQ Light server.
|
<T> boolean |
send(String topic,
ByteBuffer data,
Map<String,Object> properties,
CompletionListener<T> listener,
T context)
Send a message to the MQ Light server.
|
abstract <T> boolean |
send(String topic,
ByteBuffer data,
Map<String,Object> properties,
SendOptions sendOptions,
CompletionListener<T> listener,
T context)
Sends a
ByteBuffer to a topic. |
boolean |
send(String topic,
Object json,
Map<String,Object> properties)
Send a message to the MQ Light server.
|
<T> boolean |
send(String topic,
Object json,
Map<String,Object> properties,
CompletionListener<T> listener,
T context)
Sends a message to the MQ Light server.
|
abstract <T> boolean |
send(String topic,
Object json,
Map<String,Object> properties,
SendOptions sendOptions,
CompletionListener<T> listener,
T context)
Sends a JSON object to a topic.
|
boolean |
send(String topic,
Object json,
Type type,
Map<String,Object> properties)
Send a message to the MQ Light server.
|
<T> boolean |
send(String topic,
Object json,
Type type,
Map<String,Object> properties,
CompletionListener<T> listener,
T context)
Sends a message to the MQ Light server.
|
abstract <T> boolean |
send(String topic,
Object json,
Type type,
Map<String,Object> properties,
SendOptions sendOptions,
CompletionListener<T> listener,
T context)
Sends a JSON object to a topic.
|
boolean |
send(String topic,
String data,
Map<String,Object> properties)
Send a message to the MQ Light server.
|
<T> boolean |
send(String topic,
String data,
Map<String,Object> properties,
CompletionListener<T> listener,
T context)
Send a message to the MQ Light server.
|
abstract <T> boolean |
send(String topic,
String data,
Map<String,Object> properties,
SendOptions sendOptions,
CompletionListener<T> listener,
T context)
Sends a string message to a topic.
|
boolean |
sendJson(String topic,
String json,
Map<String,Object> properties)
Send a message to the MQ Light server.
|
<T> boolean |
sendJson(String topic,
String json,
Map<String,Object> properties,
CompletionListener<T> listener,
T context)
Sends a message to the MQ Light server.
|
abstract <T> boolean |
sendJson(String topic,
String json,
Map<String,Object> properties,
SendOptions sendOptions,
CompletionListener<T> listener,
T context)
Sends a String containing JSON data to a topic.
|
abstract <T> NonBlockingClient |
start(CompletionListener<T> listener,
T context)
Requests that the client transitions into started state.
|
abstract <T> void |
stop(CompletionListener<T> listener,
T context)
Requests that the client transitions into stopped state, automatically unsubscribing from
any destinations previously subscribed to using the
subscribe(...) methods. |
<T> NonBlockingClient |
subscribe(String topicPattern,
DestinationListener<T> dstListener,
CompletionListener<T> compListener,
T context)
Subscribes to a destination.
|
abstract <T> NonBlockingClient |
subscribe(String topicPattern,
SubscribeOptions subOptions,
DestinationListener<T> destListener,
CompletionListener<T> compListener,
T context)
Subscribes the client to a destination, based on the supplied topic pattern and
share parameters.
|
<T> NonBlockingClient |
unsubscribe(String topicPattern,
CompletionListener<T> listener,
T context)
Unsubscribes from a destination.
|
<T> NonBlockingClient |
unsubscribe(String topicPattern,
int ttl,
CompletionListener<T> listener,
T context)
Unsubscribes from a destination.
|
abstract <T> NonBlockingClient |
unsubscribe(String topicPattern,
String share,
CompletionListener<T> listener,
T context)
Unsubscribes from a destination.
|
abstract <T> NonBlockingClient |
unsubscribe(String topicPattern,
String share,
int ttl,
CompletionListener<T> listener,
T context)
Unsubscribes from a destination.
|
protected static final ClientOptions defaultClientOptions
protected static final SendOptions defaultSendOptions
protected static final SubscribeOptions defaultSubscribeOptions
public static <T> NonBlockingClient create(String service, ClientOptions options, NonBlockingClientListener<T> listener, T context) throws IllegalArgumentException
NonBlockingClient
in starting state.service
- a URI for the service to connect to, for example: amqp://example.org:5672
.
This URI can start with either amqp://
or amqps://
(for SSL/TLS based
connections). User names and passwords may be embedded into the URL - for example:
amqp://user:pass@example.com
. If a value of null
is specified then
the client will attempt to locate a suitable service based on its environment. Currently it
is capable of locating services in this way when run in the IBM Bluemix environment.options
- a set of options that determine the behaviour of the client.listener
- a listener that is notified of major life-cycle events for the client.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the create method relating to the listener invocation.NonBlockingClient
IllegalArgumentException
- thrown if one or more of the options
is not valid.public static <T> NonBlockingClient create(EndpointService endpointService, CallbackService callbackService, NetworkService networkService, TimerService timerService, com.google.gson.GsonBuilder gsonBuilder, ClientOptions options, NonBlockingClientListener<T> listener, T context) throws IllegalArgumentException
NonBlockingClient
in starting state. The client
will use the set of plugable services, provided as arguments to this method.endpointService
- used to lookup the location of the MQ Light server.callbackService
- used to run each call back into application code.networkService
- used to establish network connections to the MQ Light server.timerService
- used to schedule work to be performed in the future.options
- a set of options that determine the behaviour of the client.listener
- a listener that is notified of major life-cycle events for the client.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the create method relating to the listener invocation.NonBlockingClient
IllegalArgumentException
- thrown if one or more of the options
is not valid.public static <T> NonBlockingClient create(String service, NonBlockingClientListener<T> listener, T context)
NonBlockingClient
in starting state. This is equivalent to calling:
create(service, ClientOptions.create, listener, context);
public abstract String getId()
ClientOptions
object passed in to the create(String, NonBlockingClientListener, Object)
method, or if this value is omitted, then the client ID will be set to a probabilistically
unique string by the client implementation.public abstract String getService()
create(String, NonBlockingClientListener, Object)
call or
null
if the client is not, currently, connected to the MQ Light server.public abstract ClientState getState()
public abstract <T> boolean send(String topic, String data, Map<String,Object> properties, SendOptions sendOptions, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
topic
- the topic to send the message to. Cannot be null.data
- the string data to send to the topic. Cannot be null.properties
- a Map
of properties that will be carried alongside the message. Keys must be non-null and values
must be one of the following types: null
, Boolean
, Byte
,
Short
, Integer
, Long
, Float
, Double
,
byte[]
, and String
.sendOptions
- a set of options that determine how the send operation works.listener
- a listener object that is notified when the send operation completes. For 'at most once' quality of
service messages, this is notified (of success) when the message has been flushed to the network.
For 'at least once' quality of service messages, this is notified (of success) when receipt of the
message has been confirmed by the service.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the send method relating to the listener invocation.boolean
true
if the message was sent
immediately, or false
if the message was buffered in
memory due to a backlog of messages to send over the network)StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public abstract <T> boolean send(String topic, ByteBuffer data, Map<String,Object> properties, SendOptions sendOptions, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
ByteBuffer
to a topic.topic
- the topic to send the message to. Cannot be null.data
- the byte buffer to send to the topic. Cannot be null.properties
- a Map
of properties that will be carried alongside the message. Keys must be non-null and values
must be one of the following types: null
, Boolean
, Byte
,
Short
, Integer
, Long
, Float
, Double
,
byte[]
, and String
.sendOptions
- a set of options that determine exactly how the send operation works.listener
- a listener object that is notified when the send operation completes. For 'at most once' quality of
service messages, this is notified (of success) when the message has been flushed to the network.
For 'at least once' quality of service messages, this is notified (of success) when receipt of the
message has been confirmed by the service.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the send method relating to the listener invocation.boolean
true
if the message was sent
immediately, or false
if the message was buffered in
memory due to a backlog of messages to send over the network)StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public abstract <T> boolean send(String topic, Object json, Map<String,Object> properties, SendOptions sendOptions, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
topic
- the topic to send the message to. Cannot be null.json
- the object to send as a JSON object. The send method will convert this object to JSON using the
Google Gson library to convert the object to JSON (essentially calling Gson.toJson(Object)
to
perform the conversion).properties
- a Map
of properties that will be carried alongside the message. Keys must be non-null and values
must be one of the following types: null
, Boolean
, Byte
,
Short
, Integer
, Long
, Float
, Double
,
byte[]
, and String
.sendOptions
- a set of options that determine exactly how the send operation works.listener
- a listener object that is notified when the send operation completes. For 'at most once' quality of
service messages, this is notified (of success) when the message has been flushed to the network.
For 'at least once' quality of service messages, this is notified (of success) when receipt of the
message has been confirmed by the service.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the send method relating to the listener invocation.boolean
true
if the message was sent
immediately, or false
if the message was buffered in
memory due to a backlog of messages to send over the network)StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public abstract <T> boolean send(String topic, Object json, Type type, Map<String,Object> properties, SendOptions sendOptions, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
topic
- the topic to send the message to. Cannot be null.json
- the object to send as a JSON object. The send method will convert this object to JSON using the
Google Gson library to convert the object to JSON (essentially calling Gson.toJson(Object, Type)
to
perform the conversion).type
- provides type information about the json
object. This allows Java types that make use of
Java Generics to be converted to JSON.properties
- a Map
of properties that will be carried alongside the message. Keys must be non-null and values
must be one of the following types: null
, Boolean
, Byte
,
Short
, Integer
, Long
, Float
, Double
,
byte[]
, and String
.sendOptions
- a set of options that determine exactly how the send operation works.listener
- a listener object that is notified when the send operation completes. For 'at most once' quality of
service messages, this is notified (of success) when the message has been flushed to the network.
For 'at least once' quality of service messages, this is notified (of success) when receipt of the
message has been confirmed by the service.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the send method relating to the listener invocation.boolean
true
if the message was sent
immediately, or false
if the message was buffered in
memory due to a backlog of messages to send over the network)StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public abstract <T> boolean sendJson(String topic, String json, Map<String,Object> properties, SendOptions sendOptions, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
topic
- the topic to send the message to. Cannot be null.json
- a String which is assumed to contain JSON information. No checking is performed on this string, it is
simply transferred as the body of a message which the appropriateproperties
- a Map
of properties that will be carried alongside the message. Keys must be non-null and values
must be one of the following types: null
, Boolean
, Byte
,
Short
, Integer
, Long
, Float
, Double
,
byte[]
, and String
.sendOptions
- a set of options that determine exactly how the send operation works.listener
- a listener object that is notified when the send operation completes. For 'at most once' quality of
service messages, this is notified (of success) when the message has been flushed to the network.
For 'at least once' quality of service messages, this is notified (of success) when receipt of the
message has been confirmed by the service.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the send method relating to the listener invocation.boolean
true
if the message was sent
immediately, or false
if the message was buffered in
memory due to a backlog of messages to send over the network)StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public <T> boolean send(String topic, String data, Map<String,Object> properties, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
send(topic, data, properties, SendOptions.builder().build(), listener, context)
public <T> boolean send(String topic, ByteBuffer data, Map<String,Object> properties, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
send(topic, data, properties, SendOptions.builder().build(), listener, context)
public <T> boolean send(String topic, Object json, Map<String,Object> properties, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
send(topic, json, properties, SendOptions.builder().build(), listener, context)
public <T> boolean send(String topic, Object json, Type type, Map<String,Object> properties, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
send(topic, json, type, properties, SendOptions.builder().build(), listener, context)
public <T> boolean sendJson(String topic, String json, Map<String,Object> properties, CompletionListener<T> listener, T context) throws StoppedException, IllegalArgumentException
sendJson(topic, json, properties, SendOptions.builder().build(), listener, context)
public boolean send(String topic, String data, Map<String,Object> properties) throws StoppedException, IllegalArgumentException
send(topic, data, properties, SendOptions.builder().build(), null, null)
public boolean send(String topic, ByteBuffer data, Map<String,Object> properties) throws StoppedException, IllegalArgumentException
send(topic, data, properties, SendOptions.builder().build(), null, null)
public boolean send(String topic, Object json, Map<String,Object> properties) throws StoppedException, IllegalArgumentException
send(topic, json, properties, SendOptions.builder().build(), null, null)
public boolean send(String topic, Object json, Type type, Map<String,Object> properties) throws StoppedException, IllegalArgumentException
send(topic, json, type, properties, SendOptions.builder().build(), null, null)
public boolean sendJson(String topic, String json, Map<String,Object> properties) throws StoppedException, IllegalArgumentException
send(topic, json, properties, SendOptions.builder().build(), null, null)
public abstract <T> NonBlockingClient start(CompletionListener<T> listener, T context) throws StoppedException
NonBlockingClient
are created in starting
state, this method need
only be called if an instance of the client has been stopped using the
stop(CompletionListener, Object)
method.listener
- this listener will be invoked when the start operation completes. This can
either be when the client has attained started state, or when a subsequent
call to stop results in the client attaining stopped state before it ever
achieves started state.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the start method relating to the listener invocation.NonBlockingClient
that the start method was invoked upon.StoppedException
- if the client is in the process of stopping when this method is invoked.public abstract <T> void stop(CompletionListener<T> listener, T context) throws StartingException
subscribe(...)
methods.
Any messages held by the client, pending transmission to the server, will not be transmitted.listener
- a listener that is notified when the stop operation has completed and
the client has attained stopped state.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the stop method relating to the listener invocation.StartingException
- if the client is in the process of starting when this method is invoked.public abstract <T> NonBlockingClient subscribe(String topicPattern, SubscribeOptions subOptions, DestinationListener<T> destListener, CompletionListener<T> compListener, T context) throws SubscribedException, StoppedException, IllegalArgumentException
topicPattern
parameter is matched against the
topic that messages are sent to, allowing the messaging service to determine whether a
particular message will be delivered to a particular destination, and hence the subscribing client.topicPattern
- the topic pattern to subscribe to. This determines which messages will be
held at the subscribed to destination pending delivery to this client. Cannot be null.subOptions
- a set of options that control the behaviour of the destination subscribed to
and influence how this client receives messages from the destination.destListener
- a listener that is notified when messages arrive at the client and also
when the destination is unsubscribed from using the
unsubscribe(String, String, int, CompletionListener, Object)
method.
Cannot be null.compListener
- a listener that is notified when the subscribe operation completes. If a
value of null
is specified then no-one is notified.context
- a context object that is passed into the listeners registered using this method.
The object supplied can be used within the listener code to identify the specific
instance of the subscribe method relating to the listener invocation.NonBlockingClient
that the unsubscribe method was invoked upon. This is to
allow invocations of the unsubscribe method to be chained.SubscribedException
- if the client is already subscribed to the destination identified by a combination
of the topic pattern and share options.StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public <T> NonBlockingClient subscribe(String topicPattern, DestinationListener<T> dstListener, CompletionListener<T> compListener, T context) throws SubscribedException, StoppedException, IllegalArgumentException
subscribe(topicPattern, new SubscribeOptions(), listener, context)
public abstract <T> NonBlockingClient unsubscribe(String topicPattern, String share, int ttl, CompletionListener<T> listener, T context) throws UnsubscribedException, StoppedException, IllegalArgumentException
topicPattern
- a topic pattern that identifies the destination to unsubscribe from. This must match
one of the topic patterns previously subscribed to using the
subscribe(String, SubscribeOptions, DestinationListener, CompletionListener, Object)
method.share
- a share name that identifies the destination to unsubscribe from. This must match the
share option specified when the corresponding topic pattern was subscribed to. If a value
of null
is supplied for this parameter then the client will attempt to
unsubscribe from a private destination.ttl
- the new time-to-live value to assign to the destination. Currently the only supported value for this
parameter is zero.listener
- a listener that is notified when the unsubscribe operation has completed. Invocation of this
listener is deferred until any messages, buffered pending delivery to a DestiantionListener
registered with the client, have been delivered. If a value of null
is supplied for this
parameter then no notification will be generated.context
- a context object that is passed into the listener. This can be used within the listener code to
identify the specific instance of the stop method relating to the listener invocation.NonBlockingClient
that the unsubscribe method was invoked upon. This is to
allow invocations of the unsubscribe method to be chained.UnsubscribedException
- if an attempt is made to unsubscribe from a destination that the client is not
currently subscribed to.StoppedException
- if the client is in stopped or stopping state when this method is invoked.IllegalArgumentException
- if an invalid value is specified for one of the arguments.public <T> NonBlockingClient unsubscribe(String topicPattern, int ttl, CompletionListener<T> listener, T context) throws UnsubscribedException, StoppedException, IllegalArgumentException
unsubscribe(topicPattern, null, ttl)
public abstract <T> NonBlockingClient unsubscribe(String topicPattern, String share, CompletionListener<T> listener, T context) throws UnsubscribedException, StoppedException, IllegalArgumentException
unsubscribe(String, String, int, CompletionListener, Object)
method without
specifying a ttl (time-to-live) value. This has the effect of unsubscribing from the destination
without changing the ttl value currently assigned to the destination.public <T> NonBlockingClient unsubscribe(String topicPattern, CompletionListener<T> listener, T context) throws UnsubscribedException, StoppedException, IllegalArgumentException
unsubscribe(topicPattern, null, listerner, context)
Copyright © 2015 International Business Machines Corporation. All rights reserved.