Created
June 6, 2018 10:49
-
-
Save Coneboy-k/9c7496abc45012f6943d2a94ffcfa93a to your computer and use it in GitHub Desktop.
PoolItemService
This file contains hidden or 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
/** | |
* 定期回收心跳的数据 | |
*/ | |
public class PoolItemService { | |
final static HashedWheelTimer TIMER = new HashedWheelTimer(KKThreadFactory.getInstance("MOTimer", 1)); | |
private ManageClientServiceService manageClientService; | |
public PoolItemService(ManageClientServiceService iTimerToHeat) { | |
this.manageClientService = iTimerToHeat; | |
} | |
private ConcurrentHashMap<String, ClientTimerItem> timerMap = new ConcurrentHashMap<>(); | |
protected void addTaskTimer(String clientID, HY_CLIENT_DEVICE_TYPE deviceType) { | |
Preconditions.checkNotNull(clientID); | |
if (deviceType == null) { | |
deviceType = HY_CLIENT_DEVICE_TYPE.UNKNOW; | |
} | |
String idTmp = clientID + "_" + deviceType.getValue(); | |
int READER_IDLETIMESECONDS = 30; | |
int WRITE_IDLETIMESECONDS = 4; | |
ClientTimerItem timerItemTmp = new ClientTimerItem(clientID, deviceType); | |
timerItemTmp.initTimer(TIMER, READER_IDLETIMESECONDS, WRITE_IDLETIMESECONDS); | |
timerItemTmp.startTimer(); | |
ClientTimerItem fuckTimer = timerMap.putIfAbsent(idTmp, timerItemTmp); | |
// 如果数据 | |
if (fuckTimer != null) { | |
timerItemTmp.stopTimer(); | |
log.info("MC:timer 重复添加了..."); | |
} | |
} | |
/** | |
* 读取激活下 | |
*/ | |
protected void activeTaskTimer(String clientID, HY_CLIENT_DEVICE_TYPE deviceType) { | |
Preconditions.checkNotNull(clientID); | |
if (deviceType == null) { | |
deviceType = HY_CLIENT_DEVICE_TYPE.UNKNOW; | |
} | |
String idTmp = clientID + "_" + deviceType.getValue(); | |
ClientTimerItem timerItem = this.timerMap.get(idTmp); | |
if (timerItem != null) { | |
timerItem.nowActiveReadingTimer(); | |
} else { | |
log.warn("MC: clientID={} deviceType={} not in map", clientID, deviceType); | |
this.addTaskTimer(clientID, deviceType); | |
} | |
} | |
protected void removeTaskTimer(String clientID, HY_CLIENT_DEVICE_TYPE deviceType) { | |
Preconditions.checkNotNull(clientID); | |
if (deviceType == null) { | |
deviceType = HY_CLIENT_DEVICE_TYPE.UNKNOW; | |
} | |
String idTmp = clientID + "_" + deviceType.getValue(); | |
ClientTimerItem fuckTimer = timerMap.remove(idTmp); | |
if (fuckTimer != null) { | |
fuckTimer.stopTimer(); | |
} | |
} | |
protected void removeAllTimer() { | |
if (CollectionUtil.isEmpty(timerMap)) { | |
return; | |
} | |
// 清空所有item | |
for (ClientTimerItem itemTmp : timerMap.values()) { | |
itemTmp.stopTimer(); | |
} | |
timerMap.clear(); | |
} | |
/** | |
* 关闭timer,同时停止所有数据 | |
*/ | |
protected void start() { | |
// 其实这个在创建HashTimer的时候就启动了 | |
TIMER.start(); | |
} | |
/** | |
* 关闭timer,同时停止所有数据 | |
*/ | |
protected void stop() { | |
this.removeAllTimer(); | |
TIMER.stop(); | |
} | |
protected class ClientTimerItem extends AbsTimerItem { | |
private final String clientID; | |
private final HY_CLIENT_DEVICE_TYPE deviceType; | |
public ClientTimerItem(String clientID, HY_CLIENT_DEVICE_TYPE deviceType) { | |
this.clientID = clientID; | |
this.deviceType = deviceType; | |
} | |
/** | |
* 读取超时准备释放资源 | |
*/ | |
@Override | |
public void alreadyTimerOut() { | |
PoolItemService.this.manageClientService.alreadyTimerOut(clientID, deviceType); | |
} | |
/** | |
* 改进行写了 | |
*/ | |
@Override | |
public void timeToWrite() { | |
PoolItemService.this.manageClientService.timeToWrite(clientID, deviceType); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof ClientTimerItem)) return false; | |
ClientTimerItem that = (ClientTimerItem) o; | |
if (!clientID.equals(that.clientID)) return false; | |
return deviceType == that.deviceType; | |
} | |
@Override | |
public int hashCode() { | |
int result = clientID.hashCode(); | |
result = 31 * result + deviceType.hashCode(); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment