Search in sources :

Example 11 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method testStopActionDefault.

@Test
public void testStopActionDefault() throws Exception {
    final IMqttClient client = mock(IMqttClient.class);
    MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, null);
    adapter.start();
    adapter.stop();
    verifyUnsubscribe(client);
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) Test(org.junit.Test)

Example 12 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method testStopActionAlways.

@Test
public void testStopActionAlways() throws Exception {
    final IMqttClient client = mock(IMqttClient.class);
    MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, false, ConsumerStopAction.UNSUBSCRIBE_ALWAYS);
    adapter.start();
    adapter.stop();
    verifyUnsubscribe(client);
    adapter.connectionLost(new RuntimeException("Intentional"));
    TaskScheduler taskScheduler = TestUtils.getPropertyValue(adapter, "taskScheduler", TaskScheduler.class);
    verify(taskScheduler, never()).schedule(any(Runnable.class), any(Date.class));
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) Date(java.util.Date) Test(org.junit.Test)

Example 13 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method testStopActionNever.

@Test
public void testStopActionNever() throws Exception {
    final IMqttClient client = mock(IMqttClient.class);
    MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
    adapter.start();
    adapter.stop();
    verifyNotUnsubscribe(client);
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) Test(org.junit.Test)

Example 14 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method testReconnect.

@Test
public void testReconnect() throws Exception {
    final IMqttClient client = mock(IMqttClient.class);
    MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
    adapter.setRecoveryInterval(10);
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    given(logger.isDebugEnabled()).willReturn(true);
    final AtomicInteger attemptingReconnectCount = new AtomicInteger();
    willAnswer(i -> {
        if (attemptingReconnectCount.getAndIncrement() == 0) {
            adapter.connectionLost(new RuntimeException("while schedule running"));
        }
        i.callRealMethod();
        return null;
    }).given(logger).debug("Attempting reconnect");
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.start();
    adapter.connectionLost(new RuntimeException("initial"));
    Thread.sleep(1000);
    // the following assertion should be equalTo, but leq to protect against a slow CI server
    assertThat(attemptingReconnectCount.get(), lessThanOrEqualTo(2));
    adapter.stop();
    taskScheduler.destroy();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) Log(org.apache.commons.logging.Log) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 15 with MqttPahoMessageDrivenChannelAdapter

use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter 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

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