@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);
this.connectHandler = connectHandler; if (options.isAutoGeneratedClientId() && (options.getClientId() == null || options.getClientId().isEmpty())) { options.setClientId(generateRandomClientId()); PROTOCOL_NAME, PROTOCOL_VERSION, options.hasUsername(), options.hasPassword(), options.isWillRetain(), options.getWillQoS(), options.isWillFlag(), options.isCleanSession(), options.getKeepAliveTimeSeconds() ); options.getClientId() == null ? "" : options.getClientId(), options.getWillTopic(), options.getWillMessage() != null ? options.getWillMessage().getBytes(StandardCharsets.UTF_8) : null, options.hasUsername() ? options.getUsername() : null, options.hasPassword() ? options.getPassword().getBytes() : null );
public static void toJson(MqttClientOptions obj, java.util.Map<String, Object> json) { json.put("autoGeneratedClientId", obj.isAutoGeneratedClientId()); json.put("autoKeepAlive", obj.isAutoKeepAlive()); json.put("cleanSession", obj.isCleanSession()); if (obj.getClientId() != null) { json.put("clientId", obj.getClientId()); } json.put("keepAliveTimeSeconds", obj.getKeepAliveTimeSeconds()); json.put("maxInflightQueue", obj.getMaxInflightQueue()); json.put("maxMessageSize", obj.getMaxMessageSize()); if (obj.getPassword() != null) { json.put("password", obj.getPassword()); } if (obj.getUsername() != null) { json.put("username", obj.getUsername()); } json.put("willFlag", obj.isWillFlag()); if (obj.getWillMessage() != null) { json.put("willMessage", obj.getWillMessage()); } json.put("willQoS", obj.getWillQoS()); json.put("willRetain", obj.isWillRetain()); if (obj.getWillTopic() != null) { json.put("willTopic", obj.getWillTopic()); } } }
case "autoGeneratedClientId": if (member.getValue() instanceof Boolean) { obj.setAutoGeneratedClientId((Boolean)member.getValue()); obj.setAutoKeepAlive((Boolean)member.getValue()); obj.setCleanSession((Boolean)member.getValue()); obj.setClientId((String)member.getValue()); obj.setKeepAliveTimeSeconds(((Number)member.getValue()).intValue()); obj.setMaxInflightQueue(((Number)member.getValue()).intValue()); obj.setMaxMessageSize(((Number)member.getValue()).intValue()); obj.setPassword((String)member.getValue()); obj.setUsername((String)member.getValue()); obj.setWillFlag((Boolean)member.getValue()); obj.setWillMessage((String)member.getValue()); obj.setWillQoS(((Number)member.getValue()).intValue()); obj.setWillRetain((Boolean)member.getValue()); obj.setWillTopic((String)member.getValue());
@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 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(); }
/** * 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()); }
@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 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 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(); }
Async async = context.async(); MqttClient client = MqttClient.create(Vertx.vertx(), new MqttClientOptions() .setMaxMessageSize(MQTT_MAX_MESSAGE_SIZE) );
@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(); }
private void initChannel(ChannelPipeline pipeline) { // add into pipeline netty's (en/de)coder pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } if (this.options.isAutoKeepAlive() && this.options.getKeepAliveTimeSeconds() != 0) { pipeline.addBefore("handler", "idle", new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0)); pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.WRITER_IDLE) { ping(); } } } }); } }
/** * Do the same thing as {@link MqttClientOptions#setKeepAliveTimeSeconds(int)}. Use it instead. */ @Deprecated @Override public MqttClientOptions setIdleTimeout(int idleTimeout) { return setKeepAliveTimeSeconds(idleTimeout); }
@Override public synchronized String clientId() { return this.options.getClientId(); }
this.connectHandler = connectHandler; if (options.isAutoGeneratedClientId() && (options.getClientId() == null || options.getClientId().isEmpty())) { options.setClientId(generateRandomClientId()); PROTOCOL_NAME, PROTOCOL_VERSION, options.hasUsername(), options.hasPassword(), options.isWillRetain(), options.getWillQoS(), options.isWillFlag(), options.isCleanSession(), options.getKeepAliveTimeSeconds() ); options.getClientId() == null ? "" : options.getClientId(), options.getWillTopic(), options.getWillMessage() != null ? options.getWillMessage().getBytes(StandardCharsets.UTF_8) : null, options.hasUsername() ? options.getUsername() : null, options.hasPassword() ? options.getPassword().getBytes() : null );
public static void toJson(MqttClientOptions obj, java.util.Map<String, Object> json) { json.put("autoGeneratedClientId", obj.isAutoGeneratedClientId()); json.put("autoKeepAlive", obj.isAutoKeepAlive()); json.put("cleanSession", obj.isCleanSession()); if (obj.getClientId() != null) { json.put("clientId", obj.getClientId()); } json.put("keepAliveTimeSeconds", obj.getKeepAliveTimeSeconds()); json.put("maxInflightQueue", obj.getMaxInflightQueue()); json.put("maxMessageSize", obj.getMaxMessageSize()); if (obj.getPassword() != null) { json.put("password", obj.getPassword()); } if (obj.getUsername() != null) { json.put("username", obj.getUsername()); } json.put("willFlag", obj.isWillFlag()); if (obj.getWillMessage() != null) { json.put("willMessage", obj.getWillMessage()); } json.put("willQoS", obj.getWillQoS()); json.put("willRetain", obj.isWillRetain()); if (obj.getWillTopic() != null) { json.put("willTopic", obj.getWillTopic()); } } }
case "autoGeneratedClientId": if (member.getValue() instanceof Boolean) { obj.setAutoGeneratedClientId((Boolean)member.getValue()); obj.setAutoKeepAlive((Boolean)member.getValue()); obj.setCleanSession((Boolean)member.getValue()); obj.setClientId((String)member.getValue()); obj.setKeepAliveTimeSeconds(((Number)member.getValue()).intValue()); obj.setMaxInflightQueue(((Number)member.getValue()).intValue()); obj.setMaxMessageSize(((Number)member.getValue()).intValue()); obj.setPassword((String)member.getValue()); obj.setUsername((String)member.getValue()); obj.setWillFlag((Boolean)member.getValue()); obj.setWillMessage((String)member.getValue()); obj.setWillQoS(((Number)member.getValue()).intValue()); obj.setWillRetain((Boolean)member.getValue()); obj.setWillTopic((String)member.getValue());
@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(); }