Search in sources :

Example 6 with DefaultPahoMessageConverter

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

Aggregations

DefaultPahoMessageConverter (org.springframework.integration.mqtt.support.DefaultPahoMessageConverter)6 MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)3 Test (org.junit.Test)2 Bean (org.springframework.context.annotation.Bean)2 BeanFactory (org.springframework.beans.factory.BeanFactory)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)1 EmbeddedJsonHeadersMessageMapper (org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1