org.eclipse.paho.client.mqttv3.IMqttAsyncClient Java Examples

The following examples show how to use org.eclipse.paho.client.mqttv3.IMqttAsyncClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectFactoryTest.java    From rxmqtt with Apache License 2.0 7 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final MqttConnectOptions options = Mockito
            .mock(MqttConnectOptions.class);
    final ConnectFactory factory = new ConnectFactory(client, options);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final Completable obs = factory.create();
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).connect(Matchers.same(options),
            Matchers.isNull(), actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof ConnectFactory.ConnectActionListener);
}
 
Example #2
Source File: PahoObservableMqttClientTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
private Builder builderWithMocks(final String expectedClientId) {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.getClientId()).thenReturn(expectedClientId);
    final CloseFactory closeFactory = Mockito.mock(CloseFactory.class);
    final ConnectFactory connectFactory = Mockito
            .mock(ConnectFactory.class);
    final DisconnectFactory disconnectFactory = Mockito
            .mock(DisconnectFactory.class);
    final PublishFactory publishFactory = Mockito
            .mock(PublishFactory.class);
    final SubscribeFactory subscribeFactory = Mockito
            .mock(SubscribeFactory.class);
    final UnsubscribeFactory unsubscribeFactory = Mockito
            .mock(UnsubscribeFactory.class);
    return new PahoObservableMqttClient.Builder(client)
            .setCloseFactory(closeFactory).setConnectFactory(connectFactory)
            .setDisconnectFactory(disconnectFactory)
            .setPublishFactory(publishFactory)
            .setSubscribeFactory(subscribeFactory)
            .setUnsubscribeFactory(unsubscribeFactory);
}
 
Example #3
Source File: Mqttv3ClientImpl.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Stop
public void stop() {
    LOG.info("Mqttv3Client stoping!");

    if (clnt != null) {
        for (Entry<String, ObjectPool<IMqttAsyncClient>> entry : clnt.entrySet()) {
            try {
                entry.getValue().close();
            } catch (Exception e) {
                LOG.error(e.getMessage(), e.fillInStackTrace());
            }
        }
        clnt = null;
    }

    LOG.info("Mqttv3Client finish stoping!");
}
 
Example #4
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final SubscribeFactory factory = new SubscribeFactory(client);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor
            .forClass(IMqttMessageListener[].class);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = new int[] { 1, 2 };
    final Flowable<SubscribeMessage> obs = factory.create(topics, qos,
            BackpressureStrategy.ERROR);
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).subscribe(Matchers.same(topics),
            Matchers.same(qos), Matchers.isNull(), actionListener.capture(),
            messageListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof SubscribeFactory.SubscribeActionListener);
    Assert.assertTrue(messageListener
            .getValue() instanceof SubscriberMqttMessageListener[]);
    Assert.assertEquals(2, messageListener.getValue().length);
}
 
Example #5
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);
    final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor
            .forClass(IMqttMessageListener[].class);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = new int[] { 1, 2 };
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.subscribe(Matchers.same(topics), Matchers.same(qos),
            Matchers.isNull(), actionListener.capture(),
            messageListener.capture()))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final SubscribeFactory factory = new SubscribeFactory(client);
    final Flowable<SubscribeMessage> obs = factory.create(topics, qos,
            BackpressureStrategy.ERROR);
    obs.blockingFirst();
}
 
Example #6
Source File: DisconnectFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final DisconnectFactory factory = new DisconnectFactory(client);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Completable obs = factory.create();

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).disconnect(Matchers.isNull(),
            actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof DisconnectFactory.DisconnectActionListener);
}
 
Example #7
Source File: UnsubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.unsubscribe(Matchers.any(String[].class),
            Matchers.isNull(),
            Matchers.any(
                    UnsubscribeFactory.UnsubscribeActionListener.class)))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final UnsubscribeFactory factory = new UnsubscribeFactory(client);
    final Completable obs = factory
            .create(new String[] { "topic1", "topic2" });
    obs.blockingAwait();
}
 
Example #8
Source File: UnsubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final UnsubscribeFactory factory = new UnsubscribeFactory(client);
    final String[] topics = new String[] { "topic1", "topic2" };
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Completable obs = factory.create(topics);

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).unsubscribe(Matchers.same(topics),
            Matchers.isNull(), actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof UnsubscribeFactory.UnsubscribeActionListener);
}
 
Example #9
Source File: PublishFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.publish(Matchers.any(String.class),
            Matchers.any(byte[].class), Matchers.any(int.class),
            Matchers.any(boolean.class), Matchers.isNull(),
            Matchers.any(PublishFactory.PublishActionListener.class)))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final PublishFactory factory = new PublishFactory(client);
    final Single<PublishToken> obs = factory.create("topic1",
            Mockito.mock(MqttMessage.class));
    obs.blockingGet();
}
 
Example #10
Source File: PublishFactoryTest.java    From rxmqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final PublishFactory factory = new PublishFactory(client);
    final String topic = "topic1";
    final PublishMessage msg = PublishMessage
            .create(new byte[] { 'a', 'b', 'c' }, 1, true);
    final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor
            .forClass(IMqttActionListener.class);

    // When
    final Single<PublishToken> obs = factory.create(topic, msg);

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).publish(Matchers.same(topic),
            Matchers.same(msg.getPayload()), Matchers.anyInt(),
            Matchers.anyBoolean(), Matchers.any(),
            actionListener.capture());
    Assert.assertTrue(actionListener
            .getValue() instanceof PublishFactory.PublishActionListener);
}
 
Example #11
Source File: CloseFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenCreateIsCalledThenAnObservableIsReturned()
        throws Exception {
    // Given
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final CloseFactory factory = new CloseFactory(client);

    // When
    final Completable obs = factory.create();

    // Then
    Assert.assertNotNull(obs);
    obs.subscribe();
    Mockito.verify(client).close();
}
 
Example #12
Source File: ConnectFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final MqttConnectOptions options = Mockito
            .mock(MqttConnectOptions.class);
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.connect(Matchers.same(options), Matchers.isNull(),
            Matchers.any(ConnectFactory.ConnectActionListener.class)))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final ConnectFactory factory = new ConnectFactory(client, options);
    final Completable obs = factory.create();
    obs.blockingAwait();
}
 
Example #13
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void whenANullTopicsIsSuppliedThenAnExceptionIsThrown() {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final SubscribeFactory factory = new SubscribeFactory(client);
    final String[] topics = null;
    final int[] qos = new int[] { 1, 2 };
    factory.create(topics, qos, BackpressureStrategy.ERROR);
}
 
Example #14
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void whenANullQoSIsSuppliedThenAnExceptionIsThrown() {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final SubscribeFactory factory = new SubscribeFactory(client);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = null;
    factory.create(topics, qos, BackpressureStrategy.ERROR);
}
 
Example #15
Source File: SubscribeFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void whenANullBackpressureStrategyIsSuppliedThenAnExceptionIsThrown() {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    final SubscribeFactory factory = new SubscribeFactory(client);
    final String[] topics = new String[] { "topic1", "topic2" };
    final int[] qos = new int[] { 1, 2 };
    factory.create(topics, qos, null);
}
 
Example #16
Source File: PublishFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenOnSuccessIsCalledThenObserverOnNextAndOnCompletedAreCalled()
        throws Exception {
    @SuppressWarnings("unchecked")
    final SingleEmitter<MqttToken> observer = Mockito
            .mock(SingleEmitter.class);
    final PublishActionListener listener = new PublishFactory.PublishActionListener(
            observer);
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.getClientId()).thenReturn("client_id");
    final IMqttToken iMqttDeliveryToken = Mockito.mock(IMqttToken.class);
    Mockito.when(iMqttDeliveryToken.getClient()).thenReturn(client);
    Mockito.when(iMqttDeliveryToken.getMessageId()).thenReturn(123);
    Mockito.when(iMqttDeliveryToken.getSessionPresent()).thenReturn(false);
    Mockito.when(iMqttDeliveryToken.getGrantedQos()).thenReturn(new int[0]);
    Mockito.when(iMqttDeliveryToken.getTopics())
            .thenReturn(new String[] { "topic" });
    final ArgumentCaptor<MqttToken> publishToken = ArgumentCaptor
            .forClass(MqttToken.class);
    listener.onSuccess(iMqttDeliveryToken);
    Mockito.verify(observer).onSuccess(publishToken.capture());
    Assert.assertNotNull(iMqttDeliveryToken);
    Assert.assertNotNull(publishToken);
    Assert.assertNotNull(publishToken.getValue().getClientId());
    Assert.assertEquals(iMqttDeliveryToken.getClient().getClientId(),
            publishToken.getValue().getClientId());
    Assert.assertNotNull(publishToken.getValue().getMessageId());
    Assert.assertEquals(iMqttDeliveryToken.getMessageId(),
            publishToken.getValue().getMessageId());
    Assert.assertNotNull(publishToken.getValue().getTopics());
    Assert.assertArrayEquals(iMqttDeliveryToken.getTopics(),
            publishToken.getValue().getTopics());
    Assert.assertNotNull(publishToken.getValue().getMessageId());
    Assert.assertEquals(iMqttDeliveryToken.getMessageId(),
            publishToken.getValue().getMessageId());
}
 
Example #17
Source File: Mqttv3ClientImpl.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Override
public void returnMqttClient(String id, IMqttAsyncClient mqttClient) {
    try {
        clnt.get(id).returnObject(mqttClient);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e.fillInStackTrace());
    }
}
 
Example #18
Source File: CloseFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.doThrow(
            new MqttException(MqttException.REASON_CODE_CLIENT_CONNECTED))
            .when(client).close();
    final CloseFactory factory = new CloseFactory(client);
    final Completable obs = factory.create();
    obs.blockingAwait();
}
 
Example #19
Source File: DisconnectFactoryTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled()
        throws Throwable {
    this.expectedException.expectCause(isA(MqttException.class));
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.disconnect(Matchers.isNull(),
            Matchers.any(DisconnectFactory.DisconnectActionListener.class)))
            .thenThrow(new MqttException(
                    MqttException.REASON_CODE_CLIENT_CONNECTED));
    final DisconnectFactory factory = new DisconnectFactory(client);
    final Completable obs = factory.create();
    obs.blockingAwait();
}
 
Example #20
Source File: MqttAsyncClientFactory.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Override
public IMqttAsyncClient create() throws Exception {
    String broker = conf.getConf(id, MqttClientConf.F_mqtt_broker);
    MqttAsyncClient mqttClient = new MqttAsyncClient(broker, createClientId(), createPersistence());
    mqttClient.connect(createConnectOptions()).waitForCompletion();
    return mqttClient;
}
 
Example #21
Source File: MqttAsyncClientFactory.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<IMqttAsyncClient> p) throws Exception {
    IMqttAsyncClient client = p.getObject();
    try {
        client.disconnect();
    } finally {
        client.close();
    }
}
 
Example #22
Source File: Mqttv3ClientImpl.java    From jframe with Apache License 2.0 5 votes vote down vote up
public void init(InputStream file) throws Exception {
    MqttClientConf props = new MqttClientConf();
    props.init(file);

    String[] ids = props.getGroupIds();
    clnt = new HashMap<>(ids.length, 1);
    for (String id : ids) {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxTotal(props.getConfInt(id, MqttClientConf.F_pool_maxTotal, "100"));
        config.setMaxIdle(props.getConfInt(id, MqttClientConf.F_pool_maxIdle, "10"));
        config.setMinIdle(props.getConfInt(id, MqttClientConf.F_pool_minIdle, "1"));
        clnt.put(id, new GenericObjectPool<IMqttAsyncClient>(new MqttAsyncClientFactory(id, props), config));
    }
}
 
Example #23
Source File: Mqttv3ClientImpl.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Override
public IMqttAsyncClient borrowMqttClient(String id) {
    try {
        return clnt.get(id).borrowObject();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e.fillInStackTrace());
    }
    return null;
}
 
Example #24
Source File: PahoObservableMqttClientTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenGetBrokerUriIsCalledItReturnsPahoServerUrl() {
    final String expectedBrokerUri = "brokerUri";
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.getServerURI()).thenReturn(expectedBrokerUri);
    final Builder builder = new PahoObservableMqttClient.Builder(client);
    final PahoObservableMqttClient target = builder.build();
    Assert.assertEquals(expectedBrokerUri, target.getBrokerUri());
}
 
Example #25
Source File: PahoObservableMqttClientTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenGetClientIdIsCalledItReturnsPahoClientId() {
    final String expectedClientId = "clientId";
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.getClientId()).thenReturn(expectedClientId);
    final Builder builder = new PahoObservableMqttClient.Builder(client);
    final PahoObservableMqttClient target = builder.build();
    Assert.assertEquals(expectedClientId, target.getClientId());
}
 
Example #26
Source File: PahoObservableMqttClientTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenAValidBackpressureStrategyThenTheAccessorReturnsIt()
        throws MqttException {
    final BackpressureStrategy expected = BackpressureStrategy.BUFFER;
    final Builder builder = PahoObservableMqttClient
            .builder(Mockito.mock(IMqttAsyncClient.class))
            .setBackpressureStrategy(expected);
    Assert.assertNotNull(builder);
    Assert.assertNotNull(builder.getBackpressureStrategy());
    Assert.assertEquals(expected, builder.getBackpressureStrategy());
}
 
Example #27
Source File: PahoObservableMqttClientTest.java    From rxmqtt with Apache License 2.0 5 votes vote down vote up
@Test
public void whenThePahoClientIsConnectedIsConnectedReturnsTrue() {
    final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
    Mockito.when(client.isConnected()).thenReturn(true);
    final Builder builder = new PahoObservableMqttClient.Builder(client);
    final PahoObservableMqttClient target = builder.build();
    Assert.assertEquals(true, target.isConnected());
}
 
Example #28
Source File: Factory.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
public IMqttAsyncClient getMqttAsyncClient (String protocol, String mqttUrl, String clientID, MemoryPersistence persistence) throws MqttException {
	if(protocol.equals("tcp"))
		return new MqttAsyncClient(mqttUrl, clientID, persistence);
	else if (protocol.equals("ws") || protocol.equals("wss"))
		return new MqttWebSocketAsyncClient (mqttUrl, clientID, persistence);
	else
		return null;
}
 
Example #29
Source File: MqttCallBackImpl.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
public MqttCallBackImpl (IMqttAsyncClient  client,String clientID,String logLevel,String encoding) {
	super(encoding);    	
   	this.clientID = clientID;
	this.logLevel = logLevel;
	this.client = client;
	this.encoding = encoding;
}
 
Example #30
Source File: MqttCallBackImplTest.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
@BeforeClass
  public static void setUpClass()
          throws Exception {
clientMock = Mockito.mock(IMqttAsyncClient.class);
messageMock = Mockito.mock(MqttMessage.class);
tokenMock = Mockito.mock(IMqttDeliveryToken.class);
causeMock = Mockito.mock(Throwable.class);
  }