use of org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions 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.MqttClientConnectionOptions 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());
}
use of org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions in project kapua by eclipse.
the class MqttClientTest method testMqttClientConnectDisconnect.
/**
* 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 testMqttClientConnectDisconnect() 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());
}
//
// Verify
assertTrue("client.connected", mqttClient.isConnected());
assertEquals("client.clientId", clientConnectOptions.getClientId(), mqttClient.getClientId());
// Disconnect
try {
mqttClient.disconnectClient();
} catch (Exception e) {
fail(e.getMessage());
}
assertFalse("client.connected", mqttClient.isConnected());
}
Aggregations