Search in sources :

Example 1 with Mqtt3AsyncClient

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();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Mqtt3AsyncClient(com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient)

Example 2 with Mqtt3AsyncClient

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();
}
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

Mqtt3AsyncClient (com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient)2 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 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 CountDownLatch (java.util.concurrent.CountDownLatch)1 StringUtils (org.apache.commons.lang3.StringUtils)1 MqttClientDefaults (org.opensmartgridplatform.adapter.protocol.mqtt.domain.valueobjects.MqttClientDefaults)1