Search in sources :

Example 1 with MqttDeliveryToken

use of org.eclipse.paho.client.mqttv3.MqttDeliveryToken in project openhab1-addons by openhab.

the class MqttBrokerConnection method startProducer.

/**
     * Start a registered producer, so that it can start sending messages.
     * 
     * @param publisher
     *            to start.
     */
private void startProducer(MqttMessageProducer publisher) {
    logger.trace("Starting message producer for broker '{}'", name);
    publisher.setSenderChannel(new MqttSenderChannel() {

        @Override
        public void publish(String topic, byte[] payload) throws Exception {
            if (!started) {
                logger.warn("Broker connection not started. Cannot publish message to topic '{}'", topic);
                return;
            }
            // Create and configure a message
            MqttMessage message = new MqttMessage(payload);
            message.setQos(qos);
            message.setRetained(retain);
            // publish message asynchronously
            MqttTopic mqttTopic = client.getTopic(topic);
            MqttDeliveryToken deliveryToken = mqttTopic.publish(message);
            logger.debug("Publishing message {} to topic '{}'", deliveryToken.getMessageId(), topic);
            if (!async) {
                // wait for publish confirmation
                deliveryToken.waitForCompletion(10000);
                if (!deliveryToken.isComplete()) {
                    logger.error("Did not receive completion message within timeout limit whilst publishing to topic '{}'", topic);
                }
            }
        }
    });
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttTopic(org.eclipse.paho.client.mqttv3.MqttTopic) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MqttDeliveryToken(org.eclipse.paho.client.mqttv3.MqttDeliveryToken) MqttSenderChannel(org.openhab.io.transport.mqtt.MqttSenderChannel) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 2 with MqttDeliveryToken

use of org.eclipse.paho.client.mqttv3.MqttDeliveryToken in project spring-integration by spring-projects.

the class MqttAdapterTests method testOutboundOptionsApplied.

@Test
public void testOutboundOptionsApplied() throws Exception {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    factory.setCleanSession(false);
    factory.setConnectionTimeout(23);
    factory.setKeepAliveInterval(45);
    factory.setPassword("pass");
    MemoryPersistence persistence = new MemoryPersistence();
    factory.setPersistence(persistence);
    final SocketFactory socketFactory = mock(SocketFactory.class);
    factory.setSocketFactory(socketFactory);
    final Properties props = new Properties();
    factory.setSslProperties(props);
    factory.setUserName("user");
    Will will = new Will("foo", "bar".getBytes(), 2, true);
    factory.setWill(will);
    factory = spy(factory);
    final MqttAsyncClient client = mock(MqttAsyncClient.class);
    willAnswer(invocation -> client).given(factory).getAsyncClientInstance(anyString(), anyString());
    MqttPahoMessageHandler handler = new MqttPahoMessageHandler("foo", "bar", factory);
    handler.setDefaultTopic("mqtt-foo");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.start();
    final MqttToken token = mock(MqttToken.class);
    final AtomicBoolean connectCalled = new AtomicBoolean();
    willAnswer(invocation -> {
        MqttConnectOptions options = invocation.getArgument(0);
        assertEquals(23, options.getConnectionTimeout());
        assertEquals(45, options.getKeepAliveInterval());
        assertEquals("pass", new String(options.getPassword()));
        assertSame(socketFactory, options.getSocketFactory());
        assertSame(props, options.getSSLProperties());
        assertEquals("user", options.getUserName());
        assertEquals("foo", options.getWillDestination());
        assertEquals("bar", new String(options.getWillMessage().getPayload()));
        assertEquals(2, options.getWillMessage().getQos());
        connectCalled.set(true);
        return token;
    }).given(client).connect(any(MqttConnectOptions.class));
    willReturn(token).given(client).subscribe(any(String[].class), any(int[].class));
    final MqttDeliveryToken deliveryToken = mock(MqttDeliveryToken.class);
    final AtomicBoolean publishCalled = new AtomicBoolean();
    willAnswer(invocation -> {
        assertEquals("mqtt-foo", invocation.getArguments()[0]);
        MqttMessage message = invocation.getArgument(1);
        assertEquals("Hello, world!", new String(message.getPayload()));
        publishCalled.set(true);
        return deliveryToken;
    }).given(client).publish(anyString(), any(MqttMessage.class));
    handler.handleMessage(new GenericMessage<String>("Hello, world!"));
    verify(client, times(1)).connect(any(MqttConnectOptions.class));
    assertTrue(connectCalled.get());
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) SocketFactory(javax.net.SocketFactory) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) MqttToken(org.eclipse.paho.client.mqttv3.MqttToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Properties(java.util.Properties) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) MqttDeliveryToken(org.eclipse.paho.client.mqttv3.MqttDeliveryToken) Will(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will) Test(org.junit.Test)

Aggregations

MqttDeliveryToken (org.eclipse.paho.client.mqttv3.MqttDeliveryToken)2 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SocketFactory (javax.net.SocketFactory)1 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MqttToken (org.eclipse.paho.client.mqttv3.MqttToken)1 MqttTopic (org.eclipse.paho.client.mqttv3.MqttTopic)1 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 MqttSenderChannel (org.openhab.io.transport.mqtt.MqttSenderChannel)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 DefaultMqttPahoClientFactory (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory)1 Will (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will)1 MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)1