views:

173

answers:

2

Hi,

I hava a web app running on a local tomcat server.

When the user starts the app (via desktop shortcut) the server starts and the app is opened in a browser window. But when the user just clicks on the close button to stop the application the server is still running in the background - that's annoying.

I tried to utilize the "unonload" and "onbeforeunload" events from javascript but unfortunately these events are also fired on some other requests in the app. So I can't use them, except I do a lot of refactoring.

Does anyone have an idea for a possible solution?

Btw, what I find interesting is the behaviour of Visual Studio when debugging a web application. When I close the browser window Visual Studio also gets a trigger to stop debug mode. So it seems it somehow notices the close event of the browser window, which would be exactly what I need. But I don't know how they do it...

Cheers, Helmut

A: 

You can setup a short session timeout, and use a HttpSessionListener. On sessionDestroyed(..) you can stop tomcat (using catalina.bat for example) .

Otherwise you can try to detect browser close, and send a shutdown message to the server using ajax (before the browser is closed).

Bozho
Thanks for your answer! On some systems the app runs constantly but is only used from time to time. So when I set a short session timeout and act on sessionDestroyed(..) I'm afraid the app isn't available when the users need them. So I really have to know when the user closes the browser window.
agez
@agez see my update
Bozho
+1  A: 

Can you wrap the starting of Tomcat and launching your app in a batch file or shell script? (Not sure what your target OS is...)

The script/batch file would start Tomcat and then launch your application. When the user exits your application, the script/batch file would then shut down Tomcat.

kmontgom
Thanks for the answer!The target OS is Windows and I actually start the app and the server via batch. But users normally close the app via the close button of the browser window ... and tomcat keeps running ...
agez
OK, but can you also start the application in the same batch file as you start and stop Tomcat?i.e. application.bat:tomcat startyourapp.exetomcat stopUnless you do a "start yourapp.exe", running yourapp will suspend batch processing until it exits. When it does exit, tomcat should be shut down.The key is to put all of that in a single batch file.
kmontgom
Unfortunately this doesn't come out right in the comments. In the batch file: tomcat start <newline> yourapp.exe <newline> tomcat stop <newline>.I can't remember the catalina command parameters to start and stop tomcat.
kmontgom
this sounds like a real good suggestion. Any other way is going to be a hack.
Sky Sanders
I think I'll give this suggestion a try. It sounds good. Thanks. I'll give feedback if it worked out.
agez