Search in sources :

Example 1 with Mqtt3ClientBuilder

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();
}
Also used : MqttClientState(com.hivemq.client.mqtt.MqttClientState) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) MqttClientSslConfig(com.hivemq.client.mqtt.MqttClientSslConfig) Mqtt3Client(com.hivemq.client.mqtt.mqtt3.Mqtt3Client) Mqtt3ConnAck(com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck) LoggerFactory(org.slf4j.LoggerFactory) MqttQos(com.hivemq.client.mqtt.datatypes.MqttQos) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) UnknownHostException(java.net.UnknownHostException) StringUtils(org.apache.commons.lang3.StringUtils) Mqtt3SubAck(com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck) InetAddress(java.net.InetAddress) MqttClientIdentifier(com.hivemq.client.mqtt.datatypes.MqttClientIdentifier) Mqtt3AsyncClient(com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient) MqttClientDefaults(org.opensmartgridplatform.adapter.protocol.mqtt.domain.valueobjects.MqttClientDefaults) Mqtt3ClientBuilder(com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder) Mqtt3Publish(com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish) Mqtt3ConnAckReturnCode(com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAckReturnCode) Mqtt3SubAckReturnCode(com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAckReturnCode) MqttClientIdentifier(com.hivemq.client.mqtt.datatypes.MqttClientIdentifier) Mqtt3ClientBuilder(com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder)

Aggregations

MqttClientSslConfig (com.hivemq.client.mqtt.MqttClientSslConfig)1 MqttClientState (com.hivemq.client.mqtt.MqttClientState)1 MqttClientIdentifier (com.hivemq.client.mqtt.datatypes.MqttClientIdentifier)1 MqttQos (com.hivemq.client.mqtt.datatypes.MqttQos)1 Mqtt3AsyncClient (com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient)1 Mqtt3Client (com.hivemq.client.mqtt.mqtt3.Mqtt3Client)1 Mqtt3ClientBuilder (com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder)1 Mqtt3ConnAck (com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck)1 Mqtt3ConnAckReturnCode (com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAckReturnCode)1 Mqtt3Publish (com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish)1 Mqtt3SubAck (com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck)1 Mqtt3SubAckReturnCode (com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAckReturnCode)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Arrays (java.util.Arrays)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 StringUtils (org.apache.commons.lang3.StringUtils)1 MqttClientDefaults (org.opensmartgridplatform.adapter.protocol.mqtt.domain.valueobjects.MqttClientDefaults)1 Logger (org.slf4j.Logger)1