use of com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient 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.Mqtt3AsyncClient in project open-smart-grid-platform by OSGP.
the class MqttClient method createClient.
private Mqtt3AsyncClient createClient(final MqttClientDefaults mqttClientDefaults, final MqttClientSslConfig mqttClientSslConfig, final String clientIdentifier) {
Mqtt3ClientBuilder clientBuilder = Mqtt3Client.builder().identifier(clientIdentifier).serverHost(mqttClientDefaults.getDefaultHost()).serverPort(mqttClientDefaults.getDefaultPort()).sslConfig(mqttClientSslConfig).automaticReconnectWithDefaultConfig().addConnectedListener(context -> LOGGER.info("{} client {} connected to broker at {}:{}", context.getClientConfig().getMqttVersion(), context.getClientConfig().getClientIdentifier().map(MqttClientIdentifier::toString).orElse(clientIdentifier), context.getClientConfig().getServerHost(), context.getClientConfig().getServerPort())).addDisconnectedListener(context -> LOGGER.info("{} client {} disconnected from broker at {}:{}", context.getClientConfig().getMqttVersion(), context.getClientConfig().getClientIdentifier().map(MqttClientIdentifier::toString).orElse(clientIdentifier), context.getClientConfig().getServerHost(), context.getClientConfig().getServerPort()));
if (StringUtils.isNotEmpty(mqttClientDefaults.getDefaultUsername())) {
LOGGER.debug("Using username/password for MQTT connection");
clientBuilder = clientBuilder.simpleAuth().username(mqttClientDefaults.getDefaultUsername()).password(mqttClientDefaults.getDefaultPassword().getBytes()).applySimpleAuth();
}
return clientBuilder.buildAsync();
}
Aggregations