Skip to content

Commit

Permalink
update: 更改QoS=1/QoS=2 messageId 生成规则
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Jul 31, 2018
1 parent 96856ba commit 3ec187b
Showing 1 changed file with 13 additions and 15 deletions.
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");
}
}

0 comments on commit 3ec187b

Please sign in to comment.