Example usage for org.springframework.integration.mqtt.outbound MqttPahoMessageHandler setDefaultTopic

List of usage examples for org.springframework.integration.mqtt.outbound MqttPahoMessageHandler setDefaultTopic

Introduction

In this page you can find the example usage for org.springframework.integration.mqtt.outbound MqttPahoMessageHandler setDefaultTopic.

Prototype

public void setDefaultTopic(String defaultTopic) 

Source Link

Document

Set the topic to which the message will be published if the #setTopicExpression(Expression) topicExpression evaluates to `null`.

Usage

From source file:org.springframework.integration.mqtt.DownstreamExceptionTests.java

@Test
public void testNoErrorChannel() throws Exception {
    service.n = 0;/*  ww  w. j a  va2 s . co  m*/
    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();
}

From source file:org.springframework.integration.mqtt.DownstreamExceptionTests.java

@Test
public void testWithErrorChannel() throws Exception {
    assertSame(this.errors, TestUtils.getPropertyValue(this.withErrorChannel, "errorChannel"));
    service.n = 0;/*from   w  w  w  .  j a v  a  2 s.  co m*/
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-fooEx2");
    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);
    assertNotNull(errors.receive(10000));
    service.barrier.reset();
    adapter.stop();
}

From source file:org.springframework.integration.mqtt.MqttAdapterTests.java

@Test
public void testOutboundOptionsApplied() throws Exception {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    factory.setCleanSession(false);//from   ww w  .ja v a  2 s.  c  o m
    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());
}

From source file:org.springframework.integration.samples.mqtt.Application.java

@Bean
public MessageHandler mqttOutbound() {
    MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("siSamplePublisher",
            mqttClientFactory());/*  w  ww  . j  av a  2  s.com*/
    messageHandler.setAsync(true);
    messageHandler.setDefaultTopic("siSampleTopic");
    return messageHandler;
}