|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eclipse.paho.client.mqttv3.MqttClient
Lightweight client for talking to a server via the MQTT version 3 protocol. The client allows an application to use publish/subscribe messaging.
Constructor Summary | |
---|---|
MqttClient(java.lang.String serverURI,
java.lang.String clientId)
Creates an MqttClient to connect to the specified address, using the specified client identifier. |
|
MqttClient(java.lang.String serverURI,
java.lang.String clientId,
MqttClientPersistence persistence)
Creates an MqttClient to connect to the specified address, using the specified client identifer and persistence implementation. |
Method Summary | |
---|---|
void |
connect()
Connects to a server using the default options. |
void |
connect(MqttConnectOptions options)
Connects to a server using the specified options. |
void |
disconnect()
Disconnects from the server, which quiesces for up to a maximum of thirty seconds, to allow the client to finish any work it currently has. |
void |
disconnect(long quiesceTimeout)
Disconnects from the server. |
static java.lang.String |
generateClientId()
Returns a randomly generated client identifier based on the current user's login name and the system time. |
java.lang.String |
getClientId()
Returns the client ID used by this client. |
MqttDeliveryToken[] |
getPendingDeliveryTokens()
Returns the delivery tokens for any outstanding publish operations. |
java.lang.String |
getServerURI()
Returns the address of the server used by this client, as a URI. |
MqttTopic |
getTopic(java.lang.String topic)
Gets a topic object which can be used to publish messages. |
boolean |
isConnected()
Determines if this client is currently connected to the server. |
void |
setCallback(MqttCallback callback)
Sets the callback listener to use for asynchronously received messages. |
void |
subscribe(java.lang.String topicFilter)
Subscribes to a topic, which may include wildcards, using the default options. |
void |
subscribe(java.lang.String[] topicFilters)
Subscribes to multiple topics, each of which may include wildcards, using the default options. |
void |
subscribe(java.lang.String[] topicFilters,
int[] qos)
Subscribes to multiple topics, each of which may include wildcards, using the specified options. |
void |
subscribe(java.lang.String topicFilter,
int qos)
Subscribes to a topic, which may include wildcards, using the specified options. |
void |
unsubscribe(java.lang.String topicFilter)
Unsubscribes from a topic. |
void |
unsubscribe(java.lang.String[] topicFilters)
Unsubscribes from multiple topics. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public MqttClient(java.lang.String serverURI, java.lang.String clientId) throws MqttException
tcp://localhost:1883
ssl://localhost:8883
If the port is not specified, it will default to 1883 for "tcp://" URIs, and 8883 for "ssl://" URIs.
The client identifier should be unique across all clients connecting to the same
server. A convenience method is provided to generate a random client id that
should satisfy this criteria - generateClientId()
. As the client identifier
is used by the server to identify a client when it reconnects, the client must use the
same identifier between connections if durable subscriptions are to be used.
In Java SE, SSL can be configured in one of several ways, which the client will use in the following order:
SSLSocketFactory
- applications can
use MqttConnectOptions.setSocketFactory(SocketFactory)
to supply
a factory with the appropriate SSL settings.MqttConnectOptions.setSSLProperties(Properties)
.In Java ME, the platform settings are used for SSL connections.
A default instance of MqttDefaultFilePersistence
is used by
the client. To specify a different persistence implementation, or to turn
off persistence, use the MqttClient(String, String, MqttClientPersistence)
constructor.
serverURI
- the address to connect to, specified as a URIclientId
- the client ID to use
java.lang.IllegalArgumentException
- if the URI does not start with
"tcp://", "ssl://" or "local://".
java.lang.IllegalArgumentException
- if the clientId is null or is greater than 23 characters in length
MqttException
- if any other problem was encounteredpublic MqttClient(java.lang.String serverURI, java.lang.String clientId, MqttClientPersistence persistence) throws MqttException
tcp://localhost:1883
ssl://localhost:8883
local://FirstBroker
If the port is not specified, it will default to 1883 for "tcp://" URIs, and 8883 for "ssl://" URIs.
The client identifier should be unique across all clients connecting to the same
server. A convenience method is provided to generate a random client id that
should satisfy this criteria - generateClientId()
. As the client identifier
is used by the server to identify a client when it reconnects, the client must use the
same identifier between connections if durable subscriptions are to be used.
In Java SE, SSL can be configured in one of several ways, which the client will use in the following order:
SSLSocketFactory
- applications can
use MqttConnectOptions.setSocketFactory(SocketFactory)
to supply
a factory with the appropriate SSL settings.MqttConnectOptions.setSSLProperties(Properties)
.In Java ME, the platform settings are used for SSL connections.
The persistence mechanism is used to enable reliable messaging. For qualities of server (QoS) 1 or 2 to work, messages must be persisted to disk by both the client and the server. If this is not done, then a failure in the client or server will result in lost messages. It is the application's responsibility to provide an implementation of theMqttClientPersistence
interface, which the client can use to
persist messages. If the application is only sending QoS 0 messages,
then this is not needed.
An implementation of file-based persistence is provided in the
class MqttDefaultFilePersistence
.
If no persistence is needed, it can be explicitly set to null
.
serverURI
- the address to connect to, specified as a URIclientId
- the client ID to usepersistence
- the persistence mechanism to use.
java.lang.IllegalArgumentException
- if the URI does not start with
"tcp://", "ssl://" or "local://".
java.lang.IllegalArgumentException
- if the clientId is null or is greater than 23 characters in length
MqttException
- if any other problem was encounteredMethod Detail |
public void connect() throws MqttSecurityException, MqttException
setCallback(MqttCallback)
prior to
connecting.
MqttSecurityException
MqttException
public void connect(MqttConnectOptions options) throws MqttSecurityException, MqttException
setCallback(MqttCallback)
prior to
connecting.
MqttSecurityException
MqttException
public void disconnect() throws MqttException
MqttException
disconnect(long)
public void disconnect(long quiesceTimeout) throws MqttException
MqttCallback
methods.
Firstly, the client will wait for all MqttCallback
methods to
complete. It will then quiesce for the specified time, to allow for
work which has already been accepted to complete - for example, it will
wait for the QoS 2 flows from earlier publications to complete. After
the quiesce timeout, the client will disconnect from the server. When
the client is next connected, any QoS 1 or 2 messages which have not
completed will be retried.
quiesceTimeout
- the amount of time in milliseconds to allow for existing work to finish
before disconnecting. A value of zero or less means the client will
not quiesce.
MqttException
public boolean isConnected()
true
if connected, false
otherwise.public java.lang.String getClientId()
public java.lang.String getServerURI()
MqttClient(String, String)
public MqttTopic getTopic(java.lang.String topic)
When you build an application, the design of the topic tree should take into account the following principles of topic name syntax and semantics:
The following principles apply to the construction and content of a topic tree:
getTopic
in interface org.eclipse.paho.client.mqttv3.internal.DestinationProvider
topic
- the topic to use, for example "finance/stock/ibm".
java.lang.IllegalArgumentException
- if the topic contains a '+' or '#'
wildcard character.public void subscribe(java.lang.String topicFilter) throws MqttException, MqttSecurityException
setCallback(MqttCallback)
method should be called
before this method, otherwise any received messages will be discarded.
MqttException
MqttSecurityException
subscribe(String[], int[])
public void subscribe(java.lang.String[] topicFilters) throws MqttException, MqttSecurityException
setCallback(MqttCallback)
method should be called
before this method, otherwise any received messages will be discarded.
MqttException
MqttSecurityException
subscribe(String[], int[])
public void subscribe(java.lang.String topicFilter, int qos) throws MqttException, MqttSecurityException
setCallback(MqttCallback)
method should be called
before this method, otherwise any received messages will be discarded.
topicFilter
- the topic to subscribe to, which can include wildcards.qos
- the quality of service at which to subscribe.
MqttException
MqttSecurityException
subscribe(String[], int[])
public void subscribe(java.lang.String[] topicFilters, int[] qos) throws MqttException, MqttSecurityException
setCallback(MqttCallback)
method should be called
before this method, otherwise any received messages will be discarded.
The "topic filter" string used when subscribing may contain special characters, which allow you to subscribe to multiple topics at once.
The topic level separator is used to introduce structure into the topic, and can therefore be specified within the topic for that purpose. The multi-level wildcard and single-level wildcard can be used for subscriptions, but they cannot be used within a topic by the publisher of a message.
The number sign (#) is a wildcard character that matches any number of levels within a topic. For example, if you subscribe to finance/stock/ibm/#, you receive messages on these topics:
finance/stock/ibm
finance/stock/ibm/closingprice
finance/stock/ibm/currentprice
The multi-level wildcard can represent zero or more levels. Therefore, finance/# can also match the singular finance, where # represents zero levels. The topic level separator is meaningless in this context, because there are no levels to separate.
The multi-level wildcard can be specified only on its own or next to the topic level separator character. Therefore, # and finance/# are both valid, but finance# is not valid. The multi-level wildcard must be the last character used within the topic tree. For example, finance/# is valid but finance/#/closingprice is not valid.
The plus sign (+) is a wildcard character that matches only one topic level. For example, finance/stock/+ matches finance/stock/ibm and finance/stock/xyz, but not finance/stock/ibm/closingprice. Also, because the single-level wildcard matches only a single level, finance/+ does not match finance.
Use the single-level wildcard at any level in the topic tree, and in conjunction with the multilevel wildcard. Specify the single-level wildcard next to the topic level separator, except when it is specified on its own. Therefore, + and finance/+ are both valid, but finance+ is not valid. The single-level wildcard can be used at the end of the topic tree or within the topic tree. For example, finance/+ and finance/+/ibm are both valid.
topicFilters
- the topics to subscribe to, which can include wildcards.qos
- the qualities of service levels at which to subscribe.
MqttException
- if there was an error registering the subscription.
java.lang.IllegalArgumentException
- if the two supplied arrays are not the same size.
MqttSecurityException
public void unsubscribe(java.lang.String topicFilter) throws MqttException
topicFilter
- the topic to unsubscribe from.
MqttException
public void unsubscribe(java.lang.String[] topicFilters) throws MqttException
topicFilters
- the topics to unsubscribe from.
MqttException
public void setCallback(MqttCallback callback) throws MqttException
MqttCallback.messageArrived(MqttTopic, MqttMessage)
method will be called back whenever a message arrives.
callback
- the class to callback when a message arrives.
MqttException
public static java.lang.String generateClientId()
When cleanSession is set to false, an application should ensure it uses the same client identifier when it reconnects to the server to resume state and maintain assured message delivery.
MqttConnectOptions.setCleanSession(boolean)
public MqttDeliveryToken[] getPendingDeliveryTokens()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |