Application close

Hi,

I have a web application which controls some proprietory hardware via a set of native libraries.

In order to increase efficiency a setup native method needs to be called once and only once when the first instnace of the web application starts, and when the last instance closes a shutdown native method needs to be called. This way the harware is only ever ‘active’ when at least one web app is running.

I have achieved this by having a static flag in the application class which tracks the whether the hardware is currently setup, and a static integer which tracks the number of open application instmnaces. I can then shutdown the hardware when the last application instnace closes. The code for this is:

//flag indicating the device is started
private static boolean hardwareInitialised = false;

//count of the number of active application instances
private static int activeApplications = 0;

@Override
public void init(){
    logger.info("Application starting");
    activeApplications++;

    //peform hardware initialisation
    initialiseHardware();
        
    setMainWindow(new LoginWindow());
}
    
@Override
public void close() {
    super.close();
    //peform hardware shutdown
    shutdownHardware();
    activeApplications--;
}

private static synchronized void initialiseHardware(){
    if(!hardwareInitialised){
        DMXControl.startDevice(); // this is the harware setup method
	logger.info("DMX Device started");
	hardwareInitialised = true;
    }
}
    
private static synchronized void shutdownHardware(){
    if(activeApplications == 1){
        DMXControl.stopDevice();
    	logger.info("DMX Device stopped");
    }
}

This seems to work ok if I allow the application instance to close due to session timeouts, unfortunatley if I kill the tomcat container then the system just terminates and none of the close methods are called.

I am assuming this means Vaadin doesn’t reigister any context listeners; is this correct? If so will I be best off managing the hardware setup/shutdown outside of Vaadin in my own context listenerns or is there a Vaadin approve mechanism for this?

Thanks for any assistance.

Simon.

There is no such event in Vaadin.

Your problem is more related with Tomcat than with Vaadin. You may want to look at the Tomcat LifecycleListeners http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Lifecycle_Listeners

It’s not really a problem to ‘solve’ this as it is trivial using a javax.servlet.ServletContextListener, and this is, in fact, what I have done. I was just wondering if there was a 100% Vaadin solution.

Can you give little bit more specific example on how I can listen when my browser window or tab is closed or when I browse to another web page. I know I will eventually get application.close event when session timeout but is there a way know when application is closed in browser in other words, mainwindow is not visible anymore?

Have you tried Window.CloseListener on the main application windows?

Yes, I noticed that’s the way to know when browser is closed or application page is closed in IE7 or Firefox. But for example in Opera this kind of event is not published. I was also curious to know when browser is refreshed and couldn’t figure out any other solution than parse requests coming to my application.

For the Opera issue, if I remember correctly, this is because Opera (unlike many other browsers) does not give the client side JavaScript a chance to notify the server that the page is closing but simply terminates the execution.

As for detecting page reloads - even though not needed frequently in Vaadin applications - is indeed unnecessarily hard with current Vaadin versions. Right now, I cannot recall what options are available or which method in AbstractApplicationServlet you might need to override - the method service() is the common entry point through which all requests come, so you might want to start looking there.

Maybe someone else can chime in on this, or maybe there is an older forum thread.