Search in sources :

Example 1 with MqttClientIdProvider

use of io.joynr.messaging.mqtt.MqttClientIdProvider in project joynr by bmwcarit.

the class DefaultJoynrRuntimeFactoryTest method createFixture.

@SuppressWarnings("unchecked")
private void createFixture(Instance<Properties> joynrProperties, Instance<String> joynrLocalDomain) throws Exception {
    Instance<RawMessagingPreprocessor> rawMessageProcessor = mock(Instance.class);
    when(rawMessageProcessor.get()).thenReturn(new NoOpRawMessagingPreprocessor());
    BeanManager beanManager = mock(BeanManager.class);
    Bean<JoynrMessageProcessor> bean = mock(Bean.class);
    when(bean.create(Mockito.any())).thenReturn(new JoynrMessageProcessorTest());
    when(beanManager.getBeans(Mockito.<Type>eq(JoynrMessageProcessor.class), Mockito.<Annotation>any())).thenReturn(Sets.newHashSet(bean));
    final String mqttClientId = "someTestMqttClientId";
    MqttClientIdProvider mqttClientIdProvider = mock(MqttClientIdProvider.class);
    when(mqttClientIdProvider.getClientId()).thenReturn(mqttClientId);
    Instance<MqttClientIdProvider> mqttClientIdProviderInstance = mock(Instance.class);
    when(mqttClientIdProviderInstance.get()).thenReturn(mqttClientIdProvider);
    fixture = new DefaultJoynrRuntimeFactory(joynrProperties, joynrLocalDomain, rawMessageProcessor, mqttClientIdProviderInstance, beanManager, mock(StatusReceiver.class), mock(MqttStatusReceiver.class));
    scheduledExecutorService = mock(ScheduledExecutorService.class);
    Field executorField = DefaultJoynrRuntimeFactory.class.getDeclaredField("scheduledExecutorService");
    executorField.setAccessible(true);
    executorField.set(fixture, scheduledExecutorService);
}
Also used : NoOpRawMessagingPreprocessor(io.joynr.messaging.NoOpRawMessagingPreprocessor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MqttClientIdProvider(io.joynr.messaging.mqtt.MqttClientIdProvider) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor) NoOpRawMessagingPreprocessor(io.joynr.messaging.NoOpRawMessagingPreprocessor) RawMessagingPreprocessor(io.joynr.messaging.RawMessagingPreprocessor) Field(java.lang.reflect.Field) DefaultJoynrRuntimeFactory(io.joynr.jeeintegration.DefaultJoynrRuntimeFactory) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 2 with MqttClientIdProvider

use of io.joynr.messaging.mqtt.MqttClientIdProvider in project joynr by bmwcarit.

the class MqttPahoClientTest method mqttClientTestResubscriptionWithCleanRestartEnabled.

@Test
public void mqttClientTestResubscriptionWithCleanRestartEnabled() throws Exception {
    properties.put(MqttModule.PROPERTY_KEY_MQTT_BROKER_URI, "tcp://localhost:1883");
    injector = Guice.createInjector(new MqttPahoModule(), new JoynrPropertiesModule(properties), new AbstractModule() {

        @Override
        protected void configure() {
            bind(MessageRouter.class).toInstance(mockMessageRouter);
            bind(ScheduledExecutorService.class).annotatedWith(Names.named(MessageRouter.SCHEDULEDTHREADPOOL)).toInstance(Executors.newScheduledThreadPool(10));
            bind(RawMessagingPreprocessor.class).to(NoOpRawMessagingPreprocessor.class);
            Multibinder.newSetBinder(binder(), new TypeLiteral<JoynrMessageProcessor>() {
            });
        }
    });
    ownTopic = injector.getInstance((Key.get(MqttAddress.class, Names.named(MqttModule.PROPERTY_MQTT_GLOBAL_ADDRESS))));
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
    MqttClientIdProvider mqttClientIdProvider = injector.getInstance(MqttClientIdProvider.class);
    String clientId = mqttClientIdProvider.getClientId();
    String brokerUri = "tcp://localhost:1883";
    int reconnectSleepMs = 100;
    int keepAliveTimerSec = 60;
    int connectionTimeoutSec = 60;
    int timeToWaitMs = -1;
    int maxMsgsInflight = 100;
    int maxMsgSizeBytes = 0;
    boolean cleanSession = true;
    MqttClient mqttClient = new MqttClient(brokerUri, clientId, new MemoryPersistence(), scheduledExecutorService);
    joynrMqttClient = new MqttPahoClient(mqttClient, reconnectSleepMs, keepAliveTimerSec, connectionTimeoutSec, timeToWaitMs, maxMsgsInflight, maxMsgSizeBytes, cleanSession, "", "", "", "", mock(MqttStatusReceiver.class));
    joynrMqttClient.start();
    joynrMqttClient.setMessageListener(mockReceiver);
    joynrMqttClient.subscribe(ownTopic.getTopic());
    // manually call disconnect and connectionLost
    mqttClient.disconnect(500);
    MqttException exeption = new MqttException(MqttException.REASON_CODE_CLIENT_TIMEOUT);
    MqttPahoClient mqttPahoClient = (MqttPahoClient) joynrMqttClient;
    mqttPahoClient.connectionLost(exeption);
    joynrMqttClientPublishAndVerifyReceivedMessage(serializedMessage);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MessageRouter(io.joynr.messaging.routing.MessageRouter) MqttClientIdProvider(io.joynr.messaging.mqtt.MqttClientIdProvider) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor) NoOpRawMessagingPreprocessor(io.joynr.messaging.NoOpRawMessagingPreprocessor) RawMessagingPreprocessor(io.joynr.messaging.RawMessagingPreprocessor) AbstractModule(com.google.inject.AbstractModule) JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttAddress(joynr.system.RoutingTypes.MqttAddress) Test(org.junit.Test)

Aggregations

JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)2 NoOpRawMessagingPreprocessor (io.joynr.messaging.NoOpRawMessagingPreprocessor)2 RawMessagingPreprocessor (io.joynr.messaging.RawMessagingPreprocessor)2 MqttClientIdProvider (io.joynr.messaging.mqtt.MqttClientIdProvider)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AbstractModule (com.google.inject.AbstractModule)1 JoynrPropertiesModule (io.joynr.common.JoynrPropertiesModule)1 DefaultJoynrRuntimeFactory (io.joynr.jeeintegration.DefaultJoynrRuntimeFactory)1 JoynrMqttClient (io.joynr.messaging.mqtt.JoynrMqttClient)1 MessageRouter (io.joynr.messaging.routing.MessageRouter)1 Field (java.lang.reflect.Field)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 MqttAddress (joynr.system.RoutingTypes.MqttAddress)1 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)1 Test (org.junit.Test)1