本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttConnectOptions.setConnectionTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java MqttConnectOptions.setConnectionTimeout方法的具体用法?Java MqttConnectOptions.setConnectionTimeout怎么用?Java MqttConnectOptions.setConnectionTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.MqttConnectOptions
的用法示例。
在下文中一共展示了MqttConnectOptions.setConnectionTimeout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMqttConnectOptions
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MqttConnectOptions getMqttConnectOptions() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(isCleanSession());
options.setConnectionTimeout(getTimeout());
options.setKeepAliveInterval(getKeepAlive());
if (!getUsername().isEmpty()) {
options.setUserName(getUsername());
}
if (!getPassword().isEmpty()) {
options.setPassword(getPassword().toCharArray());
}
if (!getLwtTopic().isEmpty() && !getLwtPayload().isEmpty()) {
options.setWill(getLwtTopic(), getLwtPayload().getBytes(), getLwtQos(), isLwtRetained());
}
return options;
}
示例2: buildMqttConnectOptions
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
MqttConnectOptions options = new MqttConnectOptions();
options.setSocketFactory(socketFactory);
options.setCleanSession(true);
options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);
Set<String> serverUris = getServerUris();
if (serverUris != null && !serverUris.isEmpty()) {
String[] uriArray = new String[serverUris.size()];
serverUris.toArray(uriArray);
options.setServerURIs(uriArray);
}
if (client.getWillMessage() != null) {
AWSIotMessage message = client.getWillMessage();
options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
}
return options;
}
示例3: MyMqttCloudClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MyMqttCloudClient(String cloudBrokerAddress, String clientId) {
// this._cloudTopic = cloudTopic;
this._cloudBrokerAddress = cloudBrokerAddress;
this._clientId = clientId;
MemoryPersistence persistence = new MemoryPersistence();
try {
this._mqCloudClient = new MqttClient(this._cloudBrokerAddress,
this._clientId, persistence);
this._mqCloudClient.setCallback(this);
MqttConnectOptions connOpts = new MqttConnectOptions();
// connOpts.setCleanSession(true);
connOpts.setConnectionTimeout(0);
connOpts.setKeepAliveInterval(30);
connOpts.setAutomaticReconnect(true);
System.out.println("Connecting to cloud broker: " + this._cloudBrokerAddress);
this._mqCloudClient.connect(connOpts);
System.out.println("Connected");
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
示例4: connect
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
/**
*
*
*/
private void connect() throws MqttException {
connOpt = new MqttConnectOptions();
connOpt.setCleanSession(true);
connOpt.setKeepAliveInterval(3600);
connOpt.setConnectionTimeout(3600);
connOpt.setUserName(sharedPref.getString("pref_username", ""));
connOpt.setPassword(sharedPref.getString("pref_password", "").toCharArray());
String tmpDir = createTempDir().getPath(); //System.getProperty("java.io.tmpdir");
Log.i(TAG, "Persistence will be done in " + tmpDir);
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
// Connect to Broker
mClient = new MqttClient(sharedPref.getString("pref_url", "") + ":" + sharedPref.getString("pref_port", "1883"), android_id + "_client", dataStore);
mClient.setCallback(this);
mClient.connect(connOpt);
Log.i(TAG, "Connected to " + sharedPref.getString("pref_url", ""));
}
示例5: optionsFromModel
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private MqttConnectOptions optionsFromModel(ConnectionModel model){
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(model.isCleanSession());
connOpts.setConnectionTimeout(model.getTimeout());
connOpts.setKeepAliveInterval(model.getKeepAlive());
if(!model.getUsername().equals(ActivityConstants.empty)){
connOpts.setUserName(model.getUsername());
}
if(!model.getPassword().equals(ActivityConstants.empty)){
connOpts.setPassword(model.getPassword().toCharArray());
}
/*
if(!model.getLwtTopic().equals(ActivityConstants.empty) && !model.getLwtMessage().equals(ActivityConstants.empty)){
connOpts.setWill(model.getLwtTopic(), model.getLwtMessage().getBytes(), model.getLwtQos(), model.isLwtRetain());
}*/
// if(tlsConnection){
// // TODO Add Keys to conOpts here
// //connOpts.setSocketFactory();
// }
return connOpts;
}
示例6: run
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public void run() {
try {
// Connect to the MQTT Server
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(30);
options.setKeepAliveInterval(30);
options.setUserName(username);
options.setPassword(password.toCharArray());
client = new MqttClient(serverUrl, clientId);
client.setTimeToWait(5000); // short timeout on failure to connect
client.connect(options);
client.setCallback(this);
// Just listen to all DDATA messages on spAv1.0 topics and wait for inbound messages
client.subscribe("spBv1.0/#", 0);
} catch(Exception e) {
e.printStackTrace();
}
}
示例7: startConnect
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private void startConnect(String clientID, String serverIP, String port) {
//服务器地址
String uri ="tcp://";
uri=uri+serverIP+":"+port;
Log.d("MainActivity",uri+" "+clientID);
/**
* 连接的选项
*/
MqttConnectOptions conOpt = new MqttConnectOptions();
/**设计连接超时时间*/
conOpt.setConnectionTimeout(3000);
/**设计心跳间隔时间300秒*/
conOpt.setKeepAliveInterval(300);
/**
* 创建连接对象
*/
client = new MqttAndroidClient(this,uri, clientID);
/**
* 连接后设计一个回调
*/
client.setCallback(new MqttCallbackHandler(this, clientID));
/**
* 开始连接服务器,参数:ConnectionOptions, IMqttActionListener
*/
try {
client.connect(conOpt, null, new ConnectCallBackHandler(this));
} catch (MqttException e) {
e.printStackTrace();
}
}
示例8: pubMsg
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public static void pubMsg(String tcpUrl, String clientId, String topicName,
String message) throws MqttException, UnsupportedEncodingException {
MqttClient client = new MqttClient(tcpUrl, clientId);
MqttConnectOptions mqcConf = new MqttConnectOptions();
mqcConf.setConnectionTimeout(300);
mqcConf.setKeepAliveInterval(1200);
client.connect(mqcConf);
MqttTopic topic = client.getTopic(topicName);
topic.publish(message.getBytes("utf8"), 1, false);
// client.close();
}
示例9: pubMsg
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public static void pubMsg(String tcpUrl, String clientId, String topicName)
throws MqttException, UnsupportedEncodingException {
MqttClient client = new MqttClient(tcpUrl, clientId);
MqttConnectOptions mqcConf = new MqttConnectOptions();
mqcConf.setConnectionTimeout(300);
mqcConf.setKeepAliveInterval(1200);
client.connect(mqcConf);
MqttTopic topic = client.getTopic(topicName);
for (int i = 0; i < 10; i++) {
String message = "{\"id\":" + (i+1) + ",\"temp\":12}";
topic.publish(message.getBytes("utf8"), 1, false);
}
client.disconnect();
}
示例10: doConnect
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public void doConnect(IMqttActionListener listener) {
try {
//realiza a conexão com o broker
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(5);
IMqttToken token = mqttAndroidClient.connect();
token.setActionCallback(listener);
} catch (MqttException e) {
e.printStackTrace();
}
}
示例11: run
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public void run() {
try {
// Connect to the MQTT Server
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(30);
options.setKeepAliveInterval(30);
options.setUserName(username);
options.setPassword(password.toCharArray());
client = new MqttClient(serverUrl, clientId);
client.setTimeToWait(2000);
client.setCallback(this);
client.connect(options);
// Subscribe to control/command messages for both the edge of network node and the attached devices
client.subscribe(NAMESPACE + "/" + groupId + "/+/" + edgeNode, 0);
client.subscribe(NAMESPACE + "/" + groupId + "/+/" + edgeNode + "/*", 0);
// Loop to receive input commands
while (true) {
System.out.print("\n> ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
handleCommand(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}
示例12: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private void init() {
try {
//host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(host, "test",
new MemoryPersistence());
//MQTT的连接设置
options = new MqttConnectOptions();
//设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(true);
//设置连接的用户名
//options.setUserName(userName);
//设置连接的密码
//options.setPassword(passWord.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
//设置回调
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
//连接丢失后,一般在这里面进行重连
System.out.println("connectionLost----------");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//publish后会执行到这里
System.out.println("deliveryComplete---------"
+ token.isComplete());
}
@Override
public void messageArrived(String topicName, MqttMessage message)
throws Exception {
//subscribe后得到的消息会执行到这里面
System.out.println("messageArrived----------");
Message msg = new Message();
msg.what = 1;
msg.obj = topicName + "---" + message.toString();
handler.sendMessage(msg);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: MyMqttFogClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MyMqttFogClient(ArrayList<Item> items, String fogBrokerAddress, String cloudBrokerAddress, String fogClientId, String cloudClientId) {
this._fogBrokerAddress = fogBrokerAddress;
this._cloudBrokerAddress = cloudBrokerAddress;
this._fogClientId = fogClientId;
this._cloudClientId = cloudClientId;
this._items = items;
MemoryPersistence persistence = new MemoryPersistence();
// try {
// this._mqCloudClient = new MqttClient(this._cloudBrokerAddress,
// _cloudClientId, persistence);
// this._mqCloudClient.setCallback(this);
// MqttConnectOptions connOpts = new MqttConnectOptions();
// connOpts.setCleanSession(true);
// connOpts.setKeepAliveInterval(30);
// System.out.println("Connecting to cloud broker: " + this._cloudBrokerAddress);
// this._mqCloudClient.connect(connOpts);
// System.out.println("Connected");
// } catch (MqttException me) {
// System.out.println("reason " + me.getReasonCode());
// System.out.println("msg " + me.getMessage());
// System.out.println("loc " + me.getLocalizedMessage());
// System.out.println("cause " + me.getCause());
// System.out.println("excep " + me);
// me.printStackTrace();
// }
try {
this._mqFogClient = new MqttClient(this._fogBrokerAddress, this._fogClientId,
persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setKeepAliveInterval(60);
connOpts.setAutomaticReconnect(true);
connOpts.setConnectionTimeout(0);
System.out.println("Connecting to fog broker: " + this._fogBrokerAddress);
this._mqFogClient.connect(connOpts);
System.out.println("Connected");
this._mqCloudClient = new MyMqttCloudClient(this._cloudBrokerAddress, this._cloudClientId);
this._mqFogClient.setCallback(this);
} catch (MqttException e) {
e.printStackTrace();
}
// this._mqCloudClient = new MyMqttCloudClient(this._cloudTopic, this._cloudBrokerAddress, "oneM2M publisher");
}
示例14: establishMqttSession
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
/**
* Establish an MQTT Session with Sparkplug defined Death Certificate. It may not be
* Immediately intuitive that the Death Certificate is created prior to publishing the
* Birth Certificate, but the Death Certificate is actually part of the MQTT Session
* establishment. For complete details of the actual MQTT wire protocol refer to the
* latest OASyS MQTT V3.1.1 standards at:
* http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
*
* @return true = MQTT Session Established
*/
public boolean establishMqttSession() {
try {
//
// Setup the MQTT connection parameters using the Paho MQTT Client.
//
MqttConnectOptions options = new MqttConnectOptions();
if (USING_REAL_TLS) {
SocketFactory sf = SSLSocketFactory.getDefault();
options.setSocketFactory(sf);
}
// Autoreconnect enable
options.setAutomaticReconnect(true);
// MQTT session parameters Clean Start = true
options.setCleanSession(true);
// Session connection attempt timeout period in seconds
options.setConnectionTimeout(10);
// MQTT session parameter Keep Alive Period in Seconds
options.setKeepAliveInterval(30);
// MQTT Client Username
options.setUserName(username);
// MQTT Client Password
options.setPassword(password.toCharArray());
//
// Build up the Death Certificate MQTT Payload. Note that the Death
// Certificate payload sequence number
// is not tied to the normal message sequence numbers.
//
SparkplugBPayload payload = new SparkplugBPayloadBuilder(getNextSeqNum())
.setTimestamp(new Date())
.addMetric(new MetricBuilder("bdSeq",
MetricDataType.Int64,
bdSeq)
.createMetric())
.createPayload();
byte[] bytes = new SparkplugBPayloadEncoder().getBytes(payload);
//
// Setup the Death Certificate Topic/Payload into the MQTT session
// parameters
//
options.setWill(NAMESPACE + "/" + groupId + "/NDEATH/" + edgeNode, bytes, 0, false);
//
// Create a new Paho MQTT Client
//
client = new MqttClient(serverUrl, clientId);
//
// Using the parameters set above, try to connect to the define MQTT
// server now.
//
System.out.println("Trying to establish an MQTT Session to the MQTT Server @ :" + serverUrl);
client.connect(options);
System.out.println("MQTT Session Established");
client.setCallback(this);
//
// With a successful MQTT Session in place, now issue subscriptions
// for the EoN Node and Device "Command" Topics of 'NCMD' and 'DCMD'
// defined in Sparkplug
//
client.subscribe(NAMESPACE + "/" + groupId + "/NCMD/" + edgeNode + "/#", 0);
client.subscribe(NAMESPACE + "/" + groupId + "/DCMD/" + edgeNode + "/#", 0);
} catch (Exception e) {
System.out.println("Error Establishing an MQTT Session:");
e.printStackTrace();
return false;
}
return true;
}
示例15: run
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public void run() {
try {
// Random generator and thread pool for outgoing published messages
executor = Executors.newFixedThreadPool(1);
// Build up DEATH payload - note DEATH payloads don't have a regular sequence number
SparkplugBPayloadBuilder deathPayload = new SparkplugBPayloadBuilder().setTimestamp(new Date());
deathPayload = addBdSeqNum(deathPayload);
byte [] deathBytes = new SparkplugBPayloadEncoder().getBytes(deathPayload.createPayload());
MqttConnectOptions options = new MqttConnectOptions();
if (USING_REAL_TLS) {
SocketFactory sf = SSLSocketFactory.getDefault();
options.setSocketFactory(sf);
}
// Connect to the MQTT Server
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(30);
options.setKeepAliveInterval(30);
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setWill(NAMESPACE + "/" + groupId + "/NDEATH/" + edgeNode, deathBytes, 0, false);
client = new MqttClient(serverUrl, clientId);
client.setTimeToWait(2000);
client.setCallback(this); // short timeout on failure to connect
client.connect(options);
// Subscribe to control/command messages for both the edge of network node and the attached devices
client.subscribe(NAMESPACE + "/" + groupId + "/NCMD/" + edgeNode + "/#", 0);
client.subscribe(NAMESPACE + "/" + groupId + "/DCMD/" + edgeNode + "/#", 0);
client.subscribe(NAMESPACE + "/#", 0);
// Loop forever publishing data every PUBLISH_PERIOD
while (true) {
Thread.sleep(PUBLISH_PERIOD);
if (client.isConnected()) {
synchronized(seqLock) {
System.out.println("Connected - publishing new data");
// Create the payload and add some metrics
SparkplugBPayload payload = new SparkplugBPayload(
new Date(),
newMetrics(false),
getSeqNum(),
newUUID(),
null);
client.publish(NAMESPACE + "/" + groupId + "/DDATA/" + edgeNode + "/" + deviceId,
new SparkplugBPayloadEncoder().getBytes(payload), 0, false);
}
} else {
System.out.println("Not connected - not publishing data");
}
}
} catch(Exception e) {
e.printStackTrace();
}
}