com.payneteasy.superfly.client.SessionToucherImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of superfly-client Show documentation
Show all versions of superfly-client Show documentation
Contains classes used to use Superfly facilities by clients
package com.payneteasy.superfly.client;
import com.payneteasy.superfly.api.SSOService;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Default session toucher implementation. Accumulates IDs of sessions
* and sends them in bulks every minute (by default).
*
* @author Roman Puchkovskiy
*/
public class SessionToucherImpl implements SessionToucher {
private boolean enabled = true;
private SSOService ssoService;
private int flushPeriodInSeconds;
private final Queue queue = new ConcurrentLinkedQueue();
private Timer timer;
public SessionToucherImpl() {
this(60); // every minute
}
public SessionToucherImpl(int flushPeriodInSeconds) {
this.flushPeriodInSeconds = flushPeriodInSeconds;
}
public void setSsoService(SSOService ssoService) {
this.ssoService = ssoService;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@PostConstruct
public void startup() {
if (ssoService == null) {
throw new IllegalStateException("ssoService not set");
}
if (enabled) {
timer = new Timer("session-toucher-timer");
timer.scheduleAtFixedRate(new ToucherTask(),
getFlushPeriodInMillis(), getFlushPeriodInMillis());
}
}
private long getFlushPeriodInMillis() {
return flushPeriodInSeconds * 1000;
}
@PreDestroy
public void shutdown() {
if (timer != null) {
timer.cancel();
}
}
@Override
public void addSessionId(long sessionId) {
if (enabled) {
queue.add(sessionId);
}
}
private class ToucherTask extends TimerTask {
@Override
public void run() {
Set sessionIds = new HashSet();
Long sessionId;
do {
sessionId = queue.poll();
if (sessionId != null) {
sessionIds.add(sessionId);
}
} while (sessionId != null);
if (!sessionIds.isEmpty()) {
ssoService.touchSessions(new ArrayList(sessionIds));
}
}
}
}