com.gw.common.utils.OnlineStatus Maven / Gradle / Ivy
The newest version!
package com.gw.common.utils;
import java.util.Hashtable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OnlineStatus implements IConnectionListener {
private final Hashtable hashtable = new Hashtable();
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void onConnected(String serverName) {
addCount(serverName, 1);
}
@Override
public void onDisconnected(String serverName) {
addCount(serverName, -1);
}
private synchronized void addCount(String serverName, int num) {
Integer count = hashtable.get(serverName);
if (count == null) {
count = 0;
}
count += num;
if (count < 0) {
count = 0;
}
hashtable.put(serverName, count);
logger.debug("OnlineStatus: name={},count={}", serverName, count);
}
public boolean isOnline(String serverName) {
Integer count = hashtable.get(serverName);
logger.debug("{}:{}",serverName,count);
if (count == null) {
return false;
} else {
return count > 0;
}
}
}