use of io.netty.util.concurrent.DefaultPromise in project thingsboard by thingsboard.
the class MqttClientImpl method connect.
private Future<MqttConnectResult> connect(String host, int port, boolean reconnect) {
if (this.eventLoop == null) {
this.eventLoop = new NioEventLoopGroup();
}
this.host = host;
this.port = port;
Promise<MqttConnectResult> connectFuture = new DefaultPromise<>(this.eventLoop.next());
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(this.eventLoop);
bootstrap.channel(clientConfig.getChannelClass());
bootstrap.remoteAddress(host, port);
bootstrap.handler(new MqttChannelInitializer(connectFuture, host, port, clientConfig.getSslContext()));
ChannelFuture future = bootstrap.connect();
future.addListener((ChannelFutureListener) f -> {
if (f.isSuccess()) {
MqttClientImpl.this.channel = f.channel();
MqttClientImpl.this.channel.closeFuture().addListener((ChannelFutureListener) channelFuture -> {
if (isConnected()) {
return;
}
ChannelClosedException e = new ChannelClosedException("Channel is closed!");
if (callback != null) {
callback.connectionLost(e);
}
pendingSubscriptions.forEach((id, mqttPendingSubscription) -> mqttPendingSubscription.onChannelClosed());
pendingSubscriptions.clear();
serverSubscriptions.clear();
subscriptions.clear();
pendingServerUnsubscribes.forEach((id, mqttPendingServerUnsubscribes) -> mqttPendingServerUnsubscribes.onChannelClosed());
pendingServerUnsubscribes.clear();
qos2PendingIncomingPublishes.clear();
pendingPublishes.forEach((id, mqttPendingPublish) -> mqttPendingPublish.onChannelClosed());
pendingPublishes.clear();
pendingSubscribeTopics.clear();
handlerToSubscribtion.clear();
scheduleConnectIfRequired(host, port, true);
});
} else {
scheduleConnectIfRequired(host, port, reconnect);
}
});
return connectFuture;
}
Aggregations