-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathClient.java
51 lines (42 loc) · 1.56 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
49
50
51
package io.vertx.example.mqtt.ssl;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.buffer.Buffer;
import io.vertx.launcher.application.VertxApplication;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;
public class Client extends VerticleBase {
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) {
VertxApplication.main(new String[]{Client.class.getName()});
}
private MqttClient mqttClient;
@Override
public Future<?> start() {
MqttClientOptions options = new MqttClientOptions();
options.setSsl(true);
// In real life you would use a certificate and not trust any server
options.setTrustAll(true);
options.setHostnameVerificationAlgorithm("http");
mqttClient = MqttClient.create(vertx, options);
return
mqttClient.connect(BROKER_PORT, BROKER_HOST)
.compose(ack -> {
System.out.println("Connected to a server");
return mqttClient.publish(
MQTT_TOPIC,
Buffer.buffer(MQTT_MESSAGE),
MqttQoS.AT_MOST_ONCE,
false,
false)
.onSuccess(ignore -> System.out.println("Message published"));
})
.eventually(() -> mqttClient
.disconnect()
.onComplete(ar -> System.out.println("Disconnected from server")));
}
}