Search in sources :

Example 6 with MqttPahoMessageDrivenChannelAdapter

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

use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project sinsim by WilsonHu.

the class MqttService method inbound.

@Bean
public MessageProducer inbound() {
    String addressFormat = "tcp://%s:1883";
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(String.format(addressFormat, brokerHost), "server", "topic/client/send/#");
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    adapter.setOutputChannel(mqttInputChannel());
    return adapter;
}
Also used : DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) Bean(org.springframework.context.annotation.Bean)

Example 8 with MqttPahoMessageDrivenChannelAdapter

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

the class Application method mqttInbound.

@Bean
public MessageProducerSupport mqttInbound() {
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("siSampleConsumer", mqttClientFactory(), "siSampleTopic");
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    return adapter;
}
Also used : DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) Bean(org.springframework.context.annotation.Bean)

Example 9 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method testDifferentQos.

@Test
public void testDifferentQos() 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);
    MqttAsyncClient aClient = mock(MqttAsyncClient.class);
    final MqttClient client = mock(MqttClient.class);
    willAnswer(invocation -> client).given(factory).getClientInstance(anyString(), anyString());
    given(client.isConnected()).willReturn(true);
    new DirectFieldAccessor(client).setPropertyValue("aClient", aClient);
    willAnswer(new CallsRealMethods()).given(client).connect(any(MqttConnectOptions.class));
    willAnswer(new CallsRealMethods()).given(client).subscribe(any(String[].class), any(int[].class));
    willReturn(alwaysComplete).given(aClient).connect(any(MqttConnectOptions.class), any(), any());
    IMqttToken token = mock(IMqttToken.class);
    given(token.getGrantedQos()).willReturn(new int[] { 2, 0 });
    willReturn(token).given(aClient).subscribe(any(String[].class), any(int[].class), any(), any());
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("foo", "bar", factory, "baz", "fix");
    AtomicReference<Method> method = new AtomicReference<>();
    ReflectionUtils.doWithMethods(MqttPahoMessageDrivenChannelAdapter.class, m -> {
        m.setAccessible(true);
        method.set(m);
    }, m -> m.getName().equals("connectAndSubscribe"));
    assertNotNull(method.get());
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    given(logger.isWarnEnabled()).willReturn(true);
    method.get().invoke(adapter);
    verify(logger, atLeastOnce()).warn("Granted QOS different to Requested QOS; topics: [baz, fix] requested: [1, 1] granted: [2, 0]");
    verify(client).setTimeToWait(30_000L);
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Log(org.apache.commons.logging.Log) SocketFactory(javax.net.SocketFactory) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) Properties(java.util.Properties) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Will(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will) Test(org.junit.Test)

Example 10 with MqttPahoMessageDrivenChannelAdapter

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

the class MqttAdapterTests method buildAdapter.

private MqttPahoMessageDrivenChannelAdapter buildAdapter(final IMqttClient client, Boolean cleanSession, ConsumerStopAction action) throws MqttException {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory() {

        @Override
        public IMqttClient getClientInstance(String uri, String clientId) throws MqttException {
            return client;
        }
    };
    factory.setServerURIs("tcp://localhost:1883");
    if (cleanSession != null) {
        factory.setCleanSession(cleanSession);
    }
    if (action != null) {
        factory.setConsumerStopAction(action);
    }
    given(client.isConnected()).willReturn(true);
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("client", factory, "foo");
    adapter.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
    adapter.setOutputChannel(new NullChannel());
    adapter.setTaskScheduler(mock(TaskScheduler.class));
    adapter.afterPropertiesSet();
    return adapter;
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TaskScheduler(org.springframework.scheduling.TaskScheduler) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) NullChannel(org.springframework.integration.channel.NullChannel)

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