Search in sources :

Example 16 with MqttPahoMessageDrivenChannelAdapter

use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testSingleTopic.

@Test
public void testSingleTopic() {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> out = outputChannel.receive(20000);
    assertNotNull(out);
    adapter.stop();
    inbound.stop();
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 17 with MqttPahoMessageDrivenChannelAdapter

use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testAsyncPersisted.

@Test
public void testAsyncPersisted() throws Exception {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    MqttClientPersistence persistence = new MqttDefaultFilePersistence(folder.getRoot().getAbsolutePath());
    factory.setPersistence(persistence);
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out", factory);
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.setAsync(true);
    adapter.setAsyncEvents(true);
    adapter.setDefaultQos(1);
    EventPublisher publisher1 = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher1);
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo", "mqtt-bar");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    Message<String> message1 = new GenericMessage<String>("foo");
    adapter.handleMessage(message1);
    verifyEvents(adapter, publisher1, message1);
    Message<String> message2 = MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build();
    EventPublisher publisher2 = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher2);
    adapter.handleMessage(message2);
    verifyEvents(adapter, publisher2, message2);
    verifyMessageIds(publisher1, publisher2);
    int clientInstance = publisher1.delivered.getClientInstance();
    adapter.stop();
    // new client instance
    adapter.start();
    publisher1 = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher1);
    adapter.handleMessage(message1);
    verifyEvents(adapter, publisher1, message1);
    publisher2 = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher2);
    adapter.handleMessage(message2);
    verifyEvents(adapter, publisher2, message2);
    verifyMessageIds(publisher1, publisher2);
    assertNotEquals(clientInstance, publisher1.delivered.getClientInstance());
    Message<?> out = null;
    for (int i = 0; i < 4; i++) {
        out = outputChannel.receive(20000);
        assertNotNull(out);
        if ("foo".equals(out.getPayload())) {
            assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
        } else if ("bar".equals(out.getPayload())) {
            assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
        } else {
            fail("unexpected payload " + out.getPayload());
        }
    }
    adapter.stop();
    inbound.stop();
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) QueueChannel(org.springframework.integration.channel.QueueChannel) MqttClientPersistence(org.eclipse.paho.client.mqttv3.MqttClientPersistence) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) GenericMessage(org.springframework.messaging.support.GenericMessage) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) MqttDefaultFilePersistence(org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence) Test(org.junit.Test)

Aggregations

MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)17 Test (org.junit.Test)14 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)10 IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)8 BeanFactory (org.springframework.beans.factory.BeanFactory)7 QueueChannel (org.springframework.integration.channel.QueueChannel)7 MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)6 DefaultMqttPahoClientFactory (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory)5 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)4 Properties (java.util.Properties)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SocketFactory (javax.net.SocketFactory)3 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)3 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 Will (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will)3 DefaultPahoMessageConverter (org.springframework.integration.mqtt.support.DefaultPahoMessageConverter)3 Method (java.lang.reflect.Method)2 Log (org.apache.commons.logging.Log)2 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)2