-
Notifications
You must be signed in to change notification settings - Fork 219
/
MqttClientActorIT.java
617 lines (514 loc) · 25.4 KB
/
MqttClientActorIT.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Status;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationModelFactory;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.api.BaseClientState;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.mqtt.IllegalSessionExpiryIntervalSecondsException;
import org.eclipse.ditto.connectivity.model.mqtt.ReceiveMaximum;
import org.eclipse.ditto.connectivity.model.mqtt.SessionExpiryInterval;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.CloseConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.OpenConnection;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.KeepAliveInterval;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.connect.GenericMqttConnect;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.publish.GenericMqttPublish;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.subscribe.GenericMqttSubscribe;
import org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.message.subscribe.GenericMqttSubscription;
import org.eclipse.ditto.internal.utils.test.docker.mosquitto.MosquittoResource;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.google.common.collect.Sets;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
import com.hivemq.client.mqtt.MqttVersion;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.datatypes.MqttTopic;
import com.hivemq.client.mqtt.datatypes.MqttTopicFilter;
import com.hivemq.client.mqtt.mqtt3.Mqtt3BlockingClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.duration.FiniteDuration;
import scala.jdk.CollectionConverters;
/**
* Tests {@link MqttClientActor}.
*/
@RunWith(Parameterized.class)
public final class MqttClientActorIT {
@Parameterized.Parameters(name = "MQTT version: {0}, separate publisher client: {1}, clean session: {2}")
public static Collection<Object[]> parameters() {
return Stream.of(MqttVersion.MQTT_3_1_1, MqttVersion.MQTT_5_0).flatMap(mqttVersion ->
Stream.of(true, false).flatMap(separatePublisherClient ->
Stream.of(true, false).map(cleanSession ->
new Object[] { mqttVersion, separatePublisherClient, cleanSession })))
.map(Object[].class::cast)
.toList();
}
@Parameterized.Parameter(0)
public static MqttVersion mqttVersion;
@Parameterized.Parameter(1)
public static Boolean separatePublisherClient;
@Parameterized.Parameter(2)
public static Boolean cleanSession;
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
@ClassRule
public static final MosquittoResource MOSQUITTO_RESOURCE = new MosquittoResource("mosquitto.conf");
private static final ConnectionId CONNECTION_ID = ConnectionId.of("connection");
private static final String CLIENT_ID_DITTO = "ditto";
private static final String TOPIC_NAME = "topic";
private static final String ANOTHER_TOPIC_NAME = "topic2";
private static final AuthorizationContext AUTHORIZATION_CONTEXT = AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.PRE_AUTHENTICATED_CONNECTION,
AuthorizationModelFactory.newAuthSubject("nginx:ditto"));
private static final FiniteDuration CONNECTION_TIMEOUT = FiniteDuration.apply(30, TimeUnit.SECONDS);
private static final FiniteDuration COMMAND_TIMEOUT = FiniteDuration.apply(5, TimeUnit.SECONDS);
private static final FiniteDuration NO_MESSAGE_TIMEOUT = FiniteDuration.apply(3, TimeUnit.SECONDS);
private static final int MESSAGES_FROM_PREVIOUS_SESSION_TIMEOUT = 3;
private static final SessionExpiryInterval SESSION_EXPIRY_INTERVAL;
static {
try {
SESSION_EXPIRY_INTERVAL = SessionExpiryInterval.of(Duration.ofSeconds(60));
} catch (IllegalSessionExpiryIntervalSecondsException e) {
throw new RuntimeException(e);
}
}
private static Config actorsTestConfig;
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
@Rule public TestName name = new TestName();
private ActorSystem actorSystem;
private TestProbe commandForwarderProbe;
@BeforeClass
public static void beforeClass() {
actorsTestConfig = ConfigFactory.load("test.conf");
}
@Before
public void before() {
actorSystem = ActorSystem.create(getClass().getSimpleName(), actorsTestConfig);
commandForwarderProbe = TestProbe.apply("commandForwarder", actorSystem);
cleanPreviousSession();
}
private void cleanPreviousSession() {
final var mqttClient = getMqttClient(getDittoClientId());
mqttClient.cleanSession();
}
@After
public void after() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
actorSystem = null;
}
}
@Test
public void testSingleTopic() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }));
connect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key2", "test2");
mqttClient.disconnect();
expectMergeThingMessage(commandForwarderProbe, "key", "test");
commandForwarderProbe.expectNoMessage(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};
}
@Test
public void testMultipleTopics() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME, ANOTHER_TOPIC_NAME }));
connect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key2", "test2");
mqttClient.disconnect();
expectMergeThingMessage(commandForwarderProbe, "key", "test");
expectMergeThingMessage(commandForwarderProbe, "key2", "test2");
expectNoMessage(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};
}
@Test
public void testMultipleSources() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }, new String[] { ANOTHER_TOPIC_NAME }));
connect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key2", "test2");
mqttClient.disconnect();
expectMergeThingMessages(commandForwarderProbe,
Map.of("key", "test",
"key2", "test2"));
commandForwarderProbe.expectNoMessage(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};}
@Test
public void testPersistentSession() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }));
// create session
connect(underTest, this);
disconnect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key2", "test2");
mqttClient.disconnect();
connect(underTest, this);
expectMergeThingMessageIfNotCleanSession(commandForwarderProbe, "key", "test");
expectMergeThingMessageIfNotCleanSession(commandForwarderProbe, "key2", "test2");
commandForwarderProbe.expectNoMsg(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};
}
@Test
public void testPersistentSessionWithMultipleSources() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }, new String[] { ANOTHER_TOPIC_NAME }));
// create session
connect(underTest, this);
disconnect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key3", "test3");
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key2", "test2");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key4", "test4");
mqttClient.disconnect();
connect(underTest, this);
expectMergeThingMessagesIfNotCleanSession(commandForwarderProbe,
Map.of("key", "test",
"key2", "test2",
"key3", "test3",
"key4", "test4"));
commandForwarderProbe.expectNoMsg(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};
}
@Test
public void testPersistentSessionMessageFromTopicWhichIsNoLongerSubscribed() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }));
// create session for ditto client ID with subscription to 2 topics
final var dittoMqttClient = getMqttClient(getDittoClientId());
dittoMqttClient.connect(GenericMqttConnect.newInstance(false, KeepAliveInterval.defaultKeepAlive(), SESSION_EXPIRY_INTERVAL, ReceiveMaximum.defaultReceiveMaximum()));
dittoMqttClient.subscribe(GenericMqttSubscribe.of(Set.of(
GenericMqttSubscription.newInstance(MqttTopicFilter.of(TOPIC_NAME), MqttQos.EXACTLY_ONCE),
GenericMqttSubscription.newInstance(MqttTopicFilter.of(ANOTHER_TOPIC_NAME), MqttQos.EXACTLY_ONCE))));
dittoMqttClient.disconnect();
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
publishMergeThingMessage(mqttClient, ANOTHER_TOPIC_NAME, "key2", "test2");
mqttClient.disconnect();
connect(underTest, this);
expectMergeThingMessageIfNotCleanSession(commandForwarderProbe, "key", "test");
commandForwarderProbe.expectNoMsg(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
ensureAllMessagesAcknowledged();
}};
}
@Test
public void testSingleTopicAfterReconnect() {
new TestKit(actorSystem) {{
final var underTest = getMqttClientActor(getConnection(new String[] { TOPIC_NAME }));
connect(underTest, this);
final var mqttClient = getMqttClient(name.getMethodName());
mqttClient.connect();
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
expectMergeThingMessage(commandForwarderProbe, "key", "test");
commandForwarderProbe.expectNoMessage(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
connect(underTest, this);
publishMergeThingMessage(mqttClient, TOPIC_NAME, "key", "test");
expectMergeThingMessage(commandForwarderProbe, "key", "test");
commandForwarderProbe.expectNoMessage(NO_MESSAGE_TIMEOUT);
disconnect(underTest, this);
mqttClient.disconnect();
ensureAllMessagesAcknowledged();
}};
}
private ActorRef getMqttClientActor(final Connection connection) {
return actorSystem.actorOf(MqttClientActor.props(
connection,
commandForwarderProbe.ref(),
TestProbe.apply("connectionActor", actorSystem).ref(),
DittoHeaders.empty(),
ConfigFactory.empty()));
}
private String getDittoClientId() {
return CLIENT_ID_DITTO + "-" + name.getMethodName();
}
private Connection getConnection(final String[]... sourcesTopics) {
return ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
mqttVersion.equals(MqttVersion.MQTT_3_1_1) ? ConnectionType.MQTT : ConnectionType.MQTT_5,
ConnectivityStatus.CLOSED,
"tcp://" + MOSQUITTO_RESOURCE.getBindIp() + ":" + MOSQUITTO_RESOURCE.getPort())
.specificConfig(Map.of(
"clientId", getDittoClientId(),
"cleanSession", String.valueOf(cleanSession),
"separatePublisherClient", String.valueOf(separatePublisherClient)))
.setSources(Arrays.stream(sourcesTopics)
.map(topics -> ConnectivityModelFactory.newSourceBuilder()
.authorizationContext(AUTHORIZATION_CONTEXT)
.qos(MqttQos.EXACTLY_ONCE.getCode())
.addresses(Sets.newHashSet(topics))
.build())
.toList())
.build();
}
private static void connect(final ActorRef underTest, final TestKit testKit) {
underTest.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), testKit.getRef());
testKit.expectMsg(CONNECTION_TIMEOUT, new Status.Success(BaseClientState.CONNECTED));
}
private static void disconnect(final ActorRef underTest, final TestKit testKit) {
underTest.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), testKit.getRef());
testKit.expectMsg(CONNECTION_TIMEOUT, new Status.Success(BaseClientState.DISCONNECTED));
}
private static void publishMergeThingMessage(final GenericBlockingMqttClient mqttClient, final String topic, final String key, final String value) {
mqttClient.publish(GenericMqttPublish.builder(MqttTopic.of(topic), MqttQos.EXACTLY_ONCE)
.payload(ByteBuffer.wrap(String.format("""
{
"topic": "test/thing-01/things/twin/commands/merge",
"path": "/attributes/%s",
"headers": {
"content-type": "application/merge-patch+json",
"requested-acks": []
},
"value": "%s"
}
""", key, value).getBytes(StandardCharsets.UTF_8)))
.contentType("application/vnd.eclipse.ditto+json")
.build()
);
}
private static void sleep(final int seconds) {
try {
Thread.sleep(seconds * 1000L);
} catch (InterruptedException ignored) {
}
}
private GenericBlockingMqttClient getMqttClient(final String clientId) {
return GenericBlockingMqttClientBuilder.newInstance(mqttVersion, MOSQUITTO_RESOURCE.getBindIp(), MOSQUITTO_RESOURCE.getPort())
.clientIdentifier(clientId)
.build();
}
private void expectMergeThingMessageIfNotCleanSession(final TestProbe testProbe, final String key, final String value) {
if (!cleanSession) {
expectMergeThingMessage(testProbe, key, value);
}
}
private void expectMergeThingMessagesIfNotCleanSession(final TestProbe testProbe, final Map<String, String> updates) {
if (!cleanSession) {
expectMergeThingMessages(testProbe, updates);
}
}
private void expectMergeThingMessage(final TestProbe testProbe, final String key, final String value) {
final var command = testProbe.expectMsgClass(COMMAND_TIMEOUT, MergeThing.class);
softly.assertThat((CharSequence) command.getEntityId())
.as("entity ID")
.isEqualTo(ThingId.of("test:thing-01"));
softly.assertThat((CharSequence) command.getPath())
.as("path")
.isEqualTo(JsonFactory.newPointer(String.format("/attributes/%s", key)));
softly.assertThat(command.getValue())
.as("value")
.isEqualTo(JsonFactory.newValue(value));
}
private void expectMergeThingMessages(final TestProbe testProbe, final Map<String, String> updates) {
final var messages = CollectionConverters.SeqHasAsJava(testProbe.receiveN(updates.size(), COMMAND_TIMEOUT)).asJava();
assertThat(messages).hasSize(updates.size());
// Can't create map here, because in case of error the key might duplicate, and we'll get ugly error instead of
// pretty assertion failure.
final var actualUpdates = messages.stream()
.filter(MergeThing.class::isInstance)
.map(MergeThing.class::cast)
.map(m -> Map.entry(
String.valueOf(m.getPath()).replace("/attributes/", ""),
m.getValue().asString()))
.toList();
assertThat(actualUpdates).containsExactlyInAnyOrderElementsOf(updates.entrySet());
}
private void ensureAllMessagesAcknowledged() {
final var mqttClient = getMqttClient(getDittoClientId());
final var unacknowledgedPublishes = new ArrayList<GenericMqttPublish>();
mqttClient.setPublishesCallback(MqttGlobalPublishFilter.ALL, unacknowledgedPublishes::add);
mqttClient.connect(GenericMqttConnect.newInstance(false, KeepAliveInterval.defaultKeepAlive(), SessionExpiryInterval.defaultSessionExpiryInterval(), ReceiveMaximum.defaultReceiveMaximum()));
sleep(MESSAGES_FROM_PREVIOUS_SESSION_TIMEOUT);
mqttClient.disconnect();
assertThat(unacknowledgedPublishes).isEmpty();
}
private interface GenericBlockingMqttClient {
void connect();
void connect(final GenericMqttConnect connect);
void disconnect();
void cleanSession();
void setPublishesCallback(final MqttGlobalPublishFilter mqttGlobalPublishFilter, final Consumer<GenericMqttPublish> callback);
void publish(final GenericMqttPublish publish);
void subscribe(final GenericMqttSubscribe subscribe);
}
private static class GenericBlockingMqttClientBuilder {
private final MqttVersion mqttVersion;
private final String host;
private final Integer port;
@Nullable private String clientId;
private GenericBlockingMqttClientBuilder(final MqttVersion mqttVersion, final String host, final int port) {
this.mqttVersion = mqttVersion;
this.host = host;
this.port = port;
}
public static GenericBlockingMqttClientBuilder newInstance(final MqttVersion mqttVersion, final String host, final int port) {
return new GenericBlockingMqttClientBuilder(mqttVersion, host, port);
}
public GenericBlockingMqttClientBuilder clientIdentifier(final String clientId) {
this.clientId = clientId;
return this;
}
public GenericBlockingMqttClient build() {
return mqttVersion == MqttVersion.MQTT_3_1_1 ?
new Mqtt3BlockingMqttClient(this) :
new Mqtt5BlockingMqttClient(this);
}
private static final class Mqtt3BlockingMqttClient implements GenericBlockingMqttClient {
private final Mqtt3BlockingClient client;
private Mqtt3BlockingMqttClient(final GenericBlockingMqttClientBuilder builder) {
var mqtt3ClientBuilder = MqttClient.builder()
.useMqttVersion3()
.serverHost(builder.host)
.serverPort(builder.port);
if (builder.clientId != null) {
mqtt3ClientBuilder = mqtt3ClientBuilder.identifier(builder.clientId);
}
client = mqtt3ClientBuilder.buildBlocking();
}
@Override
public void connect() {
client.connect();
}
@Override
public void connect(final GenericMqttConnect connect) {
client.connect(connect.getAsMqtt3Connect());
}
@Override
public void disconnect() {
client.disconnect();
}
@Override
public void cleanSession() {
client.connectWith().cleanSession(true).send();
client.disconnect();
}
@Override
public void setPublishesCallback(final MqttGlobalPublishFilter mqttGlobalPublishFilter, final Consumer<GenericMqttPublish> callback) {
client.toAsync().publishes(mqttGlobalPublishFilter, p -> callback.accept(GenericMqttPublish.ofMqtt3Publish(p)));
}
@Override
public void publish(final GenericMqttPublish publish) {
client.publish(publish.getAsMqtt3Publish());
}
@Override
public void subscribe(final GenericMqttSubscribe subscribe) {
client.subscribe(subscribe.getAsMqtt3Subscribe());
}
}
private static final class Mqtt5BlockingMqttClient implements GenericBlockingMqttClient {
private final Mqtt5BlockingClient client;
private Mqtt5BlockingMqttClient(final GenericBlockingMqttClientBuilder builder) {
var mqtt5ClientBuilder = MqttClient.builder()
.useMqttVersion5()
.serverHost(builder.host)
.serverPort(builder.port);
if (builder.clientId != null) {
mqtt5ClientBuilder = mqtt5ClientBuilder.identifier(builder.clientId);
}
client = mqtt5ClientBuilder.buildBlocking();
}
@Override
public void connect() {
client.connect();
}
@Override
public void connect(final GenericMqttConnect connect) {
client.connect(connect.getAsMqtt5Connect());
}
@Override
public void disconnect() {
client.disconnect();
}
@Override
public void cleanSession() {
client.connectWith().cleanStart(true).send();
client.disconnect();
}
@Override
public void setPublishesCallback(final MqttGlobalPublishFilter mqttGlobalPublishFilter, final Consumer<GenericMqttPublish> callback) {
client.toAsync().publishes(mqttGlobalPublishFilter, p -> callback.accept(GenericMqttPublish.ofMqtt5Publish(p)));
}
@Override
public void publish(final GenericMqttPublish publish) {
client.publish(publish.getAsMqtt5Publish());
}
@Override
public void subscribe(final GenericMqttSubscribe subscribe) {
client.subscribe(subscribe.getAsMqtt5Subscribe());
}
}
}
}