0

is there any other way to add actions for destroying specific session in other way than by implementing SessionDestroyListener. I want to fire it in other places in code. For example i want to delete some data from static list that are assigned for session that will be destroyed but left other elements in this list that are assigned for still living sessions. I tried to add something like this:

ui.getSession().getService().addSessionDestroyListener( evt -> {
                list.remove(this);
            });

But then this listener is fired as excpected when some session is destroyed but then it is fired also for still living sessions. So after one session is detroyed then element for all sessions are removed. In debugging i discoverd that each Session have this same Service.

I'm using Vaadin 14.8.14.

I want to fire destroy listener only for session where it was added.

  • It would probably be easier to help if you asked about what you want to achieve instead of telling about how SessionDestroyListener doesn't work. – ollitietavainen Jan 10 '23 at 11:46

2 Answers2

1

You can do per-session actions with VaadinService::addSessionDestroyListener by adding the actual listener only once (e.g. in a VaadinServiceInitListener and storing the actual thing to do as an attribute in each session.

For the particular example you show, you might want to store the list item to remove in each session:

someSession.setAttribute("item-to-remove", someItem);

And then then singleton listener can do something like like this:

Object itemToRemove = event.getSession().getAttribute("item-to-remove");
list.remove(itemToRemove);

For more complex cases, you can also store a callback in the session instead of only data.

One unrelated thing to notice is that multiple session destroy events may be fired concurrently which means that you need to guard your list against concurrent modification e.g. by using a CopyOnWriteArrayList or using Collections::synchronizedList.

Leif Åstrand
  • 7,820
  • 13
  • 19
0

I made it work with mapping VaadinSession to SpringVaadinSession and then using addDestroyLsitener:

VaadinSession session = SpringVaadinSession.getCurrent();
if(session instanceof SpringVaadinSession) {
    ((SpringVaadinSession) session).addDestroyListener(evt -> {
            someFunctionThatIsfiredWhenSessionIsDestoryed();
    });
}

I created this issue with as simple code as i could but i want to use it for more complex actions like for executing longer than one code line functions.