[Minor] Use ScheduledExecutor for session timeout and persistence
This commit is contained in:
parent
fd4d428000
commit
0c68006f77
|
@ -29,8 +29,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Timer;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -64,7 +63,8 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
|
||||||
private Map<String, Certificate> certificateMap;
|
private Map<String, Certificate> certificateMap;
|
||||||
private boolean reloadSessions;
|
private boolean reloadSessions;
|
||||||
private long sessionTtl;
|
private long sessionTtl;
|
||||||
private Timer sessionTimeoutTimer;
|
|
||||||
|
private ScheduledFuture<?> sessionHandler;
|
||||||
|
|
||||||
public DefaultStrolchSessionHandler(ComponentContainer container, String componentName) {
|
public DefaultStrolchSessionHandler(ComponentContainer container, String componentName) {
|
||||||
super(container, componentName);
|
super(container, componentName);
|
||||||
|
@ -97,20 +97,21 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
|
||||||
+ (certificates.size() - this.certificateMap.size()) + " had timed out and were removed.");
|
+ (certificates.size() - this.certificateMap.size()) + " had timed out and were removed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sessionTimeoutTimer = new Timer("SessionTimeoutTimer", true); //$NON-NLS-1$
|
this.sessionHandler = getScheduledExecutor().scheduleWithFixedDelay(this::handleSessions, 5, 1,
|
||||||
long delay = TimeUnit.MINUTES.toMillis(5);
|
TimeUnit.MINUTES);
|
||||||
long checkInterval = TimeUnit.MINUTES.toMillis(1);
|
|
||||||
this.sessionTimeoutTimer.schedule(new SessionTimeoutTask(), delay, checkInterval);
|
|
||||||
|
|
||||||
super.start();
|
super.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() throws Exception {
|
public void stop() throws Exception {
|
||||||
|
|
||||||
|
if (this.sessionHandler != null)
|
||||||
|
this.sessionHandler.cancel(true);
|
||||||
|
|
||||||
if (this.reloadSessions) {
|
if (this.reloadSessions) {
|
||||||
|
|
||||||
runAsAgent(ctx -> getContainer().getPrivilegeHandler().getPrivilegeHandler()
|
persistSessions();
|
||||||
.persistSessions(ctx.getCertificate()));
|
|
||||||
|
|
||||||
} else if (this.certificateMap != null) {
|
} else if (this.certificateMap != null) {
|
||||||
synchronized (this.certificateMap) {
|
synchronized (this.certificateMap) {
|
||||||
|
@ -121,11 +122,6 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.sessionTimeoutTimer != null) {
|
|
||||||
this.sessionTimeoutTimer.cancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sessionTimeoutTimer = null;
|
|
||||||
this.privilegeHandler = null;
|
this.privilegeHandler = null;
|
||||||
super.stop();
|
super.stop();
|
||||||
}
|
}
|
||||||
|
@ -217,18 +213,13 @@ public class DefaultStrolchSessionHandler extends StrolchComponent implements St
|
||||||
return this.certificateMap;
|
return this.certificateMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void handleSessions() {
|
||||||
* Simpler {@link TimerTask} to check for sessions which haven't been active for
|
|
||||||
* {@link DefaultStrolchSessionHandler#PARAM_SESSION_TTL_MINUTES} minutes.
|
|
||||||
*
|
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
|
||||||
*/
|
|
||||||
private class SessionTimeoutTask extends TimerTask {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
checkSessionsForTimeout();
|
checkSessionsForTimeout();
|
||||||
|
persistSessions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void persistSessions() {
|
||||||
|
runAsAgent(ctx -> this.privilegeHandler.getPrivilegeHandler().persistSessions(ctx.getCertificate()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkSessionsForTimeout() {
|
private void checkSessionsForTimeout() {
|
||||||
|
|
Loading…
Reference in New Issue