Search in sources :

Example 1 with MqttClientSetting

use of org.eclipse.kapua.transport.mqtt.setting.MqttClientSetting 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)

Aggregations

URI (java.net.URI)1 KapuaException (org.eclipse.kapua.KapuaException)1 MqttClient (org.eclipse.kapua.transport.mqtt.MqttClient)1 MqttClientConnectionOptions (org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions)1 MqttClientPoolSetting (org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting)1 MqttClientSetting (org.eclipse.kapua.transport.mqtt.setting.MqttClientSetting)1