本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttConnectOptions.setAutomaticReconnect方法的典型用法代码示例。如果您正苦于以下问题:Java MqttConnectOptions.setAutomaticReconnect方法的具体用法?Java MqttConnectOptions.setAutomaticReconnect怎么用?Java MqttConnectOptions.setAutomaticReconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.MqttConnectOptions
的用法示例。
在下文中一共展示了MqttConnectOptions.setAutomaticReconnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
@Override
public void init(MqttPluginConfiguration configuration) {
retryInterval = configuration.getRetryInterval();
mqttClientOptions = new MqttConnectOptions();
mqttClientOptions.setCleanSession(false);
mqttClientOptions.setMaxInflight(configuration.getMaxInFlight());
mqttClientOptions.setAutomaticReconnect(true);
String clientId = configuration.getClientId();
if (StringUtils.isEmpty(clientId)) {
clientId = UUID.randomUUID().toString();
}
if (!StringUtils.isEmpty(configuration.getAccessToken())) {
mqttClientOptions.setUserName(configuration.getAccessToken());
}
try {
mqttClient = new MqttAsyncClient("tcp://" + configuration.getHost() + ":" + configuration.getPort(), clientId);
} catch (Exception e) {
log.error("Failed to create mqtt client", e);
throw new RuntimeException(e);
}
// connect();
}
示例2: connectAndSubscribe
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
/*********************************************************************************************************************************************************************
*
*/
private void connectAndSubscribe() throws Exception {
ConfigHandler configHandler = serialMqttBridge.getConfigHandler();
mqttClient = new MqttClient(configHandler.getMqttBrokerUrl(), configHandler.getMqttClientId(), null);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setAutomaticReconnect(true);
// Authentication
if (configHandler.getMqttBrokerUsername() != null && configHandler.getMqttBrokerPassword() != null) {
connOpts.setUserName(configHandler.getMqttBrokerUsername());
connOpts.setPassword(configHandler.getMqttBrokerPassword().toCharArray());
}
// MqttCallback
mqttCallback = new MqttSubscriptionCallback(this);
mqttClient.setCallback(mqttCallback);
mqttClient.connect(connOpts);
// Subscribe to defined inbound topic
mqttClient.subscribe(configHandler.getMqttTopicSubscribe(), configHandler.getMqttQosSubscribe());
}
示例3: ProtobufMqttProtocolHandler
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public ProtobufMqttProtocolHandler(NativeDeviceFactoryInterface deviceFactory) {
super(deviceFactory);
String mqtt_url = PropertyUtil.getProperty(MQTT_URL_PROP, null);
if (mqtt_url == null) {
throw new RuntimeIOException("Property '" + MQTT_URL_PROP + "' must be set");
}
try {
mqttClient = new MqttClient(mqtt_url, MqttClient.generateClientId(), new MemoryPersistence());
mqttClient.setCallback(this);
MqttConnectOptions con_opts = new MqttConnectOptions();
con_opts.setAutomaticReconnect(true);
con_opts.setCleanSession(true);
mqttClient.connect(con_opts);
Logger.debug("Connected to {}", mqtt_url);
// Subscribe
Logger.debug("Subscribing to response and notification topics...");
mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC);
mqttClient.subscribe(MqttProviderConstants.GPIO_NOTIFICATION_TOPIC);
Logger.debug("Subscribed");
} catch (MqttException e) {
throw new RuntimeIOException(e);
}
}
示例4: MqttTestClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MqttTestClient(String mqttUrl) throws MqttException {
mqttClient = new MqttClient(mqttUrl, MqttClient.generateClientId(), new MemoryPersistence());
mqttClient.setCallback(this);
MqttConnectOptions con_opts = new MqttConnectOptions();
con_opts.setAutomaticReconnect(true);
con_opts.setCleanSession(true);
mqttClient.connect(con_opts);
Logger.debug("Connected to {}", mqttUrl);
lock = new ReentrantLock();
conditions = new HashMap<>();
responses = new HashMap<>();
// Subscribe
Logger.debug("Subscribing to {}...", MqttProviderConstants.RESPONSE_TOPIC);
mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC);
Logger.debug("Subscribed");
}
示例5: 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();
}
}
示例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: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
@PostConstruct
public void init() throws Exception {
scheduler = Executors.newSingleThreadScheduledExecutor();
tbClientOptions = new MqttConnectOptions();
tbClientOptions.setCleanSession(false);
tbClientOptions.setMaxInflight(connection.getMaxInFlight());
tbClientOptions.setAutomaticReconnect(true);
MqttGatewaySecurityConfiguration security = connection.getSecurity();
security.setupSecurityOptions(tbClientOptions);
tbClient = new MqttAsyncClient((security.isSsl() ? "ssl" : "tcp") + "://" + connection.getHost() + ":" + connection.getPort(),
security.getClientId(), persistence.getPersistence());
tbClient.setCallback(this);
if (persistence.getBufferSize() > 0) {
DisconnectedBufferOptions options = new DisconnectedBufferOptions();
options.setBufferSize(persistence.getBufferSize());
options.setBufferEnabled(true);
options.setPersistBuffer(true);
tbClient.setBufferOpts(options);
}
connect();
scheduler.scheduleAtFixedRate(this::reportStats, 0, reporting.getInterval(), TimeUnit.MILLISECONDS);
}
示例8: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
@PostConstruct
public void init() throws Exception {
scheduler = Executors.newSingleThreadScheduledExecutor();
tbClientOptions = new MqttConnectOptions();
tbClientOptions.setCleanSession(false);
tbClientOptions.setMaxInflight(connection.getMaxInFlight());
tbClientOptions.setAutomaticReconnect(true);
MqttGatewaySecurityConfiguration security = connection.getSecurity();
security.setupSecurityOptions(tbClientOptions);
tbClient = new MqttAsyncClient(
(security.isSsl() ? "ssl" : "tcp") + "://" + connection.getHost() + ":" + connection.getPort(),
security.getClientId(), persistence.getPersistence());
tbClient.setCallback(this);
if (persistence.getBufferSize() > 0) {
DisconnectedBufferOptions options = new DisconnectedBufferOptions();
options.setBufferSize(persistence.getBufferSize());
options.setBufferEnabled(true);
options.setPersistBuffer(true);
tbClient.setBufferOpts(options);
}
connect();
scheduler.scheduleAtFixedRate(this::reportStats, 0, reporting.getInterval(), TimeUnit.MILLISECONDS);
}
示例9: initializeMqttClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private void initializeMqttClient()
throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
mMqttOptions.getClientId(), new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
// Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
// explicitly set this. If you don't set MQTT version, the server will immediately close its
// connection to your device.
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setUserName(CloudIotCoreOptions.UNUSED_ACCOUNT_NAME);
options.setAutomaticReconnect(true);
// generate the jwt password
options.setPassword(mqttAuth.createJwt(mMqttOptions.getProjectId()));
mqttClient.setCallback(this);
mqttClient.connect(options);
if(mqttClient.isConnected()) {
try{
mSubTopic = "/devices/sense_hub_2.0_android_things/config";// + NetworkUtils.getMACAddress(mContext);
Log.i(TAG, "initializeMqttClient subscribe topic=" + mSubTopic);
mqttClient.subscribe(mSubTopic, 1);
}catch (Exception e){
e.printStackTrace();
}
}
mReady.set(true);
}
示例10: initializeMqttClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
private void initializeMqttClient()
throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Log.d(TAG, "initializeMqttClient broker=" + mMqttOptions.getBrokerUrl() +
" clientID=" + mMqttOptions.getClientId() +
" username=" + mMqttOptions.getUsername() +
" password=" + mMqttOptions.getPassword());
mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
mMqttOptions.getClientId(), new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
// Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
// explicitly set this. If you don't set MQTT version, the server will immediately close its
// connection to your device.
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setUserName(mMqttOptions.getUsername());
options.setPassword(mMqttOptions.getPassword().toCharArray());
options.setAutomaticReconnect(true);
mqttClient.setCallback(this);
mqttClient.connect(options);
if(mqttClient.isConnected()) {
try{
Log.i(TAG, "initializeMqttClient subscribe topic=" + mMqttOptions.getSubscribeTopicName());
mqttClient.subscribe(mMqttOptions.getSubscribeTopicName(), 1);
}catch (Exception e){
e.printStackTrace();
}
}
else{
Log.e(TAG, "Can't connect to " + mMqttOptions.getBrokerUrl());
}
mReady.set(true);
}
示例11: MqttTestApp
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MqttTestApp() throws UnknownHostException, MqttException {
mqttClient = new MqttClient(mqttUrl, CLIENT_ID_PREFIX + InetAddress.getLocalHost().getHostName(),
new MemoryPersistence());
mqttClient.setCallback(this);
MqttConnectOptions con_opts = new MqttConnectOptions();
con_opts.setAutomaticReconnect(true);
con_opts.setCleanSession(true);
Logger.debug("Connecting to {}...", mqttUrl);
mqttClient.connect(con_opts);
Logger.debug("Connected to {}", mqttUrl);
mqttClient.subscribe("outTopic");
mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC);
}
示例12: MqttJsonServer
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MqttJsonServer(String mqttUrl) throws UnknownHostException, MqttException {
mqttClient = new MqttClient(mqttUrl, CLIENT_ID_PREFIX + InetAddress.getLocalHost().getHostName(),
new MemoryPersistence());
mqttClient.setCallback(this);
MqttConnectOptions con_opts = new MqttConnectOptions();
con_opts.setAutomaticReconnect(true);
con_opts.setCleanSession(true);
Logger.debug("Connecting to {}...", mqttUrl);
mqttClient.connect(con_opts);
Logger.debug("Connected to {}", mqttUrl);
}
示例13: MqttProtobufServer
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
public MqttProtobufServer(String mqttUrl) throws UnknownHostException, MqttException {
mqttClient = new MqttClient(mqttUrl, CLIENT_ID_PREFIX + InetAddress.getLocalHost().getHostName(),
new MemoryPersistence());
mqttClient.setCallback(this);
MqttConnectOptions con_opts = new MqttConnectOptions();
con_opts.setAutomaticReconnect(true);
con_opts.setCleanSession(true);
Logger.debug("Connecting to {}...", mqttUrl);
mqttClient.connect(con_opts);
Logger.debug("Connected to {}", mqttUrl);
}
示例14: 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();
}
}
示例15: createConnectOptions
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //导入方法依赖的package包/类
protected MqttConnectOptions createConnectOptions() {
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(accessKey);
connOpts.setPassword(sign.toCharArray());
connOpts.setCleanSession(cleanSession);
connOpts.setKeepAliveInterval(90);
connOpts.setAutomaticReconnect(true);
return connOpts;
}