public class PahoMqttContext extends java.lang.Object implements MqttContext
MqttContext
that uses Paho.Modifier and Type | Field and Description |
---|---|
protected MqttListener |
listener
The
MqttListener called when data is received. |
QOS_0, QOS_1, QOS_2
Constructor and Description |
---|
PahoMqttContext(PahoMqttAgent mqttAgent) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this
MqttContext , relinquishing any underlying resources. |
java.lang.String |
getLocalTopicPrefix()
Returns the topic prefix to use to publish data sandboxed within the current account.
|
java.lang.String |
getSharedTopicPrefix()
Returns the topic prefix to use to publish data shared with other accounts.
|
DataWriter |
newPublication(java.lang.String topic)
Requests a new publication to the given topic and returns its associated data writer.
|
DataWriter |
newPublication(java.lang.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(java.lang.String topicFilter)
Subscribes to a topic.
|
void |
subscribe(java.lang.String topicFilter,
int qos)
Subscribes to a topic at the given QOS.
|
void |
unsubscribe(java.lang.String topicFilter)
Requests the server to unsubscribe the client from a topic.
|
protected MqttListener listener
MqttListener
called when data is received. May be null.public PahoMqttContext(PahoMqttAgent mqttAgent)
public void close() throws java.io.IOException
MqttContext
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 MqttContext
close
in interface java.lang.AutoCloseable
java.io.IOException
- If an I/O error occurs.public DataWriter newPublication(java.lang.String topic) throws java.io.IOException
MqttContext
This method behaves exactly as if it simply performs the call newPublication(topic,
.MqttContext.QOS_0
, false)
newPublication
in interface MqttContext
topic
- topic to deliver a message to.DataWriter
.java.io.IOException
- if nothing can be published on the MqttContext
.public DataWriter newPublication(java.lang.String topic, int qos, boolean retain) throws java.io.IOException
MqttContext
newPublication
in interface MqttContext
topic
- topic to deliver a message to.qos
- MQTT quality of service (MqttContext.QOS_0
, MqttContext.QOS_1
or MqttContext.QOS_2
).retain
- whether or not the message should be retained by the server.DataWriter
.java.io.IOException
- if nothing can be published on the MqttContext
.public void setListener(MqttListener listener)
MqttContext
By default there is no listener defined for this MqttContext
.
setListener
in interface MqttContext
listener
- the listener. null
to disable notification.public void subscribe(java.lang.String topicFilter) throws java.io.IOException
MqttContext
MqttListener
is called when a message is received.
This method behaves exactly as if it simply performs the call subscribe(topicFilter,
.MqttContext.QOS_0
)
subscribe
in interface MqttContext
topicFilter
- the topic to subscribe to.java.io.IOException
- if subscription fails.public void subscribe(java.lang.String topicFilter, int qos) throws java.io.IOException
MqttContext
MqttListener
is called when a message is received.The given topic filter cannot use wildcards.
subscribe
in interface MqttContext
topicFilter
- the topic to subscribe to.qos
- Quality of Service level at which the client wants to receive the messages (MqttContext.QOS_0
, MqttContext.QOS_1
or MqttContext.QOS_2
).java.io.IOException
- if subscription fails.public void unsubscribe(java.lang.String topicFilter) throws java.io.IOException
MqttContext
unsubscribe
in interface MqttContext
topicFilter
- the topic to unsubscribe from. It must match a topicFilter specified on the subscribe.java.io.IOException
- if unregistering the subscription fails.public java.lang.String getSharedTopicPrefix()
MqttContext
The returned String
ends with a '/'
if it is not empty.
Here is an example of usage:
DataWriter writer = mqttContext.newPublication(mqttContext.getSharedTopicPrefix()+"temperature");
getSharedTopicPrefix
in interface MqttContext
public java.lang.String getLocalTopicPrefix()
MqttContext
The returned String
ends with a '/'
if it is not empty.
Here is an example of usage:
DataWriter writer = mqttContext.newPublication(mqttContext.getLocalTopicPrefix()+"temperature");
getLocalTopicPrefix
in interface MqttContext