use of org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting 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;
}
use of org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting 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());
}
Aggregations