-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Client.java
48 lines (39 loc) · 1.43 KB
/
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package io.vertx.example.mqtt.ssl;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.buffer.Buffer;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;
public class Client extends AbstractVerticle {
private static final String MQTT_TOPIC = "/my_topic";
private static final String MQTT_MESSAGE = "Hello Vert.x MQTT Client";
private static final String BROKER_HOST = "localhost";
private static final int BROKER_PORT = 8883;
public static void main(String[] args) {
Launcher.executeCommand("run", Client.class.getName());
}
@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).onComplete(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)
.compose(s -> mqttClient.disconnect())
.onComplete(d -> System.out.println("Disconnected from server"));
} else {
System.out.println("Failed to connect to a server");
System.out.println(ch.cause());
}
});
}
}