当前位置: 首页>>代码示例>>Java>>正文


Java MqttClient.disconnect方法代码示例

本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttClient.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Java MqttClient.disconnect方法的具体用法?Java MqttClient.disconnect怎么用?Java MqttClient.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.paho.client.mqttv3.MqttClient的用法示例。


在下文中一共展示了MqttClient.disconnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testMQtt

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void testMQtt() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    MqttClient client = new MqttClient("tcp://localhost:" + MQTT_PORT, MqttClient.generateClientId(), new MemoryPersistence());
    client.connect();
    MqttComponent mqtt = new MqttComponent();
    mqtt.client = client;
    Publisher<byte[]> fromTopic = mqtt.from("input", byte[].class);
    Subscriber<byte[]> toTopic = mqtt.to("output", byte[].class);
    Flux.from(fromTopic)
        .log()
        .subscribe(toTopic);
    
    client.subscribe("output", (topic, message) -> {
        result = new Integer(new String(message.getPayload()));
        latch.countDown();
    });
    client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
    client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
    latch.await(100, TimeUnit.SECONDS);
    Assert.assertEquals(2, result, 0.1);
    client.disconnect();
    client.close();
}
 
开发者ID:cschneider,项目名称:reactive-components,代码行数:25,代码来源:MqttTest.java

示例2: disconnectedByClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void disconnectedByClient(TestContext context) {

  Async async = context.async();

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
    client.connect();
    client.disconnect();

    // give more time to the MqttClient to update its connection state
    this.vertx.setTimer(1000, t1 -> {
      async.complete();
    });

    async.await();

    context.assertTrue(!client.isConnected() && !this.endpoint.isConnected());

  } catch (MqttException e) {
    context.assertTrue(false);
    e.printStackTrace();
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:26,代码来源:MqttServerEndpointStatusTest.java

示例3: main

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public static void main(String args[]) {
    String topic = "iot/iot";
    String content = "Hello ith";
    int qos = 2;
    String broker = "tcp://127.0.0.1:1883";
    String clientId = "sample";
    MemoryPersistence persistence = new MemoryPersistence();

    try {
        MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker");
        sampleClient.connect(connOpts);
        System.out.println("connected");
        System.out.println("Publishing meessage: " + content);
        MqttMessage message = new MqttMessage(content.getBytes());
        message.setQos(qos);
        sampleClient.publish(topic, message);
        System.out.println("Message published");
        sampleClient.disconnect();
        System.out.println("Disconnected");
        System.exit(0);
    } catch (MqttException e){
        System.out.println("reason " + e.getReasonCode());
        System.out.println("msg " + e.getMessage());
        System.out.println("loc " + e.getLocalizedMessage());
        System.out.println("cause " + e.getCause());
        System.out.println("exxcep " + e);
    }

}
 
开发者ID:dream-lab,项目名称:echo,代码行数:33,代码来源:MQTTPublisher.java

示例4: pubMsg

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的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();
}
 
开发者ID:osswangxining,项目名称:mqttserver,代码行数:16,代码来源:PubMessage.java

示例5: testWithClientIdResolver

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void testWithClientIdResolver() throws Exception {

	final CountDownLatch cdl = new CountDownLatch(2);

	final MqttClient client = new MqttClient("tcp://localhost:1883", "pyro");

	Mockito.doAnswer(invocation -> {
		cdl.countDown();
		return null;
	}).when(this.connectionStore).upsertConnection("pyro");


	Mockito.doAnswer(invocation -> {
		cdl.countDown();
		return null;
	}).when(this.connectionStore).removeConnection("pyro");

	client.connect();
	client.disconnect();
	cdl.await(20L, TimeUnit.SECONDS);

	final InOrder inOrder = Mockito.inOrder(this.connectionStore);
	inOrder.verify(this.connectionStore).upsertConnection("pyro");
	inOrder.verify(this.connectionStore).removeConnection("pyro");
	inOrder.verifyNoMoreInteractions();
}
 
开发者ID:dcsolutions,项目名称:kalinka,代码行数:28,代码来源:KalinkaClusterPluginTest.java

示例6: destroy

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
/**
 * Shuts down the MQTT client.
 * 
 * @param client
 *            client to shut down.
 */
public void destroy(@Disposes MqttClient client) {
    try {
        if (client != null) {
            client.disconnect();
            logger.info("Disconnected from broker: " + broker);
        }
    } catch (MqttException e) {
        logger.error("Error disconnecting", e);
    }
}
 
开发者ID:zambrovski,项目名称:mqtt-camunda-bpm,代码行数:17,代码来源:MqttClientProducer.java

示例7: disconnect

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void disconnect(TestContext context) {

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
    client.connect();
    client.disconnect();
    context.assertTrue(true);
  } catch (MqttException e) {
    context.assertTrue(false);
    e.printStackTrace();
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:15,代码来源:MqttServerDisconnectTest.java

示例8: main

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public static void main(String[] args) {

        String topic = "MQTT Examples";
        String content = "Message from MqttPublishSample";
        int qos = 0;
        String broker = "tcp://localhost:8080";
        String clientId = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: " + broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: " + content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } 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();
        }
    }
 
开发者ID:lujianbo,项目名称:moonlight-mqtt,代码行数:34,代码来源:Client.java

示例9: main

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public static void main(String[] args) {

        String topic = "java";
        String content = "Message from MqttPublishSample";
        int qos = 2;
        String broker = "tcp://127.0.0.1:1883";
        String clientId = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: " + broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: " + content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } 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();
        }
    }
 
开发者ID:projectsrepos,项目名称:jim,代码行数:34,代码来源:MqttPublishSample.java

示例10: close

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void close(MqttClient client){
	try {
		if(client.isConnected())
			client.disconnect();
		logger.info("client disconnected! clientID:"+client.getClientId());
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
开发者ID:projectsrepos,项目名称:jim,代码行数:10,代码来源:MqttPerformanceClient.java

示例11: run

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void run(String... args) {
    System.out.println("QoS1Producer initializing...");

    String host = args[0];
    String username = args[1];
    String password = args[2];

    if (!host.startsWith("tcp://")) {
        host = "tcp://" + host;
    }

    try {
        // Create an Mqtt client
        MqttClient mqttClient = new MqttClient(host, "HelloWorldQoS1Producer");
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());

        // Connect the client
        System.out.println("Connecting to Solace messaging at " + host);
        mqttClient.connect(connOpts);
        System.out.println("Connected");

        // Create a Mqtt message
        String content = "Hello world from MQTT!";
        MqttMessage message = new MqttMessage(content.getBytes());
        // Set the QoS on the Messages - 
        // Here we are using QoS of 1 (equivalent to Persistent Messages in Solace)
        message.setQos(1);

        System.out.println("Publishing message: " + content);

        // Publish the message
        mqttClient.publish("Q/tutorial", message);

        // Disconnect the client
        mqttClient.disconnect();

        System.out.println("Message published. Exiting");

        System.exit(0);
    } 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();
    }
}
 
开发者ID:SolaceSamples,项目名称:solace-samples-mqtt,代码行数:52,代码来源:QoS1Producer.java

示例12: run

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void run(String... args) {
    System.out.println("TopicPublisher initializing...");

    String host = args[0];
    String username = args[1];
    String password = args[2];

    if (!host.startsWith("tcp://")) {
        host = "tcp://" + host;
    }

    try {
        // Create an Mqtt client
        MqttClient mqttClient = new MqttClient(host, "HelloWorldPub");
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        
        // Connect the client
        System.out.println("Connecting to Solace messaging at " + host);
        mqttClient.connect(connOpts);
        System.out.println("Connected");

        // Create a Mqtt message
        String content = "Hello world from MQTT!";
        MqttMessage message = new MqttMessage(content.getBytes());
        // Set the QoS on the Messages - 
        // Here we are using QoS of 0 (equivalent to Direct Messaging in Solace)
        message.setQos(0);
        
        System.out.println("Publishing message: " + content);
        
        // Publish the message
        mqttClient.publish("T/GettingStarted/pubsub", message);
        
        // Disconnect the client
        mqttClient.disconnect();
        
        System.out.println("Message published. Exiting");

        System.exit(0);
    } 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();
    }
}
 
开发者ID:SolaceSamples,项目名称:solace-samples-mqtt,代码行数:52,代码来源:TopicPublisher.java

示例13: run

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void run(String... args) {
    System.out.println("TopicSubscriber initializing...");

    String host = args[0];
    String username = args[1];
    String password = args[2];

    if (!host.startsWith("tcp://")) {
        host = "tcp://" + host;
    }

    try {
        // Create an Mqtt client
        MqttClient mqttClient = new MqttClient(host, "HelloWorldSub");
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        
        // Connect the client
        System.out.println("Connecting to Solace messaging at "+host);
        mqttClient.connect(connOpts);
        System.out.println("Connected");

        // Latch used for synchronizing b/w threads
        final CountDownLatch latch = new CountDownLatch(1);
        
        // Topic filter the client will subscribe to
        final String subTopic = "T/GettingStarted/pubsub";
        
        // Callback - Anonymous inner-class for receiving messages
        mqttClient.setCallback(new MqttCallback() {

            public void messageArrived(String topic, MqttMessage message) throws Exception {
                // Called when a message arrives from the server that
                // matches any subscription made by the client
                String time = new Timestamp(System.currentTimeMillis()).toString();
                System.out.println("\nReceived a Message!" +
                        "\n\tTime:    " + time + 
                        "\n\tTopic:   " + topic + 
                        "\n\tMessage: " + new String(message.getPayload()) + 
                        "\n\tQoS:     " + message.getQos() + "\n");
                latch.countDown(); // unblock main thread
            }

            public void connectionLost(Throwable cause) {
                System.out.println("Connection to Solace messaging lost!" + cause.getMessage());
                latch.countDown();
            }

            public void deliveryComplete(IMqttDeliveryToken token) {
            }

        });
        
        // Subscribe client to the topic filter and a QoS level of 0
        System.out.println("Subscribing client to topic: " + subTopic);
        mqttClient.subscribe(subTopic, 0);
        System.out.println("Subscribed");

        // Wait for the message to be received
        try {
            latch.await(); // block here until message received, and latch will flip
        } catch (InterruptedException e) {
            System.out.println("I was awoken while waiting");
        }
        
        // Disconnect the client
        mqttClient.disconnect();
        System.out.println("Exiting");

        System.exit(0);
    } 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();
    }
}
 
开发者ID:SolaceSamples,项目名称:solace-samples-mqtt,代码行数:82,代码来源:TopicSubscriber.java

示例14: sendMQTTMessage

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
static public void sendMQTTMessage(String command) throws MqttException {

		String deviceId = "niklas";
		String apikey = "";
		String apitoken = "";
		String deviceType = "kinect";

		String org = null;
		String topic = "iot-2/type/" + deviceType + "/id/" + deviceId
				+ "/cmd/anki/fmt/json";
		int qos = 0;

		boolean configExists = true;
		if (apikey == null)
			configExists = false;
		else {
			if (apikey.equalsIgnoreCase(""))
				configExists = false;
		}
		if (apitoken == null)
			configExists = false;
		else {
			if (apitoken.equalsIgnoreCase(""))
				configExists = false;
		}
		String[] tokens = apikey.split("-", -1);
		if (tokens == null)
			configExists = false;
		else {
			if (tokens.length != 3)
				configExists = false;
			else {
				org = tokens[1];
			}
		}

		String broker = "tcp://" + org
				+ ".messaging.internetofthings.ibmcloud.com:1883";
		String clientId = "a:" + org + ":" + deviceId;

		String content = "";
		
		if (command.equalsIgnoreCase(COMMAND_MOVE)) {
			content = "{\"d\":{\"action\":\"move\"}}";
		} else if (command.equalsIgnoreCase(COMMAND_STOP)) {
			content = "{\"d\":{\"action\":\"stop\"}}";
		} else if (command.equalsIgnoreCase(COMMAND_LEFT)) {
			content = "{\"d\":{\"action\":\"left\"}}";
		} else if (command.equalsIgnoreCase(COMMAND_RIGHT)) {
			content = "{\"d\":{\"action\":\"right\"}}";
		} 

		if (configExists == false)
			throw new MqttException(0);

		MemoryPersistence persistence = new MemoryPersistence();
		MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
		MqttConnectOptions connOpts = new MqttConnectOptions();
		connOpts.setPassword(apitoken.toCharArray());
		connOpts.setUserName(apikey);
		connOpts.setCleanSession(true);

		sampleClient.connect(connOpts);

		MqttMessage message = new MqttMessage(content.getBytes());

		message.setQos(qos);
		sampleClient.publish(topic, message);

		sampleClient.disconnect();
	}
 
开发者ID:IBM-Cloud,项目名称:controller-kinect-bluemix,代码行数:72,代码来源:MQTTUtilities.java

示例15: doDisconnect

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Override
protected synchronized void doDisconnect(MqttClient client) throws Exception {
    if (client.isConnected())
        client.disconnect();
}
 
开发者ID:quarks-edge,项目名称:quarks,代码行数:6,代码来源:MqttConnector.java


注:本文中的org.eclipse.paho.client.mqttv3.MqttClient.disconnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。