@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()); } }); } }
MqttClient client = MqttClient.create(Vertx.vertx(), options); client.publishHandler(publish -> { System.out.println("Just received message on [" + publish.topicName() + "] payload [" + publish.payload().toString(Charset.defaultCharset()) + "] with QoS [" + publish.qosLevel() + "]"); }); client.subscribeCompletionHandler(h -> { System.out.println("Receive SUBACK from server with granted QoS : " + h.grantedQoSLevels()); client.publish( MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), vertx.setTimer(5000, l -> client.unsubscribe(MQTT_TOPIC)); }); client.unsubscribeCompletionHandler(h -> { System.out.println("Receive UNSUBACK from server"); vertx.setTimer(5000, l -> client.disconnect(d -> System.out.println("Disconnected form server")) ); }); client.connect(BROKER_PORT, BROKER_HOST, ch -> { if (ch.succeeded()) { System.out.println("Connected to a server"); client.subscribe(MQTT_TOPIC, 0); } else {
public void testSubscribe(String topicFilter, boolean mustBeValid, TestContext context) { log.info(String.format("test subscribing for \"%s\" topic", topicFilter)); Async async = context.async(2); MqttClient client = MqttClient.create(Vertx.vertx()); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { Assert.assertTrue(c.succeeded()); client.subscribe( topicFilter, 0, ar -> { assertThat(ar.succeeded(), is(mustBeValid)); log.info("subscribe message id = " + ar.result()); async.countDown(); client .disconnect(ar1 -> { Assert.assertTrue(ar1.succeeded()); async.countDown(); }); }); }); async.await(); } }
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(); }); 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();
MqttClient subscriber1 = MqttClient.create(Vertx.vertx()); MqttClient subscriber2 = MqttClient.create(Vertx.vertx()); MqttClient publisher = MqttClient.create(Vertx.vertx()); subscriber1.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> { subscriber1.publishHandler(message -> { log.error("Subscriber " + subscriber1.clientId() + " received message " + new String(message.payload().getBytes())); context.fail(); }); subscriber1.subscribe(MQTT_TOPIC, MqttQoS.AT_MOST_ONCE.value(), ar1 -> { log.info("Subscriber " + subscriber1.clientId() + " subscribed to " + MQTT_TOPIC); subscriber1.unsubscribe(MQTT_TOPIC, ar2 -> { log.info("Subscriber " + subscriber1.clientId() + " un-subscribed from " + MQTT_TOPIC); publish.countDown(); }); subscriber2.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, ar -> { subscriber2.publishHandler(message -> { log.error("Subscriber " + subscriber2.clientId() + " received message " + new String(message.payload().getBytes())); async.complete(); }); subscriber2.subscribe(MQTT_TOPIC, MqttQoS.AT_MOST_ONCE.value(), ar1 -> {
@Test public void autoPing(TestContext context) throws InterruptedException { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); options.setKeepAliveTimeSeconds(KEEPALIVE_TIMEOUT); log.info("Auto ping ... " + PING_NUMBER + " times timeout " + KEEPALIVE_TIMEOUT); count = 0; MqttClient client = MqttClient.create(Vertx.vertx(), options); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); client.pingResponseHandler(v -> { log.info("Pingresp <-- "); count++; if (count == PING_NUMBER) { client.disconnect(); async.countDown(); } }); }); async.await(); } }
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(); }
@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); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); log.info("CONNACK <---"); client.subscribe(MQTT_TOPIC, 0); log.info("SUBSCRIBE --->"); });
@Test public void clientSslTrustAllTest(TestContext context) { MqttClientOptions clientOptions = new MqttClientOptions() .setSsl(true) .setTrustAll(true); MqttClient client = MqttClient.create(vertx, clientOptions); client.exceptionHandler(t -> context.assertTrue(false)); this.context = context; Async async = context.async(); client.connect(MQTT_SERVER_TLS_PORT, MQTT_SERVER_HOST, s -> client.disconnect(d -> async.countDown())); async.await(); }
@Test public void tcpConnectionFails(TestContext context) { Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx()); client.closeHandler(v -> { // when TCP connection fails, this handler should not be called, connection not established context.fail(); }); client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, c -> { // connection assertTrue(c.failed()); assertFalse(client.isConnected()); async.complete(); }); async.await(); }
@Test public void connectDisconnect(TestContext context) throws InterruptedException { Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx()); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); client .disconnect(ar -> { assertTrue(ar.succeeded()); async.countDown(); }); }); async.await(); }
@Test public void afterConnectClientId(TestContext context) { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); options.setClientId("myClient"); MqttClient client = MqttClient.create(Vertx.vertx(), options); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); assertThat(client.clientId(), notNullValue()); assertFalse(client.clientId().isEmpty()); assertEquals(client.clientId(), "myClient"); log.info("Client connected with requested client id = " + client.clientId()); async.countDown(); }); async.await(); } }
@Test public void closeHandler(TestContext context) throws InterruptedException { Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx(), new MqttClientOptions() .setKeepAliveTimeSeconds(5) .setAutoKeepAlive(false) ); client.closeHandler((v) -> { async.countDown(); }); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); }); async.await(); }
/** * Return an MQTT client instance * @param vertx Vert.x instance * @param options MQTT client options * @return MQTT client instance */ public static io.vertx.rxjava.mqtt.MqttClient create(io.vertx.rxjava.core.Vertx vertx, MqttClientOptions options) { io.vertx.rxjava.mqtt.MqttClient ret = io.vertx.rxjava.mqtt.MqttClient.newInstance(io.vertx.mqtt.MqttClient.create(vertx.getDelegate(), options)); return ret; }
/** * Subscribes to the topics with related QoS levels * @param topics topics and related QoS levels to subscribe to * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient subscribe(Map<String, Integer> topics) { delegate.subscribe(topics); return this; }
/** * Unsubscribe from receiving messages on given topic * @param topic Topic you want to unsubscribe from * @param unsubscribeSentHandler handler called after UNSUBSCRIBE packet sent * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient unsubscribe(String topic, Handler<AsyncResult<Integer>> unsubscribeSentHandler) { delegate.unsubscribe(topic, unsubscribeSentHandler); return this; }
/** * Disconnects from the MQTT server * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient disconnect() { delegate.disconnect(); 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 * @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; }
/** * Connects to an MQTT server calling connectHandler after connection * @param port port of the MQTT server * @param host hostname/ip address of the MQTT server * @param connectHandler handler called when the asynchronous connect call ends * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient connect(int port, String host, Handler<AsyncResult<io.vertx.rxjava.mqtt.messages.MqttConnAckMessage>> connectHandler) { delegate.connect(port, host, new Handler<AsyncResult<io.vertx.mqtt.messages.MqttConnAckMessage>>() { public void handle(AsyncResult<io.vertx.mqtt.messages.MqttConnAckMessage> ar) { if (ar.succeeded()) { connectHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.mqtt.messages.MqttConnAckMessage.newInstance(ar.result()))); } else { connectHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); return this; }
/** * Sets handler which will be called after UNSUBACK packet receiving * @param unsubscribeCompletionHandler handler to call with the packetid * @return current MQTT client instance */ public io.vertx.rxjava.mqtt.MqttClient unsubscribeCompletionHandler(Handler<Integer> unsubscribeCompletionHandler) { delegate.unsubscribeCompletionHandler(unsubscribeCompletionHandler); return this; }