Closing an application

Dear all,

I wonder how I can close an application and make sure the next request coming in creates a new application.

Application.close() seems not to work 100% correct for my needs as it seems to remove any application related data, expect the application instance itself. That means any members stored at the application level instance are still available and prefilled when the next request comes in. Yes init() is called again, but the application instance itself seems to be reused.

Is there a way to force creation of a new application or closing the http-session with vaadin standard calls? For now it is ok to just clear all data or re-create them on init(). But I was really surprised that the application instance is reused. I never read about that. Is it documented somewhere?

And I wondered why my events are executed multiple times (I had an eventbus and after every close/init cycle I had more eventlisteners in the same eventbus, at it was attached to the application). :smiley: :smiley: :smiley:

Thank you so much

Okay, seems that session and application have different life-cycles. So I need to close the session myself.

you could add the following to your main window close event:

((WebApplicationContext)mainwindow.getApplication().getContext()).getHttpSession().setMaxInactiveInterval(1);

I am not sure if that is what you are looking for but there it is.

Thank you.

I guess I do something similar, like invalidating the session when the user logs off the system. But I wonder if this is just an issue of using the CDI approach of creating the application or if this is Vaadin standard behaviour. Actually I expected the http-session to close when app.close is called… :slight_smile:

A session can potentially have multiple active Vaadin applications, which is why the session is not closed when closing the application.

The typical scenario where this can occur is having multiple different applications in the same WAR and using them with the same browser.

This makes perfect sense, thank you. My problem was that after closing I had the very same application instance, but I figured out that was due to the way I initialized the application (@Sessionscoped). Having eliminated the session scope I get a new application instance, this is great :slight_smile:

Hi Chris,

Can you provide me an example of that @Sessionscoped. Sorry was new in Vaadin. Thank you!

Hi

You better don’t use the sessionscoped approach IMHO. But here is how you can implement it.


https://vaadin.com/wiki/-/wiki/Main/Creating%20JEE6%20Vaadin%20Applications

See for option 2.

Important is to use Instance<> to inject the application. Note that when you close the application your session still continues and accessing the application again after closing it will be the very same.

If you don’t use/need injection at any point in your application I would suggest to configure your application via web.xml as described in the Vaadin book.

Hi Chris,

Thanks for the reply. I already did Option 2. The weird thing I am getting is that, my events are like doubling up every time I close the browser window. When I open the application once in the browser I got one event listener. But once I close the browser and and start/open a new browser then open again my application, It does multiply my event depending on how many times I close and open the browser. However the event listeners are being reset to single instance once I restart the server. I hope you can help me on this. Thanks!

Hi

Without seeing your application code it is difficult to know exactly what is going on.
As I mentioned using option 2 makes u reuse the
same application
. So I guess your event listeners are there from a previous application-“session”.

You can simply test that by just restarting the application server instead of just the browser. Another option would be use 2 different browsers. If the events still double up you have something totally weird like using one application instance for multiple users. Make sure your application is annotated with @Sessionscoped!

If it is due to reusing the same application over and over (as long as the HttpSession lives…) you just have to invalidate the session when closing the application. I never did this but I guess overriding Application.close() and invalidating the HttpSession after calling super.close() may be enough. Google for how to access the HttpSession.

EDIT:

In the ApplicationServlet where you have getNewApplication() just print the application you are returning to the console. You’ll see if it is always the same if the printed lines are all the same.

Hi Chris,

Thank you very much! You have been very helpful. Some weird things actually, You are right with that one I quote. The problem is, I am not using spring and don’t know how to annotate for @sessionscoped without the spring framework, also I already have this in my code. Which I call after I close my window. However, when I restart the browser the event still goes double. I will try to investigate further, but just in case you have some more idea can you please help suggest what I can still do. I will try to put a piece of code here once I got time to clear things, because of some privacy issues.

@Override
public void close(){
super.close();
HttpSession session = ((WebApplicationContext)this.getApplication().getContext()).getHttpSession();
session.invalidate();
}

Found out what happened. Though it is really very very weird. The buttons doubling up the events are actually global buttons. But when I changed it to method scoped the button the event is not doubling up. Don’t know what is the problem with the global/local variables with event. Thanks for the help Chris.

Hi, no problem.
It is difficult to give you some hint not knowing the exact setup. But good if you are on the right way now.

Hey guys,
in fact that this thread is 1 year old I assume this is all about Vaadin < 7.
I’m using Vaadin 7 and I need to implement an log off for my app.

I’m doing the following in my UI:
this.getSession().close();
this.close();

and then i switch to the login-page:
this.getNavigator().navigateTo(INavigation.LOGINVIEW);

The session gets closed and the Vaadin-Message SessionExpired is displayed.
Now I got 2 problems:

  1. The message should not been displayed but i need to kill it(the Session).
  2. If I reload the application I get this Exception:
java.lang.IllegalStateException: UI id has already been defined
	com.vaadin.ui.UI.doInit(UI.java:593)
	com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
	com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
	com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
	com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1329)
	com.vaadin.server.VaadinServlet.service(VaadinServlet.java:236)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I only get the Exception if i manually close the app. If there is a SessionTimeout, everything works fine.

Any clues?
Thx, and hv a nice day!

Have a look at the “closing a session” part of
this book page
.

Thx, this helps with number 1.

In my UI:

		this.close();
		this.detach();

		// Redirect from the page
		getUI().getPage().setLocation("/myApp/");
		// Close the VaadinSession
		getSession().close();

The exception comes from a Spring-Vaadin misunderstanding from me. I used @Scope(“session”) for my UI and thought it is the same session like the Vaadin session. My fault, thx anyway!