use of com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder 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