-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: 更改QoS=1/QoS=2 messageId 生成规则
- Loading branch information
Showing
1 changed file
with
13 additions
and
15 deletions.
There are no files selected for viewing
28 changes: 13 additions & 15 deletions
28
mqtt-store/src/main/java/cn/wizzer/iot/mqtt/server/store/message/MessageIdService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
/** | ||
* Copyright (c) 2018, Mr.Wang (recallcode@aliyun.com) All rights reserved. | ||
*/ | ||
|
||
package cn.wizzer.iot.mqtt.server.store.message; | ||
|
||
import cn.wizzer.iot.mqtt.server.common.message.IMessageIdService; | ||
import org.nutz.integration.jedis.RedisService; | ||
import org.nutz.ioc.loader.annotation.Inject; | ||
import org.nutz.ioc.loader.annotation.IocBean; | ||
|
||
@IocBean | ||
/** | ||
* Created by wizzer on 2018 | ||
*/ | ||
@IocBean(create = "init") | ||
public class MessageIdService implements IMessageIdService { | ||
|
||
private final int MIN_MSG_ID = 1; | ||
|
||
private final int MAX_MSG_ID = 65535; | ||
|
||
@Inject | ||
private RedisService redisService; | ||
|
||
private int nextMsgId = MIN_MSG_ID - 1; | ||
private int nextMsgId = 0; | ||
|
||
@Override | ||
public int getNextMessageId() { | ||
try { | ||
nextMsgId = redisService.incr("mqttwk:messageid:num").intValue(); | ||
if (nextMsgId > MAX_MSG_ID) { | ||
nextMsgId = MIN_MSG_ID; | ||
redisService.set("mqttwk:messageid:num", "" + MIN_MSG_ID); | ||
} | ||
nextMsgId = (int) (redisService.incr("mqttwk:messageid:num") % 65535); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return nextMsgId; | ||
} | ||
|
||
/** | ||
* 每次重启的时候初始化 | ||
*/ | ||
public void init() { | ||
redisService.del("mqttwk:messageid:num"); | ||
} | ||
} |