Search in sources :

Example 1 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler in project sinsim by WilsonHu.

the class MqttService method mqttOutbound.

@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("server", mqttClientFactory());
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic("/topic/client/test");
    return messageHandler;
}
Also used : MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) Bean(org.springframework.context.annotation.Bean)

Example 2 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testAddRemoveTopic.

@Test
public void testAddRemoveTopic() {
    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");
    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();
    inbound.addTopic("mqtt-foo");
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.addTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("bar", out.getPayload());
    assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.removeTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(1);
    assertNull(out);
    try {
        inbound.addTopic("mqtt-foo");
        fail("Expected exception");
    } catch (MessagingException e) {
        assertEquals("Topic 'mqtt-foo' is already subscribed.", e.getMessage());
    }
    inbound.addTopic("mqqt-bar", "mqqt-baz");
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    inbound.addTopics(new String[] { "mqqt-bar", "mqqt-baz" }, new int[] { 0, 0 });
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    adapter.stop();
    inbound.stop();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 3 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testTwoTopics.

@Test
public void testTwoTopics() {
    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", "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();
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> message = MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build();
    adapter.handleMessage(message);
    Message<?> out = outputChannel.receive(20000);
    assertNotNull(out);
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    out = outputChannel.receive(20000);
    assertNotNull(out);
    inbound.stop();
    assertEquals("bar", out.getPayload());
    assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    adapter.stop();
}
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 4 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testAsync.

@Test
public void testAsync() throws Exception {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.setAsync(true);
    adapter.setAsyncEvents(true);
    EventPublisher publisher = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher);
    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();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    adapter.handleMessage(message);
    verifyEvents(adapter, publisher, message);
    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 : GenericMessage(org.springframework.messaging.support.GenericMessage) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) 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 5 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler in project spring-integration by spring-projects.

the class DownstreamExceptionTests method testNoErrorChannel.

@Test
public void testNoErrorChannel() throws Exception {
    service.n = 0;
    Log logger = spy(TestUtils.getPropertyValue(noErrorChannel, "logger", Log.class));
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Unhandled")) {
            latch.countDown();
        }
        return null;
    }).when(logger).error(anyString(), any(Throwable.class));
    new DirectFieldAccessor(noErrorChannel).setPropertyValue("logger", logger);
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-fooEx1");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    adapter.handleMessage(new GenericMessage<String>("foo"));
    service.barrier.await(10, TimeUnit.SECONDS);
    service.barrier.reset();
    adapter.handleMessage(new GenericMessage<String>("foo"));
    service.barrier.await(10, TimeUnit.SECONDS);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    verify(logger).error(contains("Unhandled exception for"), any(Throwable.class));
    service.barrier.reset();
    adapter.stop();
}
Also used : Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)12 Test (org.junit.Test)10 BeanFactory (org.springframework.beans.factory.BeanFactory)9 QueueChannel (org.springframework.integration.channel.QueueChannel)6 MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)6 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 GenericMessage (org.springframework.messaging.support.GenericMessage)3 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 Bean (org.springframework.context.annotation.Bean)2 DefaultMqttPahoClientFactory (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory)2 DefaultPahoMessageConverter (org.springframework.integration.mqtt.support.DefaultPahoMessageConverter)2 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SocketFactory (javax.net.SocketFactory)1 Log (org.apache.commons.logging.Log)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)1 MqttClientPersistence (org.eclipse.paho.client.mqttv3.MqttClientPersistence)1