Search in sources :

Example 1 with MqttClientConfig

use of org.thingsboard.mqtt.MqttClientConfig in project thingsboard by thingsboard.

the class TbMqttNode method initClient.

protected MqttClient initClient(TbContext ctx) throws Exception {
    MqttClientConfig config = new MqttClientConfig(getSslContext());
    if (!StringUtils.isEmpty(this.mqttNodeConfiguration.getClientId())) {
        config.setClientId(this.mqttNodeConfiguration.isAppendClientIdSuffix() ? this.mqttNodeConfiguration.getClientId() + "_" + ctx.getServiceId() : this.mqttNodeConfiguration.getClientId());
    }
    config.setCleanSession(this.mqttNodeConfiguration.isCleanSession());
    prepareMqttClientConfig(config);
    MqttClient client = MqttClient.create(config, null);
    client.setEventLoop(ctx.getSharedEventLoop());
    Future<MqttConnectResult> connectFuture = client.connect(this.mqttNodeConfiguration.getHost(), this.mqttNodeConfiguration.getPort());
    MqttConnectResult result;
    try {
        result = connectFuture.get(this.mqttNodeConfiguration.getConnectTimeoutSec(), TimeUnit.SECONDS);
    } catch (TimeoutException ex) {
        connectFuture.cancel(true);
        client.disconnect();
        String hostPort = this.mqttNodeConfiguration.getHost() + ":" + this.mqttNodeConfiguration.getPort();
        throw new RuntimeException(String.format("Failed to connect to MQTT broker at %s.", hostPort));
    }
    if (!result.isSuccess()) {
        connectFuture.cancel(true);
        client.disconnect();
        String hostPort = this.mqttNodeConfiguration.getHost() + ":" + this.mqttNodeConfiguration.getPort();
        throw new RuntimeException(String.format("Failed to connect to MQTT broker at %s. Result code is: %s", hostPort, result.getReturnCode()));
    }
    return client;
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) MqttConnectResult(org.thingsboard.mqtt.MqttConnectResult) MqttClientConfig(org.thingsboard.mqtt.MqttClientConfig) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with MqttClientConfig

use of org.thingsboard.mqtt.MqttClientConfig in project thingsboard by thingsboard.

the class MqttGatewayClientTest method getMqttClient.

private MqttClient getMqttClient(DeviceCredentials deviceCredentials, MqttMessageListener listener) throws InterruptedException, ExecutionException {
    MqttClientConfig clientConfig = new MqttClientConfig();
    clientConfig.setClientId("MQTT client from test");
    clientConfig.setUsername(deviceCredentials.getCredentialsId());
    MqttClient mqttClient = MqttClient.create(clientConfig, listener);
    mqttClient.connect("localhost", 1883).get();
    return mqttClient;
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) MqttClientConfig(org.thingsboard.mqtt.MqttClientConfig)

Example 3 with MqttClientConfig

use of org.thingsboard.mqtt.MqttClientConfig in project thingsboard by thingsboard.

the class MqttClientTest method getMqttClient.

private MqttClient getMqttClient(DeviceCredentials deviceCredentials, MqttMessageListener listener) throws InterruptedException, ExecutionException {
    MqttClientConfig clientConfig = new MqttClientConfig();
    clientConfig.setClientId("MQTT client from test");
    clientConfig.setUsername(deviceCredentials.getCredentialsId());
    MqttClient mqttClient = MqttClient.create(clientConfig, listener);
    mqttClient.connect("localhost", 1883).get();
    return mqttClient;
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) MqttClientConfig(org.thingsboard.mqtt.MqttClientConfig)

Aggregations

MqttClient (org.thingsboard.mqtt.MqttClient)3 MqttClientConfig (org.thingsboard.mqtt.MqttClientConfig)3 TimeoutException (java.util.concurrent.TimeoutException)1 MqttConnectResult (org.thingsboard.mqtt.MqttConnectResult)1