public interface MqttContext extends AutoCloseable
Modifier and Type | Field and Description |
---|---|
static int |
QOS_0
MQTT Quality of Service level 0: data received at most once.
|
static int |
QOS_1
MQTT Quality of Service level 1: data received at least once.
|
static int |
QOS_2
MQTT Quality of Service level 2: data received exactly once.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this
MqttContext , relinquishing any underlying resources. |
String |
getLocalTopicPrefix()
Returns the topic prefix to use to publish data sandboxed within the current account.
|
String |
getSharedTopicPrefix()
Returns the topic prefix to use to publish data shared with other accounts.
|
DataWriter |
newPublication(String topic)
Requests a new publication to the given topic and returns its associated data writer.
|
DataWriter |
newPublication(String topic,
int qos,
boolean retain)
Requests a new publication to the given topic and returns its associated data writer.
|
void |
setListener(MqttListener listener)
Sets the listener that is called when a message is received.
|
void |
subscribe(String topicFilter)
Subscribes to a topic.
|
void |
subscribe(String topicFilter,
int qos)
Subscribes to a topic at the given QOS.
|
void |
unsubscribe(String topicFilter)
Requests the server to unsubscribe the client from a topic.
|
static final int QOS_0
static final int QOS_1
static final int QOS_2
void close() throws IOException
MqttContext
, relinquishing any underlying resources.
When an MqttContext
has been closed, access to any of its methods that involve an I/O operation will cause an IOException to be thrown.
Closing an already closed MqttContext
has no effect.
MqttListener
registered for this MqttContext
will not be called anymore.
close
in interface AutoCloseable
IOException
- If an I/O error occurs.String getLocalTopicPrefix()
The returned String
ends with a '/'
if it is not empty.
Here is an example of usage:
DataWriter writer = mqttContext.newPublication(mqttContext.getLocalTopicPrefix()+"temperature");
String getSharedTopicPrefix()
The returned String
ends with a '/'
if it is not empty.
Here is an example of usage:
DataWriter writer = mqttContext.newPublication(mqttContext.getSharedTopicPrefix()+"temperature");
DataWriter newPublication(String topic) throws IOException
This method behaves exactly as if it simply performs the call newPublication(topic,
.QOS_0
, false)
topic
- topic to deliver a message to.DataWriter
.IOException
- if nothing can be published on the MqttContext
.NullPointerException
- if the given topic is null
.DataWriter newPublication(String topic, int qos, boolean retain) throws IOException
topic
- topic to deliver a message to.qos
- MQTT quality of service (QOS_0
, QOS_1
or QOS_2
).retain
- whether or not the message should be retained by the server.DataWriter
.IOException
- if nothing can be published on the MqttContext
.NullPointerException
- if the given topic is null
.IllegalArgumentException
- if the QOS is not one of (QOS_0
, QOS_1
or QOS_2
).void setListener(MqttListener listener)
By default there is no listener defined for this MqttContext
.
listener
- the listener. null
to disable notification.void subscribe(String topicFilter) throws IOException
MqttListener
is called when a message is received.
This method behaves exactly as if it simply performs the call subscribe(topicFilter,
.QOS_0
)
topicFilter
- the topic to subscribe to.IOException
- if subscription fails.NullPointerException
- if the given topic filter is null
.void subscribe(String topicFilter, int qos) throws IOException
MqttListener
is called when a message is received.The given topic filter cannot use wildcards.
topicFilter
- the topic to subscribe to.qos
- Quality of Service level at which the client wants to receive the messages (QOS_0
, QOS_1
or QOS_2
).IOException
- if subscription fails.NullPointerException
- if the given topic filter is null
.IllegalArgumentException
- if the QOS is not one of (QOS_0
, QOS_1
or QOS_2
).void unsubscribe(String topicFilter) throws IOException
topicFilter
- the topic to unsubscribe from. It must match a topicFilter specified on the subscribe.IOException
- if unregistering the subscription fails.NullPointerException
- if the given topic filter is null
.