Java Code Examples for io.vertx.mqtt.MqttClient#subscribeCompletionHandler()

The following examples show how to use io.vertx.mqtt.MqttClient#subscribeCompletionHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxMqttClientExamples.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
/**
 * Example for subscribeCompletionHandler method demonstration
 *
 * @param client
 */
public void example6(MqttClient client) {
  client.subscribeCompletionHandler(mqttSubAckMessage -> {
    System.out.println("Id of just received SUBACK packet is " + mqttSubAckMessage.messageId());
    for (int s : mqttSubAckMessage.grantedQoSLevels()) {
      if (s == 0x80) {
        System.out.println("Failure");
      } else {
        System.out.println("Success. Maximum QoS is " + s);
      }
    }
  });
  client.subscribe("temp", 1);
  client.subscribe("temp2", 2);
}
 
Example 2
Source File: MqttClientSubscribeTest.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
private void subscribe(TestContext context, MqttQoS qos) {

    this.messageId = 0;

    Async async = context.async();
    MqttClient client = MqttClient.create(Vertx.vertx());

    client.subscribeCompletionHandler(suback -> {
      assertTrue(suback.messageId() == messageId);
      assertTrue(suback.grantedQoSLevels().contains(qos.value()));
      log.info("subscribing complete for message id = " + suback.messageId() + " with QoS " + suback.grantedQoSLevels());
      client.disconnect();
      async.countDown();
    });

    client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> {
      assertTrue(ar.succeeded());

      client.subscribe(MQTT_TOPIC, qos.value(), done -> {
        assertTrue(done.succeeded());
        messageId = done.result();
        log.info("subscribing on [" + MQTT_TOPIC + "] with QoS [" + qos.value() + "] message id = " + messageId);
      });
    });

    async.await();
  }
 
Example 3
Source File: MqttClientUnsubscribeTest.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
private void unsubscribe(TestContext context, MqttQoS qos) {

    this.messageId = 0;

    Async async = context.async();
    MqttClient client = MqttClient.create(Vertx.vertx());

    client.unsubscribeCompletionHandler(unsubackid -> {
      assertTrue(unsubackid == messageId);
      log.info("unsubscribing complete for message id = " + unsubackid);
      client.disconnect();
      async.countDown();
    });

    client.subscribeCompletionHandler(suback -> {
      assertTrue(suback.messageId() == messageId);
      assertTrue(suback.grantedQoSLevels().contains(qos.value()));
      log.info("subscribing complete for message id = " + suback.messageId() + " with QoS " + suback.grantedQoSLevels());

      client.unsubscribe(MQTT_TOPIC, ar2 -> {
        assertTrue(ar2.succeeded());
        messageId = ar2.result();
        log.info("unsubscribing on [" + MQTT_TOPIC + "] message id = " + messageId);
      });
    });

    client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> {
      assertTrue(ar.succeeded());

      client.subscribe(MQTT_TOPIC, qos.value(), ar1 -> {
        assertTrue(ar1.succeeded());
        messageId = ar1.result();
        log.info("subscribing on [" + MQTT_TOPIC + "] with QoS [" + qos.value() + "] message id = " + messageId);
      });
    });

    async.await();
  }
 
Example 4
Source File: MqttClientMaxMessageSizeTest.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void decoderMaxMessageSize(TestContext context) throws InterruptedException {
  Async async = context.async();
  MqttClient client = MqttClient.create(Vertx.vertx(),
    new MqttClientOptions()
      .setMaxMessageSize(MQTT_MAX_MESSAGE_SIZE)
  );

  client.subscribeCompletionHandler(sc -> {
    log.info("SUBACK <---");
    byte[] message = new byte[MQTT_BIG_MESSAGE_SIZE];
    client.publish(MQTT_TOPIC, Buffer.buffer(message), AT_MOST_ONCE, false, false);
    log.info("PUBLISH ---> ... with big message size which should cause decoder exception");
  });

  client.exceptionHandler(t->{
    log.error("Exception raised", t);

    if (t instanceof DecoderException) {
      log.info("PUBLISH <--- message with big size");
      async.countDown();
    }
  });

  log.info("CONNECT --->");
  client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> {
    assertTrue(c.succeeded());
    log.info("CONNACK <---");
    client.subscribe(MQTT_TOPIC, 0);
    log.info("SUBSCRIBE --->");
  });

  async.await();
}