本文整理汇总了Java中org.eclipse.paho.client.mqttv3.IMqttClient.close方法的典型用法代码示例。如果您正苦于以下问题:Java IMqttClient.close方法的具体用法?Java IMqttClient.close怎么用?Java IMqttClient.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.IMqttClient
的用法示例。
在下文中一共展示了IMqttClient.close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: disconnectAndCloseClient
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @param client
* @throws MqttException
*/
public static void disconnectAndCloseClient(IMqttClient client) throws MqttException {
if (client != null) {
if (client.isConnected()) {
client.disconnect(0);
}
client.close();
}
}
示例2: testConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* Tests that a client can be constructed and that it can connect to and disconnect from the
* service
* @throws Exception
*/
@Test
public void testConnect() throws Exception {
final String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
log.entering(className, methodName);
IMqttClient mqttClient = null;
try {
mqttClient = clientFactory.createMqttClient(serverURI, methodName);
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
mqttClient.connect();
log.info("Disconnecting...");
mqttClient.disconnect();
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
mqttClient.connect();
log.info("Disconnecting...");
mqttClient.disconnect();
}
catch (Exception exception) {
log.log(Level.SEVERE, "caught exception:", exception);
Assert.fail("Failed:" + methodName + " exception=" + exception);
}
finally {
if (mqttClient != null) {
log.info("Close...");
mqttClient.close();
}
}
log.exiting(className, methodName);
}
示例3: testConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
public void testConnect() throws Exception {
IMqttClient client = null;
try {
String clientId = "testConnect";
client = new MqttClient(serverURI, clientId);
System.out.println("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
client.connect();
String clientId2 = client.getClientId();
System.out.println("clientId = " + clientId2);
boolean isConnected = client.isConnected();
System.out.println("isConnected = " + isConnected);
String id = client.getServerURI();
System.out.println("ServerURI = " + id);
client.disconnect();
System.out.println("Disconnecting...");
client.connect();
System.out.println("Re-Connecting...");
client.disconnect();
System.out.println("Disconnecting...");
}
catch (MqttException exception) {
System.out.println("Unexpected exception: " + exception);
}
finally {
if (client != null) {
System.out.println("Close...");
client.close();
}
}
}
示例4: testHAConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
public void testHAConnect() throws Exception {
IMqttClient client = null;
try {
try {
String clientId = "testHAConnect";
// If a proxy client does not support the URI list in the connect options, then this test should fail.
// We ensure this happens by using a junk URI when creating the client.
String junk = "tcp://junk:123";
client = new MqttClient(junk, clientId);
// The first URI has a good protocol, but has a garbage hostname.
// This ensures that a connect is attempted to the the second URI in the list
String[] urls = new String[]{"tcp://junk", serverURI.toString()};
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(urls);
System.out.println("Connecting...");
client.connect(options);
System.out.println("Disconnecting...");
client.disconnect();
}
catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
e.printStackTrace();
throw e;
}
}
finally {
if (client != null) {
System.out.println("Close...");
client.close();
}
}
}
示例5: testConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
@Test
public void testConnect() throws Exception {
String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
IMqttClient client = null;
try {
String clientId = methodName;
client = clientFactory.createMqttClient(serverURI, clientId);
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
client.connect();
String clientId2 = client.getClientId();
log.info("clientId = " + clientId2);
boolean isConnected = client.isConnected();
log.info("isConnected = " + isConnected);
String id = client.getServerURI();
log.info("ServerURI = " + id);
log.info("Disconnecting...");
client.disconnect();
log.info("Re-Connecting...");
client.connect();
log.info("Disconnecting...");
client.disconnect();
}
catch (MqttException exception) {
log.log(Level.SEVERE, "caught exception:", exception);
Assert.fail("Unexpected exception: " + exception);
}
finally {
if (client != null) {
log.info("Close...");
client.close();
}
}
}
示例6: testHAConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
@Test
public void testHAConnect() throws Exception {
String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
// Some old clients do not support the new HA interface on the connect call
if (clientFactory.isHighAvalabilitySupported() == false) {
return;
}
IMqttClient client = null;
try {
try {
String clientId = methodName;
// If a client does not support the URI list in the connect options, then this test should fail.
// We ensure this happens by using a junk URI when creating the client.
URI junk = new URI("tcp://junk:123");
client = clientFactory.createMqttClient(junk, clientId);
// The first URI has a good protocol, but has a garbage hostname.
// This ensures that a connect is attempted to the the second URI in the list
String[] urls = new String[]{"tcp://junk", serverURI.toString()};
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(urls);
log.info("Connecting...");
client.connect(options);
log.info("Disconnecting...");
client.disconnect();
}
catch (Exception e) {
// logger.info(e.getClass().getName() + ": " + e.getMessage());
e.printStackTrace();
throw e;
}
}
finally {
if (client != null) {
log.info("Close...");
client.close();
}
}
}
示例7: testPubSub
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
@Test
public void testPubSub() throws Exception {
String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
IMqttClient client = null;
try {
String topicStr = "topic" + "_02";
String clientId = methodName;
client = clientFactory.createMqttClient(serverURI, clientId);
log.info("Assigning callback...");
MessageListener listener = new MessageListener();
client.setCallback(listener);
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
client.connect();
log.info("Subscribing to..." + topicStr);
client.subscribe(topicStr);
log.info("Publishing to..." + topicStr);
MqttTopic topic = client.getTopic(topicStr);
MqttMessage message = new MqttMessage("foo".getBytes());
topic.publish(message);
log.info("Checking msg");
MqttMessage msg = listener.getNextMessage();
Assert.assertNotNull(msg);
Assert.assertEquals("foo", msg.toString());
log.info("getTopic name");
String topicName = topic.getName();
log.info("topicName = " + topicName);
Assert.assertEquals(topicName, topicStr);
log.info("Disconnecting...");
client.disconnect();
}
finally {
if (client != null) {
log.info("Close...");
client.close();
}
}
}
示例8: testRemoteConnect
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* Test connection using a remote host name for the local host.
* @throws Exception
*/
@Test
public void testRemoteConnect() throws Exception {
final String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
log.entering(className, methodName);
IMqttClient mqttClient = null;
try {
mqttClient = clientFactory.createMqttClient(serverURI, methodName);
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
mqttClient.connect();
log.info("Disconnecting...");
mqttClient.disconnect();
MqttV3Receiver mqttV3Receiver = new MqttV3Receiver(mqttClient, LoggingUtilities.getPrintStream());
log.info("Assigning callback...");
mqttClient.setCallback(mqttV3Receiver);
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setCleanSession(false);
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName + ", cleanSession: false");
mqttClient.connect(mqttConnectOptions);
String[] topicNames = new String[]{methodName + "/Topic"};
int[] topicQos = {0};
log.info("Subscribing to..." + topicNames[0]);
mqttClient.subscribe(topicNames, topicQos);
byte[] payload = ("Message payload " + className + "." + methodName).getBytes();
MqttTopic mqttTopic = mqttClient.getTopic(topicNames[0]);
log.info("Publishing to..." + topicNames[0]);
mqttTopic.publish(payload, 1, false);
boolean ok = mqttV3Receiver.validateReceipt(topicNames[0], 0, payload);
if (!ok) {
Assert.fail("Receive failed");
}
log.info("Disconnecting...");
mqttClient.disconnect();
}
catch (Exception exception) {
log.log(Level.SEVERE, "caught exception:", exception);
Assert.fail("Failed:" + methodName + " exception=" + exception);
}
finally {
if (mqttClient != null) {
log.info("Close...");
mqttClient.close();
}
}
log.exiting(className, methodName);
}
示例9: testPubSub
import org.eclipse.paho.client.mqttv3.IMqttClient; //导入方法依赖的package包/类
/**
* @throws Exception
*/
public void testPubSub() throws Exception {
IMqttClient client = null;
try {
String topicStr = "topic" + "_02";
String clientId = "testPubSub";
client = new MqttClient(serverURI, clientId);
System.out.println("Assigning callback...");
MessageListener listener = new MessageListener();
client.setCallback(listener);
System.out.println("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
client.connect();
System.out.println("Subscribing to..." + topicStr);
client.subscribe(topicStr);
System.out.println("Publishing to..." + topicStr);
MqttTopic topic = client.getTopic(topicStr);
MqttMessage message = new MqttMessage("foo".getBytes());
topic.publish(message);
System.out.println("Checking msg");
MqttMessage msg = listener.getNextMessage();
if (msg == null) throw new Exception("message should not be null");
if (!msg.toString().equals("foo")) throw new Exception("message should equal foo");
System.out.println("getTopic name");
String topicName = topic.getName();
System.out.println("topicName = " + topicName);
if (!topicName.equals(topicStr)) throw new Exception ("topicName should equal topicStr");
System.out.println("Disconnecting...");
client.disconnect();
System.out.println("testPubSub completed successfully");
}
finally {
if (client != null) {
System.out.println("Close...");
client.close();
}
}
}