Search in sources :

Example 1 with MqttClient

use of org.eclipse.kapua.transport.mqtt.MqttClient in project kapua by eclipse.

the class PooledMqttClientFactory method create.

/**
 * Creates the {@link MqttClient} for the {@link MqttClientPool}.
 *
 * <p>
 * The client is initialized and connected. In case of any failure on connect operation, an exception is thrown and the the created client is destroyed.
 * </p>
 *
 * @throws Exception
 *             FIXME [javadoc] document exception.
 * @since 1.0.0
 */
@Override
public MqttClient create() throws Exception {
    // 
    // User pwd generation
    MqttClientSetting mqttClientSettings = MqttClientSetting.getInstance();
    MqttClientPoolSetting mqttClientPoolSettings = MqttClientPoolSetting.getInstance();
    String username = mqttClientSettings.getString(MqttClientSettingKeys.TRANSPORT_CREDENTIAL_USERNAME);
    char[] password = mqttClientSettings.getString(MqttClientSettingKeys.TRANSPORT_CREDENTIAL_PASSWORD).toCharArray();
    String clientId = ClientIdGenerator.getInstance().next(mqttClientPoolSettings.getString(MqttClientPoolSettingKeys.CLIENT_POOL_CLIENT_ID_PREFIX));
    URI brokerURI = SystemUtils.getBrokerURI();
    // 
    // Get new client and connection options
    MqttClientConnectionOptions connectionOptions = new MqttClientConnectionOptions();
    connectionOptions.setClientId(clientId);
    connectionOptions.setUsername(username);
    connectionOptions.setPassword(password);
    connectionOptions.setEndpointURI(brokerURI);
    // 
    // Connect client
    MqttClient kapuaClient = new MqttClient();
    try {
        kapuaClient.connectClient(connectionOptions);
    } catch (KapuaException ke) {
        kapuaClient.terminateClient();
        throw ke;
    }
    return kapuaClient;
}
Also used : MqttClientSetting(org.eclipse.kapua.transport.mqtt.setting.MqttClientSetting) MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) MqttClientPoolSetting(org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting) KapuaException(org.eclipse.kapua.KapuaException) MqttClientConnectionOptions(org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions) URI(java.net.URI)

Example 2 with MqttClient

use of org.eclipse.kapua.transport.mqtt.MqttClient in project kapua by eclipse.

the class MqttClientPoolTest method testPoolBorrowMax.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testPoolBorrowMax() throws Exception {
    // 
    // Get pool
    MqttClientPool transportPool = MqttClientPool.getInstance();
    // 
    // Test max borrow clients
    MqttClientPoolSetting setting = MqttClientPoolSetting.getInstance();
    long maxClients = setting.getLong(MqttClientPoolSettingKeys.CLIENT_POOL_SIZE_TOTAL_MAX);
    List<MqttClient> mqttClients = new ArrayList<>();
    for (int i = 0; i < maxClients; i++) {
        mqttClients.add(transportPool.borrowObject());
    }
    // Verify borrowed clients
    for (MqttClient t : mqttClients) {
        assertNotNull("mqttClient", t);
        assertTrue("mqttClient.isConnected", t.isConnected());
    }
    assertEquals("numActiveClients", maxClients, transportPool.getNumActive());
    // Ask one more transport client
    try {
        mqttClients.add(transportPool.borrowObject());
        fail("Should have thrown " + NoSuchElementException.class.getName() + " exception");
    } catch (NoSuchElementException nsee) {
    // All ok
    } catch (Exception e) {
        assertEquals("Should have thrown " + NoSuchElementException.class.getName() + " exception", NoSuchElementException.class, e.getClass());
    }
    // 
    // Return clients
    Iterator<MqttClient> transportClientIterator = mqttClients.iterator();
    while (transportClientIterator.hasNext()) {
        transportPool.returnObject(transportClientIterator.next());
    }
    assertEquals("numActiveClients", 0, transportPool.getNumActive());
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) ArrayList(java.util.ArrayList) MqttClientPoolSetting(org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting) MqttClientPool(org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with MqttClient

use of org.eclipse.kapua.transport.mqtt.MqttClient in project kapua by eclipse.

the class MqttClientTest method testMqttClientPublish.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testMqttClientPublish() throws Exception {
    MqttClientConnectionOptions clientConnectOptions = new MqttClientConnectionOptions();
    clientConnectOptions.setClientId(ClientIdGenerator.getInstance().next(MqttClientTest.class.getSimpleName()));
    clientConnectOptions.setUsername(username);
    clientConnectOptions.setPassword(password.toCharArray());
    clientConnectOptions.setEndpointURI(SystemUtils.getBrokerURI());
    // 
    // Connect
    MqttClient mqttClient = new MqttClient();
    try {
        mqttClient.connectClient(clientConnectOptions);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertTrue("client.connected", mqttClient.isConnected());
    // 
    // Send
    String sendTopic = "$EDC/kapua-sys/" + mqttClient.getClientId() + "/" + MqttClientTest.class.getSimpleName() + "/testMqttClientSendTopic";
    MqttTopic mqttTopic = new MqttTopic(sendTopic);
    MqttPayload mqttPayload = new MqttPayload("testMqttClientSendPayload".getBytes());
    MqttMessage mqttMessage = new MqttMessage(mqttTopic, new Date(), mqttPayload);
    try {
        mqttClient.publish(mqttMessage);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Disconnect
    try {
        mqttClient.disconnectClient();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertFalse("client.connected", mqttClient.isConnected());
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) MqttClientConnectionOptions(org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions) Date(java.util.Date) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with MqttClient

use of org.eclipse.kapua.transport.mqtt.MqttClient in project kapua by eclipse.

the class PooledMqttClientFactory method destroyObject.

/**
 * Destroys the given {@link MqttClient} pooled object.
 * <p>
 * Before calling super implementation {@link BasePooledObjectFactory#destroyObject(PooledObject)} it tries to clean up the {@link MqttClient}.
 * </p>
 *
 * @param pooledMqttClient
 *            The pooled object to destroy.
 *
 * @since 1.0.0.
 */
@Override
public void destroyObject(PooledObject<MqttClient> pooledMqttClient) throws Exception {
    MqttClient mqttClient = pooledMqttClient.getObject();
    if (mqttClient != null) {
        if (mqttClient.isConnected()) {
            mqttClient.disconnectClient();
        }
        mqttClient.terminateClient();
    }
    super.destroyObject(pooledMqttClient);
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient)

Example 5 with MqttClient

use of org.eclipse.kapua.transport.mqtt.MqttClient in project kapua by eclipse.

the class MqttClientPoolTest method testPoolBorrow.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testPoolBorrow() throws Exception {
    // 
    // Get pool
    MqttClientPool transportPool = MqttClientPool.getInstance();
    // 
    // Borrow client
    MqttClient mqttClient = null;
    try {
        mqttClient = transportPool.borrowObject();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // 
    // Verify client
    assertNotNull("mqttClient", mqttClient);
    assertTrue("mqttClient.isConnected", mqttClient.isConnected());
    // Return client
    try {
        transportPool.returnObject(mqttClient);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) MqttClientPool(org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool) NoSuchElementException(java.util.NoSuchElementException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

MqttClient (org.eclipse.kapua.transport.mqtt.MqttClient)6 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4 MqttClientConnectionOptions (org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions)3 NoSuchElementException (java.util.NoSuchElementException)2 MqttClientPool (org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool)2 MqttClientPoolSetting (org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 KapuaException (org.eclipse.kapua.KapuaException)1 MqttMessage (org.eclipse.kapua.transport.message.mqtt.MqttMessage)1 MqttPayload (org.eclipse.kapua.transport.message.mqtt.MqttPayload)1 MqttTopic (org.eclipse.kapua.transport.message.mqtt.MqttTopic)1 MqttClientSetting (org.eclipse.kapua.transport.mqtt.setting.MqttClientSetting)1