fix bug: Under high concurrency, Mqtt client nextMessageId exceeds the 0xffff limit (#2564)
The compareAndSet method and getAndIncrement method of the AtomicInteger class are atomic, but when these two methods are used at the same time, they are no longer atomic.
This commit is contained in:
parent
d94cb9ca47
commit
00b5d36e0b
@ -407,8 +407,12 @@ final class MqttClientImpl implements MqttClient {
|
||||
}
|
||||
|
||||
private MqttMessageIdVariableHeader getNewMessageId() {
|
||||
int messageId;
|
||||
synchronized (this.nextMessageId) {
|
||||
this.nextMessageId.compareAndSet(0xffff, 1);
|
||||
return MqttMessageIdVariableHeader.from(this.nextMessageId.getAndIncrement());
|
||||
messageId = this.nextMessageId.getAndIncrement();
|
||||
}
|
||||
return MqttMessageIdVariableHeader.from(messageId);
|
||||
}
|
||||
|
||||
private Future<Void> createSubscription(String topic, MqttHandler handler, boolean once, MqttQoS qos) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user