Search in sources :

Example 6 with MqttPahoMessageHandler

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

the class Application method mqttOutbound.

@Bean
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("siSamplePublisher", mqttClientFactory());
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic("siSampleTopic");
    return messageHandler;
}
Also used : MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) Bean(org.springframework.context.annotation.Bean)

Example 7 with MqttPahoMessageHandler

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

the class MqttAdapterTests method testCustomExpressions.

@SuppressWarnings("unchecked")
@Test
public void testCustomExpressions() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    MqttPahoMessageHandler handler = ctx.getBean("handler", MqttPahoMessageHandler.class);
    GenericMessage<String> message = new GenericMessage<>("foo");
    assertEquals("fooTopic", TestUtils.getPropertyValue(handler, "topicProcessor", MessageProcessor.class).processMessage(message));
    assertEquals(1, TestUtils.getPropertyValue(handler, "converter.qosProcessor", MessageProcessor.class).processMessage(message));
    assertEquals(Boolean.TRUE, TestUtils.getPropertyValue(handler, "converter.retainedProcessor", MessageProcessor.class).processMessage(message));
    handler = ctx.getBean("handlerWithNullExpressions", MqttPahoMessageHandler.class);
    assertEquals(1, TestUtils.getPropertyValue(handler, "converter", DefaultPahoMessageConverter.class).fromMessage(message, null).getQos());
    assertEquals(Boolean.TRUE, TestUtils.getPropertyValue(handler, "converter", DefaultPahoMessageConverter.class).fromMessage(message, null).isRetained());
    ctx.close();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 8 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler 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)

Example 9 with MqttPahoMessageHandler

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

the class BackToBackAdapterTests method testJson.

@Test
public void testJson() {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper(JacksonJsonUtils.messagingAwareMapper("org.springframework"));
    DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
    converter.setBytesMessageMapper(mapper);
    adapter.setConverter(converter);
    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.setConverter(converter);
    inbound.afterPropertiesSet();
    inbound.start();
    adapter.handleMessage(new GenericMessage<Foo>(new Foo("bar"), Collections.singletonMap("baz", "qux")));
    Message<?> out = outputChannel.receive(20000);
    assertNotNull(out);
    adapter.stop();
    inbound.stop();
    assertEquals(new Foo("bar"), out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    assertEquals("qux", out.getHeaders().get("baz"));
}
Also used : DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) EmbeddedJsonHeadersMessageMapper(org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 10 with MqttPahoMessageHandler

use of org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler 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)

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