use of com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish in project open-smart-grid-platform by OSGP.
the class MqttClientIT method publishMessages.
void publishMessages(final PublishedMessage... publishedMessages) throws Exception {
final Mqtt3AsyncClient mqtt3AsyncClient = Mqtt3Client.builder().identifier("async-publishing-client").serverHost("localhost").serverPort(containerMqttPort).automaticReconnectWithDefaultConfig().buildAsync();
final CountDownLatch connectLatch = new CountDownLatch(1);
mqtt3AsyncClient.connect().whenComplete((ack, t) -> {
connectLatch.countDown();
});
connectLatch.await();
try {
final CountDownLatch publishLatch = new CountDownLatch(publishedMessages.length);
for (final PublishedMessage publishedMessage : publishedMessages) {
mqtt3AsyncClient.publishWith().topic(publishedMessage.topic()).qos(MqttQos.AT_LEAST_ONCE).payload(publishedMessage.payload()).retain(false).send().whenComplete((mqtt3Publish, t) -> {
publishLatch.countDown();
});
}
publishLatch.await();
} finally {
mqtt3AsyncClient.disconnect();
}
}
use of com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish in project openhab-core by openhab.
the class Mqtt3AsyncClientWrapper method connect.
@Override
public CompletableFuture<?> connect(@Nullable MqttWillAndTestament lwt, int keepAliveInterval, @Nullable String username, @Nullable String password) {
Mqtt3ConnectBuilder connectMessageBuilder = Mqtt3Connect.builder().keepAlive(keepAliveInterval);
if (lwt != null) {
Mqtt3Publish willPublish = Mqtt3Publish.builder().topic(lwt.getTopic()).payload(lwt.getPayload()).retain(lwt.isRetain()).qos(getMqttQosFromInt(lwt.getQos())).build();
connectMessageBuilder.willPublish(willPublish);
}
if (username != null && !username.isBlank() && password != null && !password.isBlank()) {
connectMessageBuilder.simpleAuth().username(username).password(password.getBytes()).applySimpleAuth();
}
return client.connect(connectMessageBuilder.build());
}
use of com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish in project openhab-core by openhab.
the class MqttBrokerConnectionTests method subscribeToWildcardTopic.
@Test
public void subscribeToWildcardTopic() throws ConfigurationException, MqttException, InterruptedException, ExecutionException, TimeoutException {
MqttBrokerConnectionEx connection = new MqttBrokerConnectionEx("123.123.123.123", null, false, false, "MqttBrokerConnectionTests");
// Add a subscriber while still offline
MqttMessageSubscriber subscriber = mock(MqttMessageSubscriber.class);
connection.subscribe("homie/device123/+", subscriber);
MqttMessageSubscriber subscriber2 = mock(MqttMessageSubscriber.class);
connection.subscribe("#", subscriber2);
MqttMessageSubscriber subscriber3 = mock(MqttMessageSubscriber.class);
connection.subscribe("homie/#", subscriber3);
assertTrue(connection.start().get(200, TimeUnit.MILLISECONDS));
assertTrue(connection.hasSubscribers());
assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED));
Mqtt3Publish publishMessage = Mqtt3Publish.builder().topic("homie/device123/$name").payload(HELLO_BYTES).build();
connection.getSubscribers().get("homie/device123/+").messageArrived(publishMessage);
connection.getSubscribers().get("#").messageArrived(publishMessage);
connection.getSubscribers().get("homie/#").messageArrived(publishMessage);
verify(subscriber).processMessage(eq("homie/device123/$name"), eqHelloBytes());
verify(subscriber2).processMessage(eq("homie/device123/$name"), eqHelloBytes());
verify(subscriber3).processMessage(eq("homie/device123/$name"), eqHelloBytes());
}
use of com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish in project openhab-core by openhab.
the class MqttBrokerConnectionTests method subscribeBeforeOnlineThenConnect.
@Test
public void subscribeBeforeOnlineThenConnect() throws ConfigurationException, MqttException, InterruptedException, ExecutionException, TimeoutException {
MqttBrokerConnectionEx connection = new MqttBrokerConnectionEx("123.123.123.123", null, false, false, "MqttBrokerConnectionTests");
// Add a subscriber while still offline
MqttMessageSubscriber subscriber = mock(MqttMessageSubscriber.class);
connection.subscribe("homie/device123/$name", subscriber);
assertTrue(connection.start().get(200, TimeUnit.MILLISECONDS));
assertTrue(connection.hasSubscribers());
assertThat(connection.connectionState(), is(MqttConnectionState.CONNECTED));
Mqtt3Publish publishMessage = Mqtt3Publish.builder().topic("homie/device123/$name").payload(HELLO_BYTES).build();
// Test if subscription is active
connection.getSubscribers().get("homie/device123/$name").messageArrived(publishMessage);
verify(subscriber).processMessage(eq("homie/device123/$name"), eqHelloBytes());
}
Aggregations