Skip to content

Instantly share code, notes, and snippets.

@mstahv
Last active June 2, 2023 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstahv/956b4682fd3b16ffb5d1a0638892893a to your computer and use it in GitHub Desktop.
Save mstahv/956b4682fd3b16ffb5d1a0638892893a to your computer and use it in GitHub Desktop.
Prototype of session timeout warning dialog
package org.vaadin.vaadin.sb.example.views;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.VaadinSession;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@Route
public class PushBased extends VerticalLayout {
private static ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
private Span currentTimeout;
private int sessionInSeconds;
public PushBased() {
Button button = new Button("Click me",
event -> add(new Paragraph("Clicked!")));
add(button);
}
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
Logger.getLogger(getClass().getSimpleName()).info("Attached MainView");
UI ui = attachEvent.getUI();
sessionInSeconds = VaadinSession.getCurrent().getSession().getMaxInactiveInterval();
ses.scheduleAtFixedRate(() -> {
Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "Scheduler check request for UI {0} in session {1}", new Object[]{ui.getUIId(), ui.getSession().getSession().getId()});
try {
if (!ui.isAttached()) {
Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "UI {0}, is not attached!", new Object[]{ui.getUIId()});
throw new RuntimeException("Stop this task...");
}
ui.access(() -> {
long lastAccessedTime = ui.getSession().getLastRequestTimestamp();
Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "UI {0}, last access: {1}", new Object[]{ui.getUIId(), lastAccessedTime});
long now = System.currentTimeMillis();
// use f (float) for better handling of seconds
float secondsSinceLastAction = (now - ui.getSession().getLastRequestTimestamp()) / 1000f;
// use round to allow 0.7 to be 1 and 1.7 to be 2
int secondsToLogout = (sessionInSeconds - Math.round(secondsSinceLastAction));
updateCurrentTimeout(secondsToLogout);
if (secondsToLogout > 2) {
return; // 'auto save should only happen if minutesTilLogout == 1'
}
if (secondsToLogout == 2) {
}
if (secondsToLogout <= 1) {
}
});
} catch (Exception e) {
Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "ERROR", e);
throw e;
}
}, 1, 1, TimeUnit.SECONDS);
}
@Override
protected void onDetach(DetachEvent detachEvent) {
super.onDetach(detachEvent);
Logger.getLogger(getClass().getSimpleName()).info("Detached MainView");
}
private void updateCurrentTimeout(int minutesTilLogout) {
var text = minutesTilLogout + " seconds of session remaining";
if (null == currentTimeout) {
add(currentTimeout = new Span(text));
} else {
currentTimeout.setText(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment