I know this is not answering your question about servlet registration, but an easier way to install session listener is to implement a VaadinServiceInitListener. By default it should be registered through a META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener file, but since you are using running spring you just need to expose your implementation as a bean
@Component
private static final class MySessionListener implements SessionInitListener, SessionDestroyListener {
@Override
public void sessionDestroy(SessionDestroyEvent sessionDestroyEvent) {
// destroy stuff...
}
@Override
public void sessionInit(SessionInitEvent sessionInitEvent) throws ServiceException {
// destroy stuff...
}
}
@Component
private static class MyVaadinServiceInitListener implements VaadinServiceInitListener {
private final MySessionListener sessionListener;
private MyVaadinServiceInitListener(MySessionListener sessionListener) {
this.sessionListener = sessionListener;
}
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addSessionInitListener(sessionListener);
event.getSource().addSessionDestroyListener(sessionListener);
}
}
I know this is not answering your question about servlet registration, but an easier way to install session listener is to implement a VaadinServiceInitListener. By default it should be registered through a META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener file, but since you are using running spring you just need to expose your implementation as a bean
@Component
private static final class MySessionListener implements SessionInitListener, SessionDestroyListener {
@Override
public void sessionDestroy(SessionDestroyEvent sessionDestroyEvent) {
// destroy stuff...
}
@Override
public void sessionInit(SessionInitEvent sessionInitEvent) throws ServiceException {
// destroy stuff...
}
}
@Component
private static class MyVaadinServiceInitListener implements VaadinServiceInitListener {
private final MySessionListener sessionListener;
private MyVaadinServiceInitListener(MySessionListener sessionListener) {
this.sessionListener = sessionListener;
}
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addSessionInitListener(sessionListener);
event.getSource().addSessionDestroyListener(sessionListener);
}
}
HTH
Marco
It works as what i needed! I only need to regist these listeners and nothing else, this must be best way to approach!
Thank you Marco!
Can I ask you for 1 more question? About how to get current UI in sessionDestroy, i want when the user session expire, it will navigate to specific route or show up a notification to let user know that they need to re-login.
when the SessionDestroy listener is invoked all UIs are removed already from session, so this seems not the correct place for your use case.
Since you are willing to redirect all UIs on session expiration (correct me if I’m wrong) I would try customizing SystemMessages.sessionExpiredURL; this can be done in the VaadinServiceInitListener.
With CustomizedSystemMessages you can also set custom notification messages instead of redirect the user.
Also remember that redirection will happen automatically only if you have @Push enabled,
otherwise a user interaction on the UI may be needed.
I don’t know if this is the best (or correct) way; maybe some Vaadin expert can give some advice.
when the SessionDestroy listener is invoked all UIs are removed already from session, so this seems not the correct place for your use case.
Since you are willing to redirect all UIs on session expiration (correct me if I’m wrong) I would try customizing SystemMessages.sessionExpiredURL; this can be done in the VaadinServiceInitListener.
With CustomizedSystemMessages you can also set custom notification messages instead of redirect the user.
Also remember that redirection will happen automatically only if you have @Push enabled,
otherwise a user interaction on the UI may be needed.
I don’t know if this is the best (or correct) way; maybe some Vaadin expert can give some advice.
HTH
Marco
Yes, i just find out that UI will be remove before my sessionDestroyListener, i made some trick to make it redirect and maybe it’s good for now, thank you for your useful solution!
There are something kinda weird on sessionInit() or serviceInit(), when my application start up, the sessionInit get called multiple times (around 2 ~ 4 times)
This only happen on first time when server start my application, and later (after session destroy) it back to normal, 1 session init at time, but randomly called 2 times at once.
Marco Collovati:
Please, try to check the request path on session init listener ( sessionInitEvent.getRequest().getPathInfo() ).
I did some tests and noticed the listener was called also for a /sw.js request, that is related to PWA.
After clearing site data from Chrome tools, the second call to the listener has gone.
I don’t know if session creation is expected for /sw.js or if it is a bug.
HTH
Marco
You are right, there is a request to /sw.js. I don’t know that it does.
Now i add some condition to check request path before set some default values to sessionInit. Evevrything still fine, maybe i need more time to see this behaviour.