@Override public void start() throws Exception { MqttClient mqttClient = MqttClient.create(vertx); mqttClient.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) { System.out.println("Connected to a server"); mqttClient.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, s -> mqttClient.disconnect(d -> System.out.println("Disconnected from server"))); } else { System.out.println("Failed to connect to a server"); System.out.println(ch.cause()); } }); } }
client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE),
private void clientSendThreePublishMessages(MqttQoS mqttQoS, TestContext context) { Async async = context.async(3); MqttClient client = MqttClient.create(vertx); Queue<Integer> expectOrder = new LinkedList<>(); // order we expect to receive acknowledgment for published message expectOrder.add(2); expectOrder.add(1); expectOrder.add(3); client.publishCompletionHandler(h -> { context.assertEquals(h.intValue(), expectOrder.poll()); log.info("[CLIENT] Publish completed for message with id: " + h); async.countDown(); }); client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, c -> { // publish QoS = 1 message three times for (int i = 0; i < 3; i++) client.publish(MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()), mqttQoS, false, false, h -> log.info("[CLIENT] publishing message id = " + h.result())); }); async.await(); client.disconnect(); }
private void subscribeAndReceive(TestContext context, MqttQoS qos) { Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx()); client.publishHandler(publish -> { assertTrue(publish.qosLevel() == qos); log.info("Just received message on [" + publish.topicName() + "] payload [" + publish.payload().toString(Charset.defaultCharset()) + "] with QoS [" + publish.qosLevel() + "]"); client.disconnect(); async.countDown(); }); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> { assertTrue(ar.succeeded()); client.subscribe(MQTT_TOPIC, qos.value()); client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()), qos, false, false ); }); async.await(); }
private void clientSendThreePublishMessages(MqttQoS mqttQoS, TestContext context) { Async async = context.async(3); MqttClient client = MqttClient.create(vertx); Queue<Integer> expectOrder = new LinkedList<>(); // order we expect to receive acknowledgment for published message expectOrder.add(2); expectOrder.add(1); expectOrder.add(3); client.publishCompletionHandler(h -> { context.assertEquals(h.intValue(), expectOrder.poll()); log.info("[CLIENT] Publish completed for message with id: " + h); async.countDown(); }); client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, c -> { // publish QoS = 1 message three times for (int i = 0; i < 3; i++) client.publish(MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()), mqttQoS, false, false, h -> log.info("[CLIENT] publishing message id = " + h.result())); }); async.await(); client.disconnect(); }
@Override public void start() throws Exception { MqttClientOptions options = new MqttClientOptions(); options.setSsl(true); options.setTrustAll(true); MqttClient mqttClient = MqttClient.create(vertx, options); mqttClient.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) { System.out.println("Connected to a server"); mqttClient.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, s -> mqttClient.disconnect(d -> System.out.println("Disconnected from server"))); } else { System.out.println("Failed to connect to a server"); System.out.println(ch.cause()); } }); }
private void subscribeAndReceive(TestContext context, MqttQoS qos) { Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx()); client.publishHandler(publish -> { assertTrue(publish.qosLevel() == qos); log.info("Just received message on [" + publish.topicName() + "] payload [" + publish.payload().toString(Charset.defaultCharset()) + "] with QoS [" + publish.qosLevel() + "]"); client.disconnect(); async.countDown(); }); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> { assertTrue(ar.succeeded()); client.subscribe(MQTT_TOPIC, qos.value()); client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()), qos, false, false ); }); async.await(); }
client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()),
/** * Sends the PUBLISH message to the remote MQTT server * @param topic topic on which the message is published * @param payload message payload * @param qosLevel QoS level * @param isDup if the message is a duplicate * @param isRetain if the message needs to be retained * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient publish(String topic, io.vertx.rxjava.core.buffer.Buffer payload, MqttQoS qosLevel, boolean isDup, boolean isRetain) { delegate.publish(topic, payload.getDelegate(), qosLevel, isDup, isRetain); return this; }
publisher.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()),
client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()),
/** * Sends the PUBLISH message to the remote MQTT server * @param topic topic on which the message is published * @param payload message payload * @param qosLevel QoS level * @param isDup if the message is a duplicate * @param isRetain if the message needs to be retained * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient publish(String topic, io.vertx.rxjava.core.buffer.Buffer payload, MqttQoS qosLevel, boolean isDup, boolean isRetain) { delegate.publish(topic, payload.getDelegate(), qosLevel, isDup, isRetain); return this; }
publisher.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE.getBytes()),
/** * Sends the PUBLISH message to the remote MQTT server * @param topic topic on which the message is published * @param payload message payload * @param qosLevel QoS level * @param isDup if the message is a duplicate * @param isRetain if the message needs to be retained * @param publishSentHandler handler called after PUBLISH packet sent with packetid (not when QoS 0) * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient publish(String topic, io.vertx.rxjava.core.buffer.Buffer payload, MqttQoS qosLevel, boolean isDup, boolean isRetain, Handler<AsyncResult<Integer>> publishSentHandler) { delegate.publish(topic, payload.getDelegate(), qosLevel, isDup, isRetain, publishSentHandler); return this; }
/** * Sends the PUBLISH message to the remote MQTT server * @param topic topic on which the message is published * @param payload message payload * @param qosLevel QoS level * @param isDup if the message is a duplicate * @param isRetain if the message needs to be retained * @param publishSentHandler handler called after PUBLISH packet sent with packetid (not when QoS 0) * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient publish(String topic, io.vertx.rxjava.core.buffer.Buffer payload, MqttQoS qosLevel, boolean isDup, boolean isRetain, Handler<AsyncResult<Integer>> publishSentHandler) { delegate.publish(topic, payload.getDelegate(), qosLevel, isDup, isRetain, publishSentHandler); return this; }
Assert.assertTrue(c.succeeded()); client.publish( topicName, Buffer.buffer(MQTT_MESSAGE.getBytes()),
Assert.assertTrue(c.succeeded()); client.publish( topicName, Buffer.buffer(MQTT_MESSAGE.getBytes()),
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"); });
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"); });