Search in sources :

Example 1 with Mqtt3Publish

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

Example 2 with Mqtt3Publish

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());
}
Also used : Mqtt3ConnectBuilder(com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder) Mqtt3Publish(com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish)

Example 3 with Mqtt3Publish

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());
}
Also used : Mqtt3Publish(com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Example 4 with Mqtt3Publish

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());
}
Also used : Mqtt3Publish(com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Aggregations

Mqtt3Publish (com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish)3 Test (org.junit.jupiter.api.Test)2 JavaTest (org.openhab.core.test.java.JavaTest)2 Mqtt3AsyncClient (com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient)1 Mqtt3ConnectBuilder (com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder)1 CountDownLatch (java.util.concurrent.CountDownLatch)1