Search in sources :

Example 6 with IMqttClient

use of org.eclipse.paho.client.mqttv3.IMqttClient 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 7 with IMqttClient

use of org.eclipse.paho.client.mqttv3.IMqttClient 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 8 with IMqttClient

use of org.eclipse.paho.client.mqttv3.IMqttClient in project spring-integration by spring-projects.

the class BrokerRunning method apply.

@Override
public Statement apply(Statement base, Description description) {
    assumeTrue(brokerOnline.get(port));
    String url = "tcp://localhost:" + port;
    IMqttClient client = null;
    try {
        client = new DefaultMqttPahoClientFactory().getClientInstance(url, "junit-" + System.currentTimeMillis());
        client.connect();
    } catch (MqttException e) {
        logger.warn("Tests not running because no broker on " + url + ":", e);
        assumeNoException(e);
    } finally {
        if (client != null) {
            try {
                client.disconnect();
                client.close();
            } catch (MqttException e) {
            }
        }
    }
    return super.apply(base, description);
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient)

Aggregations

IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)8 Test (org.junit.Test)6 MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)6 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)3 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 DefaultMqttPahoClientFactory (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory)2 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SocketFactory (javax.net.SocketFactory)1 Log (org.apache.commons.logging.Log)1 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)1