Session expiry added.

This commit is contained in:
marcins78
2013-01-09 17:25:34 +00:00
parent 3bb3c7a8e7
commit 70347eb439

View File

@@ -11,8 +11,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
public class LoggedUserHolder {
private long _loggedUserExpireLength = 1000 * 60 * 10;
private long _expireCheckInterval = 1000 * 60;
private long _loggedUserExpireLength = 1000 * 60 * 10; // 10 minutes session length
private long _expireCheckInterval = 1000 * 60; // check every minute
private Map<String, String> _users = Collections.synchronizedMap(new HashMap<String, String>());
private Map<String, Long> _lastAccess = Collections.synchronizedMap(new HashMap<String, Long>());
@@ -36,6 +36,7 @@ public class LoggedUserHolder {
if (value != null) {
String loggedUser = _users.get(value);
if (loggedUser != null) {
System.out.println("Update last access");
_lastAccess.put(value, System.currentTimeMillis());
return loggedUser;
}
@@ -82,23 +83,25 @@ public class LoggedUserHolder {
while (true) {
_readWriteLock.writeLock().lock();
try {
long currentTime = System.currentTimeMillis();
Iterator<Map.Entry<String, Long>> iterator = _lastAccess.entrySet().iterator();
if (iterator.hasNext()) {
Map.Entry<String, Long> lastAccess = iterator.next();
if (lastAccess.getValue() > System.currentTimeMillis() + _loggedUserExpireLength) {
long expireAt = lastAccess.getValue() + _loggedUserExpireLength;
if (expireAt < currentTime) {
String userValue = lastAccess.getKey();
_users.remove(userValue);
iterator.remove();
}
}
try {
Thread.sleep(_expireCheckInterval);
} catch (InterruptedException exp) {
}
} finally {
_readWriteLock.writeLock().unlock();
}
try {
Thread.sleep(_expireCheckInterval);
} catch (InterruptedException exp) {
}
}
}
}