@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()); } }); }
@Override public void start() throws Exception { MqttClientOptions options = new MqttClientOptions().setKeepAliveTimeSeconds(2);
/** * Return an MQTT client instance using the default options * * @param vertx Vert.x instance * @return MQTT client instance */ static MqttClient create(Vertx vertx) { return new MqttClientImpl(vertx, new MqttClientOptions()); }
/** * Return an MQTT client instance using the default options * * @param vertx Vert.x instance * @return MQTT client instance */ static MqttClient create(Vertx vertx) { return new MqttClientImpl(vertx, new MqttClientOptions()); }
public static io.vertx.mqtt.MqttClient create(io.vertx.mqtt.MqttClient j_receiver, io.vertx.core.Vertx vertx, java.util.Map<String, Object> options) { return io.vertx.core.impl.ConversionHelper.fromObject(io.vertx.mqtt.MqttClient.create(vertx, options != null ? new io.vertx.mqtt.MqttClientOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(options)) : null)); } }
@Test public void connectDisconnectWithIdleOption(TestContext context) { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); options.setIdleTimeout(100); MqttClient client = MqttClient.create(Vertx.vertx(),options); 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 connectDisconnectWithIdleOption(TestContext context) { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); options.setIdleTimeout(100); MqttClient client = MqttClient.create(Vertx.vertx(),options); 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 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(); }
@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(); }
@Test public void afterConnectClientIdGenerated(TestContext context) throws InterruptedException { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); MqttClient client = MqttClient.create(Vertx.vertx(), options); assertThat(options.getClientId(), nullValue()); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); assertTrue(client.clientId().length() == 36); assertThat(client.clientId(), notNullValue()); assertFalse(client.clientId().isEmpty()); log.info("Client connected with generated client id = " + client.clientId()); async.countDown(); }); async.await(); }
@Test public void afterConnectClientIdGenerated(TestContext context) throws InterruptedException { Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); MqttClient client = MqttClient.create(Vertx.vertx(), options); assertThat(options.getClientId(), nullValue()); client.connect(TestUtil.BROKER_PORT, TestUtil.BROKER_ADDRESS, c -> { assertTrue(c.succeeded()); assertTrue(client.clientId().length() == 36); assertThat(client.clientId(), notNullValue()); assertFalse(client.clientId().isEmpty()); log.info("Client connected with generated client id = " + client.clientId()); async.countDown(); }); async.await(); }
@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(); } }
@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 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(); } }
@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 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 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 manualPing(TestContext context) throws InterruptedException { Vertx vertx = Vertx.vertx(); Async async = context.async(); MqttClientOptions options = new MqttClientOptions(); options.setAutoKeepAlive(false); log.info("Manual ping ... " + PING_NUMBER + " times timeout " + KEEPALIVE_TIMEOUT); count = 0; MqttClient client = MqttClient.create(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) { vertx.cancelTimer(timerId); client.disconnect(); async.countDown(); } }); vertx.setPeriodic(KEEPALIVE_TIMEOUT * 1000, t -> { timerId = t; log.info("Pingreq --> "); client.ping(); }); }); async.await(); }
@Test public void clientSslClientTruststoreTest(TestContext context) { this.context = context; JksOptions jksOptions = new JksOptions().setPath("/tls/client-truststore.jks"); MqttClientOptions clientOptions = new MqttClientOptions() .setSsl(true) .setTrustStoreOptions(jksOptions); MqttClient client = MqttClient.create(vertx, clientOptions); client.exceptionHandler(t -> context.assertTrue(false)); Async async = context.async(); client.connect(MQTT_SERVER_TLS_PORT, MQTT_SERVER_HOST, s -> client.disconnect(d -> async.countDown())); async.await(); }
@Test public void clientSslClientTruststoreTest(TestContext context) { this.context = context; JksOptions jksOptions = new JksOptions().setPath("/tls/client-truststore.jks"); MqttClientOptions clientOptions = new MqttClientOptions() .setSsl(true) .setTrustStoreOptions(jksOptions); MqttClient client = MqttClient.create(vertx, clientOptions); client.exceptionHandler(t -> context.assertTrue(false)); Async async = context.async(); client.connect(MQTT_SERVER_TLS_PORT, MQTT_SERVER_HOST, s -> client.disconnect(d -> async.countDown())); async.await(); }