各种MQTT Server单台能支持多少mqtt client java

各种MQTT Server单台能支持多少client_百度知道
各种MQTT Server单台能支持多少client
我想每个Android开发人员对它应该都是比较熟悉的。 MQ 遥测传输 (MQTT) 是轻量级基于代理的发布&#47,设计思想是开放;订阅的消息传输协议、轻量、简单、易于实现。这些特点使它适用于受限环境MQTT协议实现android消息推送
来自团队:
其他类似问题
为您推荐:
server的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁3127人阅读
最新maven 依赖&dependency&
&groupId&org.fusesource.mqtt-client&/groupId&
&artifactId&mqtt-client&/artifactId&
&version&1.10&/version&
&/dependency&
Server发布主题
package com.etrip.mqtt.
import java.net.URISyntaxE
import org.fusesource.mqtt.client.FutureC
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.T
import org.slf4j.L
import org.slf4j.LoggerF
* 采用Future式 发布主题
* @author longgangbai
public class MQTTFutureServer {
private static final Logger LOG = LoggerFactory.getLogger(MQTTFutureServer.class);
private final static String CONNECTION_STRING = &tcp://192.168.208.46:1883&;
private final static boolean CLEAN_START =
private final static short KEEP_ALIVE = 30;// 低耗网络,但是又需要及时获取数据,心跳30s
static Topic[] topics = {
new Topic(&china/beijing&, QoS.EXACTLY_ONCE),
new Topic(&china/tianjin&, QoS.AT_LEAST_ONCE),
new Topic(&china/henan&, QoS.AT_MOST_ONCE)};
public final
static long RECONNECTION_ATTEMPT_MAX=6;
public final
static long RECONNECTION_DELAY=2000;
public final static int SEND_BUFFER_SIZE=2*;//发送最大缓冲为2M
public static void main(String[] args)
MQTT mqtt = new MQTT();
//设置服务端的ip
mqtt.setHost(CONNECTION_STRING);
//连接前清空会话信息
mqtt.setCleanSession(CLEAN_START);
//设置重新连接的次数
mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);
//设置重连的间隔时间
mqtt.setReconnectDelay(RECONNECTION_DELAY);
//设置心跳时间
mqtt.setKeepAlive(KEEP_ALIVE);
//设置缓冲的大小
mqtt.setSendBufferSize(SEND_BUFFER_SIZE);
//创建连接
final FutureConnection connection= mqtt.futureConnection();
connection.connect();
int count=1;
while(true){
// 用于发布消息,目前手机段不需要向服务端发送消息
//主题的内容
String message=&hello &+count+&chinese people !&;
String topic = &china/beijing&;
connection.publish(topic, message.getBytes(), QoS.AT_LEAST_ONCE,
System.out.println(&MQTTFutureServer.publish Message &+&Topic Title :&+topic+& context :&+message);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Client接收主题
package com.etrip.mqtt.
import java.net.URISyntaxE
import org.fusesource.mqtt.client.F
import org.fusesource.mqtt.client.FutureC
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.M
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.T
import org.slf4j.L
import org.slf4j.LoggerF
* MQTT moquette 的Client 段用于订阅主题,并接收主题信息
* 采用Future 式 订阅主题
* @author longgangbai
public class MQTTFutureClient {
private static final Logger LOG = LoggerFactory.getLogger(MQTTFutureClient.class);
private final static String CONNECTION_STRING = &tcp://192.168.208.46:1883&;
private final static boolean CLEAN_START =
private final static short KEEP_ALIVE = 30;// 低耗网络,但是又需要及时获取数据,心跳30s
private final static String CLIENT_ID = &publishService&;
static Topic[] topics = {
new Topic(&china/beijing&, QoS.EXACTLY_ONCE),
new Topic(&china/tianjin&, QoS.AT_LEAST_ONCE),
new Topic(&china/henan&, QoS.AT_MOST_ONCE)};
public final
static long RECONNECTION_ATTEMPT_MAX=6;
public final
static long RECONNECTION_DELAY=2000;
public final static int SEND_BUFFER_SIZE=2*;//发送最大缓冲为2M
public static void main(String[] args)
//创建MQTT对象
MQTT mqtt = new MQTT();
//设置mqtt broker的ip和端口
mqtt.setHost(CONNECTION_STRING);
//连接前清空会话信息
mqtt.setCleanSession(CLEAN_START);
//设置重新连接的次数
mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);
//设置重连的间隔时间
mqtt.setReconnectDelay(RECONNECTION_DELAY);
//设置心跳时间
mqtt.setKeepAlive(KEEP_ALIVE);
//设置缓冲的大小
mqtt.setSendBufferSize(SEND_BUFFER_SIZE);
//获取mqtt的连接对象BlockingConnection
final FutureConnection connection= mqtt.futureConnection();
connection.connect();
connection.subscribe(topics);
while(true){
Future&Message& futrueMessage=connection.receive();
Message message =futrueMessage.await();
System.out.println(&MQTTFutureClient.Receive Message &+ &Topic Title :&+message.getTopic()+& context :&+String.valueOf(message.getPayloadBuffer()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:195638次
积分:2668
积分:2668
排名:第8691名
原创:66篇
转载:52篇
评论:44条
(3)(4)(2)(1)(2)(4)(13)(12)(4)(4)(7)(5)(7)(9)(11)(4)(6)(2)(1)(3)(1)(3)(1)(2)(4)(4)MqttAndroidClient
JavaScript is disabled on your browser.
Class MqttAndroidClient
java.lang.Object
BroadcastReceiver
org.eclipse.paho.android.service.MqttAndroidClient
All Implemented Interfaces:
org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public class MqttAndroidClient
extends BroadcastReceiver
implements org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Enables an android application to communicate with an MQTT server using non-blocking methods.
Implementation of the MQTT asynchronous client interface IMqttAsyncClient , using the MQTT
android service to actually interface with MQTT server. It provides android applications a simple programming interface to all features of the MQTT version 3.1
specification including:
unsubscribe
disconnect
Nested Class Summary
Nested Classes&
Modifier and Type
Class and Description
static class&
The Acknowledgment mode for messages received from MqttCallback.messageArrived(String, MqttMessage)
Field Summary
Modifier and Type
Field and Description
(package private) Context
Constructor Summary
Constructors&
Constructor and Description
(Context&context,
java.lang.String&serverURI,
java.lang.String&clientId)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
(Context&ctx,
java.lang.String&serverURI,
java.lang.String&clientId,
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
(Context&ctx,
java.lang.String&serverURI,
java.lang.String&clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence&persistence)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
(Context&context,
java.lang.String&serverURI,
java.lang.String&clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence&persistence,
constructor
Method Summary
Modifier and Type
Method and Description
(java.lang.String&messageId)
Acknowledges a message received on the MqttCallback.messageArrived(String, MqttMessage)
Close the client
Releases all resource associated with the client.
org.eclipse.paho.client.mqttv3.IMqttToken
Connects to an MQTT server using the default options.
org.eclipse.paho.client.mqttv3.IMqttToken
(org.eclipse.paho.client.mqttv3.MqttConnectOptions&options)
Connects to an MQTT server using the provided connect options.
org.eclipse.paho.client.mqttv3.IMqttToken
(org.eclipse.paho.client.mqttv3.MqttConnectOptions&options,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Connects to an MQTT server using the specified options.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Connects to an MQTT server using the default options.
org.eclipse.paho.client.mqttv3.IMqttToken
Disconnects from the server.
org.eclipse.paho.client.mqttv3.IMqttToken
(long&quiesceTimeout)
Disconnects from the server.
org.eclipse.paho.client.mqttv3.IMqttToken
(long&quiesceTimeout,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Disconnects from the server.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Disconnects from the server.
(long&disconnectTimeout)&
(long&quiesceTimeout,
long&disconnectTimeout)&
java.lang.String
Returns the client ID used by this client.
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[]
Returns the delivery tokens for any outstanding publish operations.
org.eclipse.paho.client.mqttv3.MqttProtocolVersion
java.lang.String
Returns the URI address of the server used by this client.
javax.net.ssl.SSLSocketFactory
(java.io.InputStream&keyStore,
java.lang.String&password)
Get the SSLSocketFactory using SSL key store and password
Determines if this client is currently connected to the server.
(Context&context,
Intent&intent)
Process incoming Intent objects representing the results of operations
and asynchronous activities such as message received
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
(java.lang.String&topic,
byte[]&payload,
boolean&retained)
Publishes a message to a topic on the server.
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
(java.lang.String&topic,
byte[]&payload,
boolean&retained,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Publishes a message to a topic on the server.
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
(java.lang.String&topic,
org.eclipse.paho.client.mqttv3.MqttMessage&message)
Publishes a message to a topic on the server.
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
(java.lang.String&topic,
org.eclipse.paho.client.mqttv3.MqttMessage&message,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Publishes a message to a topic on the server.
(Context&context)
Register receiver to receiver intent from MqttService.
(org.eclipse.paho.client.mqttv3.MqttCallback&callback)
Sets a callback listener to use for events that happen asynchronously.
(org.eclipse.paho.client.mqttv3.MqttProtocolVersion&version)&
(&traceCallback)
identify the callback to be invoked when making tracing calls back into
the Activity
(boolean&traceEnabled)
turn tracing on and off
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String[]&topic,
int[]&qos)
Subscribe to multiple topics, each of which may include wildcards.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String[]&topic,
int[]&qos,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Subscribes to multiple topics, each of which may include wildcards.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String&topic,
Subscribe to a topic, which may include wildcards.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Subscribe to a topic, which may include wildcards.
Unregister receiver which receives intent from MqttService avoids IntentReceiver
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String&topic)
Requests the server unsubscribe the client from a topic.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String[]&topic)
Requests the server unsubscribe the client from one or more topics.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String[]&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Requests the server unsubscribe the client from one or more topics.
org.eclipse.paho.client.mqttv3.IMqttToken
(java.lang.String&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
Requests the server unsubscribe the client from a topics.
Methods inherited from class&java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Field Detail
Context myContext
Constructor Detail
MqttAndroidClient
public&MqttAndroidClient(Context&context,
java.lang.String&serverURI,
java.lang.String&clientId)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
Parameters:context - object used to pass context to the callback.serverURI - specifies the protocol, host name and port to be used to
connect to an MQTT serverclientId - specifies the name by which this connection should be
identified to the server
MqttAndroidClient
public&MqttAndroidClient(Context&ctx,
java.lang.String&serverURI,
java.lang.String&clientId,
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
Parameters:ctx - Application's contextserverURI - specifies the protocol, host name and port to be used to connect to an MQTT serverclientId - specifies the name by which this connection should be identified to the serverackType - how the application wishes to acknowledge a message has been processed
MqttAndroidClient
public&MqttAndroidClient(Context&ctx,
java.lang.String&serverURI,
java.lang.String&clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence&persistence)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
Parameters:ctx - Application's contextserverURI - specifies the protocol, host name and port to be used to connect to an MQTT serverclientId - specifies the name by which this connection should be identified to the serverpersistence - The object to use to store persisted data
MqttAndroidClient
public&MqttAndroidClient(Context&context,
java.lang.String&serverURI,
java.lang.String&clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence&persistence,
constructor
Parameters:context - used to pass context to the callback.serverURI - specifies the protocol, host name and port to be used to
connect to an MQTT serverclientId - specifies the name by which this connection should be
identified to the serverpersistence - the persistence class to use to store in-flight message. If null then the
default persistence mechanism is usedackType - how the application wishes to acknowledge a message has been processed.
Method Detail
isConnected
public&boolean&isConnected()
Determines if this client is currently connected to the server.
Specified by:
isConnected&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:true if connected, false otherwise.
getClientId
public&java.lang.String&getClientId()
Returns the client ID used by this client.
All clients connected to the
same server or server farm must have a unique ID.
Specified by:
getClientId&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:the client ID used by this client.
getServerURI
public&java.lang.String&getServerURI()
Returns the URI address of the server used by this client.
The format of the returned String is the same as that used on the constructor.
Specified by:
getServerURI&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:the server's address, as a URI String.
public&void&close()
Close the client
Releases all resource associated with the client. After the client has
been closed it cannot be reused. For instance attempts to connect will fail.
Specified by:
close&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException - if the client is not disconnected.
public&org.eclipse.paho.client.mqttv3.IMqttToken&connect()
throws org.eclipse.paho.client.mqttv3.MqttException
Connects to an MQTT server using the default options.
The default options are specified in MqttConnectOptions class.
Specified by:
connect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:token used to track and wait for the connect to complete. The token
will be passed to the callback methods if a callback is set.
org.eclipse.paho.client.mqttv3.MqttException - for any connected problemsSee Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions&options)
throws org.eclipse.paho.client.mqttv3.MqttException
Connects to an MQTT server using the provided connect options.
The connection will be established using the options specified in the
MqttConnectOptions parameter.
Specified by:
connect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:options - a set of connection parameters that override the defaults.
Returns:token used to track and wait for the connect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for any connected problemsSee Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&connect(java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Connects to an MQTT server using the default options.
The default options are specified in MqttConnectOptions class.
Specified by:
connect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when the connect completes. Use
null if not required.
Returns:token used to track and wait for the connect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for any connected problemsSee Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions&options,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Connects to an MQTT server using the specified options.
The server to connect to is specified on the constructor.
It is recommended to call
connecting in order that messages destined for the client can be accepted
as soon as the client is connected.
The method returns control before the connect completes. Completion can
be tracked by:
Waiting on the returned token IMqttToken.waitForCompletion() or
Passing in a callback IMqttActionListener
Specified by:
connect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:options - a set of connection parameters that override the defaults.userContext - optional object for used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when the connect completes. Use
null if not required.
Returns:token used to track and wait for the connect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for any connected problems, including communication errors
disconnect
public&org.eclipse.paho.client.mqttv3.IMqttToken&disconnect()
throws org.eclipse.paho.client.mqttv3.MqttException
Disconnects from the server.
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of 30 seconds for work to quiesce before disconnecting.
This method must not be called from inside MqttCallback methods.
Specified by:
disconnect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:token used to track and wait for disconnect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for problems encountered while disconnectingSee Also:
disconnect
public&org.eclipse.paho.client.mqttv3.IMqttToken&disconnect(long&quiesceTimeout)
throws org.eclipse.paho.client.mqttv3.MqttException
Disconnects from the server.
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of the specified quiesce time
for work to complete before disconnecting.
This method must not be called from inside MqttCallback methods.
Specified by:
disconnect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters: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.
Returns:token used to track and wait for disconnect to complete. The token
will be passed to the callback methods if a callback is set.
org.eclipse.paho.client.mqttv3.MqttException - for problems encountered while disconnectingSee Also:
disconnect
public&org.eclipse.paho.client.mqttv3.IMqttToken&disconnect(java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Disconnects from the server.
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of 30 seconds for work to quiesce before disconnecting.
This method must not be called from inside MqttCallback methods.
Specified by:
disconnect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when the disconnect completes. Use
null if not required.
Returns:token used to track and wait for the disconnect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for problems encountered while disconnectingSee Also:
disconnect
public&org.eclipse.paho.client.mqttv3.IMqttToken&disconnect(long&quiesceTimeout,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Disconnects from the server.
The client will wait for MqttCallback methods to
complete. It will then wait for up to the quiesce timeout to allow for
work which has already been initiated to complete. For instance when a QoS 2
message has started flowing to the server but the QoS 2 flow has not completed.It
prevents new messages being accepted and does not send any messages that have
been accepted but not yet started delivery across the network to the server. When
work has completed or after the quiesce timeout, the client will disconnect from
the server. If the cleanSession flag was set to false and is set to false the
next time a connection is made QoS 1 and 2 messages that
were not previously delivered will be delivered.
This method must not be called from inside MqttCallback methods.
The method returns control before the disconnect completes. Completion can
be tracked by:
Waiting on the returned token IMqttToken.waitForCompletion() or
Passing in a callback IMqttActionListener
Specified by:
disconnect&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters: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.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when the disconnect completes. Use
null if not required.
Returns:token used to track and wait for the connect to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttException - for problems encountered while disconnecting
public&org.eclipse.paho.client.mqttv3.IMqttDeliveryToken&publish(java.lang.String&topic,
byte[]&payload,
boolean&retained)
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttPersistenceException
Publishes a message to a topic on the server.
A convenience method, which will
create a new MqttMessage object with a byte array payload and the
specified QoS, and then publish it.
Specified by:
publish&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - to deliver the message to, for example "finance/stock/ibm".payload - the byte array to use as the payloadqos - the Quality of Service to deliver the message at. Valid values are 0, 1 or 2.retained - whether or not this message should be retained by the server.
Returns:token used to track and wait for the publish to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttPersistenceException - when a problem occurs storing the message
java.lang.IllegalArgumentException - if value of QoS is not 0, 1 or 2.
org.eclipse.paho.client.mqttv3.MqttException - for other errors encountered while publishing the message.
For instance if too many messages are being processed.See Also:
public&org.eclipse.paho.client.mqttv3.IMqttDeliveryToken&publish(java.lang.String&topic,
org.eclipse.paho.client.mqttv3.MqttMessage&message)
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttPersistenceException
Publishes a message to a topic on the server.
Takes an MqttMessage message and delivers it to the server at the
requested quality of service.
Specified by:
publish&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - to deliver the message to, for example "finance/stock/ibm".message - to deliver to the server
Returns:token used to track and wait for the publish to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttPersistenceException - when a problem occurs storing the message
java.lang.IllegalArgumentException - if value of QoS is not 0, 1 or 2.
org.eclipse.paho.client.mqttv3.MqttException - for other errors encountered while publishing the message.
For instance client not connected.See Also:
public&org.eclipse.paho.client.mqttv3.IMqttDeliveryToken&publish(java.lang.String&topic,
byte[]&payload,
boolean&retained,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttPersistenceException
Publishes a message to a topic on the server.
A convenience method, which will
create a new MqttMessage object with a byte array payload and the
specified QoS, and then publish it.
Specified by:
publish&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - to deliver the message to, for example "finance/stock/ibm".payload - the byte array to use as the payloadqos - the Quality of Service to deliver the message at.
Valid values are 0, 1 or 2.retained - whether or not this message should be retained by the server.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when message delivery
hsa completed to the requested quality of service
Returns:token used to track and wait for the publish to complete. The token
will be passed to any callback that has been set.
org.eclipse.paho.client.mqttv3.MqttPersistenceException - when a problem occurs storing the message
java.lang.IllegalArgumentException - if value of QoS is not 0, 1 or 2.
org.eclipse.paho.client.mqttv3.MqttException - for other errors encountered while publishing the message.
For instance client not connected.See Also:
public&org.eclipse.paho.client.mqttv3.IMqttDeliveryToken&publish(java.lang.String&topic,
org.eclipse.paho.client.mqttv3.MqttMessage&message,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttPersistenceException
Publishes a message to a topic on the server.
Once this method has returned cleanly, the message has been accepted for publication by the
client and will be delivered on a background thread.
In the event the connection fails or the client stops. Messages will be delivered to the
requested quality of service once the connection is re-established to the server on condition that:
The connection is re-established with the same clientID
The original connection was made with (@link MqttConnectOptions#setCleanSession(boolean)}
set to false
The connection is re-established with (@link MqttConnectOptions#setCleanSession(boolean)}
set to false
Depending when the failure occurs QoS 0 messages may not be delivered.
When building an application,
the design of the topic tree should take into account the following principles
of topic name syntax and semantics:
A topic must be at least one character long.
Topic names are case sensitive.
For example, ACCOUNTS and Accounts are
two different topics.
Topic names can include the space character.
For example, Accounts
payable is a valid topic.
A leading "/" creates a distinct topic.
For example, /finance is
different from finance. /finance matches "+/+" and "/+", but
Do not include the null character (Unicode \x0000) in
any topic.
The following principles apply to the construction and content of a topic
The length is limited to 64k but within that there are no limits to the
number of levels in a topic tree.
There can be any
that is, there can be any number
of topic trees.
The method returns control before the publish completes. Completion can
be tracked by:
Setting an IMqttAsyncClient.setCallback(MqttCallback) where the
MqttCallback.deliveryComplete(IMqttDeliveryToken)
method will be called.
Waiting on the returned token MqttToken.waitForCompletion() or
Passing in a callback IMqttActionListener to this method
Specified by:
publish&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - to deliver the message to, for example "finance/stock/ibm".message - to deliver to the serveruserContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when message delivery
has completed to the requested quality of service
Returns:token used to track and wait for the publish to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttPersistenceException - when a problem occurs storing the message
java.lang.IllegalArgumentException - if value of QoS is not 0, 1 or 2.
org.eclipse.paho.client.mqttv3.MqttException - for other errors encountered while publishing the message.
For instance client not connected.See Also:MqttMessage
public&org.eclipse.paho.client.mqttv3.IMqttToken&subscribe(java.lang.String&topic,
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttSecurityException
Subscribe to a topic, which may include wildcards.
Specified by:
subscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - the topic to subscribe to, which can include wildcards.qos - the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.
Returns:token used to track and wait for the subscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttSecurityException - for security related problems
org.eclipse.paho.client.mqttv3.MqttException - for non security related problemsSee Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&subscribe(java.lang.String[]&topic,
int[]&qos)
throws org.eclipse.paho.client.mqttv3.MqttException,
org.eclipse.paho.client.mqttv3.MqttSecurityException
Subscribe to multiple topics, each of which may include wildcards.
Provides an optimized way to subscribe to multiple topics compared to
subscribing to each one individually.
Specified by:
subscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - one or more topics to subscribe to, which can include wildcardsqos - the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.
Returns:token used to track and wait for the subscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttSecurityException - for security related problems
org.eclipse.paho.client.mqttv3.MqttException - for non security related problemsSee Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&subscribe(java.lang.String&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Subscribe to a topic, which may include wildcards.
Specified by:
subscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - the topic to subscribe to, which can include wildcards.qos - the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when subscribe
has completed
Returns:token used to track and wait for the subscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error registering the subscription.See Also:
public&org.eclipse.paho.client.mqttv3.IMqttToken&subscribe(java.lang.String[]&topic,
int[]&qos,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Subscribes to multiple topics, each of which may include wildcards.
Provides an optimized way to subscribe to multiple topics compared to
subscribing to each one individually.
should be called before this method, otherwise any received messages
will be discarded.
If (@link MqttConnectOptions#setCleanSession(boolean)} was set to true
when when connecting to the server then the subscription remains in place
until either:
The client disconnects
An unsubscribe method is called to un-subscribe the topic
If (@link MqttConnectOptions#setCleanSession(boolean)} was set to false
when connecting to the server then the subscription remains in place
until either:
An unsubscribe method is called to unsubscribe the topic
The next time the client connects with cleanSession set to true
With cleanSession set to false the MQTT server will store messages on
behalf of the client when the client is not connected. The next time the
client connects with the same client ID the server will
deliver the stored messages to the client.
The "topic filter" string used when subscribing
may contain special characters, which allow you to subscribe to multiple topics
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.
Topic level separator
The forward slash (/) is used to separate each level within
a topic tree and provide a hierarchical structure to the topic space. The
use of the topic level separator is significant when the two wildcard characters
are encountered in topics specified by subscribers.
Multi-level wildcard
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.
Single-level wildcard
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.
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.
The method returns control before the subscribe completes. Completion can
be tracked by:
Waiting on the supplied token MqttToken.waitForCompletion() or
Passing in a callback IMqttActionListener to this method
Specified by:
subscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - one or more topics to subscribe to, which can include wildcardsqos - the maximum quality of service to subscribe each topic at.Messages
published at a lower quality of service will be received at the published
Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when subscribe
has completed
Returns:token used to track and wait for the subscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error registering the subscription.
java.lang.IllegalArgumentException - if the two supplied arrays are not the same size.
unsubscribe
public&org.eclipse.paho.client.mqttv3.IMqttToken&unsubscribe(java.lang.String&topic)
throws org.eclipse.paho.client.mqttv3.MqttException
Requests the server unsubscribe the client from a topic.
Specified by:
unsubscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - the topic to unsubscribe from. It must match a topic
specified on an earlier subscribe.
Returns:token used to track and wait for the unsubscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error unregistering the subscription.See Also:
unsubscribe
public&org.eclipse.paho.client.mqttv3.IMqttToken&unsubscribe(java.lang.String[]&topic)
throws org.eclipse.paho.client.mqttv3.MqttException
Requests the server unsubscribe the client from one or more topics.
Specified by:
unsubscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - one or more topics to unsubscribe from. Each topic
must match one specified on an earlier subscribe.
Returns:token used to track and wait for the unsubscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error unregistering the subscription.See Also:
unsubscribe
public&org.eclipse.paho.client.mqttv3.IMqttToken&unsubscribe(java.lang.String&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Requests the server unsubscribe the client from a topics.
Specified by:
unsubscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - the topic to unsubscribe from. It must match a topic
specified on an earlier subscribe.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when unsubscribe
has completed
Returns:token used to track and wait for the unsubscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error unregistering the subscription.See Also:
unsubscribe
public&org.eclipse.paho.client.mqttv3.IMqttToken&unsubscribe(java.lang.String[]&topic,
java.lang.Object&userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener&callback)
throws org.eclipse.paho.client.mqttv3.MqttException
Requests the server unsubscribe the client from one or more topics.
Unsubcribing is the opposite of subscribing. When the server receives the
unsubscribe request it looks to see if it can find a matching subscription for the
client and then removes it. After this point the server will send no more
messages to the client for this subscription.
The topic(s) specified on the unsubscribe must match the topic(s)
specified in the original subscribe request for the unsubscribe to succeed
The method returns control before the unsubscribe completes. Completion can
be tracked by:
Waiting on the returned token MqttToken.waitForCompletion() or
Passing in a callback IMqttActionListener to this method
Specified by:
unsubscribe&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:topic - one or more topics to unsubscribe from. Each topic
must match one specified on an earlier subscribe.userContext - optional object used to pass context to the callback. Use
null if not required.callback - optional listener that will be notified when unsubscribe
has completed
Returns:token used to track and wait for the unsubscribe to complete. The token
will be passed to callback methods if set.
org.eclipse.paho.client.mqttv3.MqttException - if there was an error unregistering the subscription.
getPendingDeliveryTokens
public&org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[]&getPendingDeliveryTokens()
Returns the delivery tokens for any outstanding publish operations.
If a client has been restarted and there are messages that were in the
process of being delivered when the client stopped this method
returns a token for each in-flight message enabling the delivery to be tracked
Alternately the MqttCallback.deliveryComplete(IMqttDeliveryToken)
callback can be used to track the delivery of outstanding messages.
If a client connects with cleanSession true then there will be no
delivery tokens as the cleanSession option deletes all earlier state.
For state to be remembered the client must connect with cleanSession
set to false
Specified by:
getPendingDeliveryTokens&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Returns:zero or more delivery tokens
setCallback
public&void&setCallback(org.eclipse.paho.client.mqttv3.MqttCallback&callback)
Sets a callback listener to use for events that happen asynchronously.
There are a number of events that the listener will be notified about.
These include:
A new message has arrived and is ready to be processed
The connection to the server has been lost
Delivery of a message to the server has completed
Other events that track the progress of an individual operation such
as connect and subscribe can be tracked using the MqttToken returned from
each non-blocking method or using setting a IMqttActionListener on the
non-blocking method.
Specified by:
setCallback&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Parameters:callback - which will be invoked for certain asynchronous eventsSee Also:MqttCallback
setTraceCallback
public&void&setTraceCallback(&traceCallback)
identify the callback to be invoked when making tracing calls back into
the Activity
Parameters:traceCallback - handler
setTraceEnabled
public&void&setTraceEnabled(boolean&traceEnabled)
turn tracing on and off
Parameters:traceEnabled - set true to enable trace, otherwise, set false to disable trace
public&void&onReceive(Context&context,
Intent&intent)
Process incoming Intent objects representing the results of operations
and asynchronous activities such as message received
Note: This is only a public method because the Android
APIs require such.
This method should not be explicitly invoked.
acknowledgeMessage
public&boolean&acknowledgeMessage(java.lang.String&messageId)
Acknowledges a message received on the MqttCallback.messageArrived(String, MqttMessage)
Parameters:messageId - the messageId received from the MqttMessage (To access this field you need to cast MqttMessage to )
Returns:whether or not the message was successfully acknowledged
getSSLSocketFactory
public&javax.net.ssl.SSLSocketFactory&getSSLSocketFactory(java.io.InputStream&keyStore,
java.lang.String&password)
throws org.eclipse.paho.client.mqttv3.MqttSecurityException
Get the SSLSocketFactory using SSL key store and password
A convenience method, which will help user to create a SSLSocketFactory object
Parameters:keyStore - the SSL key store which is generated by some SSL key tool, such as keytool in Java JDKpassword - the password of the key store which is set when the key store is generated
Returns:SSLSocketFactory used to connect to the server with SSL authentication
org.eclipse.paho.client.mqttv3.MqttSecurityException - if there was any error when getting the SSLSocketFactory
disconnectForcibly
public&void&disconnectForcibly()
throws org.eclipse.paho.client.mqttv3.MqttException
Specified by:
disconnectForcibly&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
disconnectForcibly
public&void&disconnectForcibly(long&disconnectTimeout)
throws org.eclipse.paho.client.mqttv3.MqttException
Specified by:
disconnectForcibly&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
disconnectForcibly
public&void&disconnectForcibly(long&quiesceTimeout,
long&disconnectTimeout)
throws org.eclipse.paho.client.mqttv3.MqttException
Specified by:
disconnectForcibly&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
getProtocolVersion
public&org.eclipse.paho.client.mqttv3.MqttProtocolVersion&getProtocolVersion()
Specified by:
getProtocolVersion&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
setProtocolVersion
public&void&setProtocolVersion(org.eclipse.paho.client.mqttv3.MqttProtocolVersion&version)
Specified by:
setProtocolVersion&in interface&org.eclipse.paho.client.mqttv3.IMqttAsyncClient
unregisterResources
public&void&unregisterResources()
Unregister receiver which receives intent from MqttService avoids IntentReceiver
registerResources
public&void&registerResources(Context&context)
Register receiver to receiver intent from MqttService. Call this method when activity
is hidden and become to show again.
Parameters:context - - Current activity context.

我要回帖

更多关于 nodejs mqtt client 的文章

 

随机推荐