-1

I have this page:

<%@page language="java"%>
<html>
     <head></head>
     <body>
         <% while(true) System.out.println("foo"); %>
    </body>
</html>

and I want stop this page execution when I close browser, before session expiration.

My idea is to use JavaScript to catch onunload event, use AJAX to postback the thread id to a servlet/another JSP, and brutally kill the thread after some time, i.e. 15s.

Is this correct? Is it possible? Any better idea?

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
  • 1
    Why would you ever have such a JSP page? – BalusC Nov 21 '11 at 19:04
  • this is a simplification, i have some jsp/servlets that execute long-running procedures on database, working on graphs. sometimes i need to stop these procedures before they naturally complete, sometimes i want these procedures to be tied to the client, because if the client disconnects, it is not possible to view results... so precious resources were completely wasted... – Michele Mariotti Nov 22 '11 at 17:02
  • Manually spawning unmanaged threads in a Java EE environment is an extremely bad practice. If you have 100 or even less visitors doing this simultaneously, you'd have to restart the server because it's totally locked up. Instead, you should use a fixed thread pool and executor service with a task queue. Most containers support this out the box. For some pointers, check those answers: http://stackoverflow.com/q/5357856 and http://stackoverflow.com/q/6151574 – BalusC Nov 22 '11 at 17:04

1 Answers1

0

You could hava a JSP page that stopped writing after asynchronous receipt of a message from a client, for example :

1) The page goes on writing, as you mentioned.

2) After every few writes, it could check in a message queue or database, for an indication that it should stop streaming data. Since the JSP is running to the server, it doesnt have any connection to the client, so this check has to be decoupled from the browser.

However, as per the comment --- its not clear that this is ever necessary.

jayunit100
  • 17,388
  • 22
  • 92
  • 167
  • since my application is extremely big, this is not a valid route. i'm searching for some "hack" to apply generally to every existent procedure. another thing i don't understand is how can be possible that a `out.print()` does not fail even if the client is listening no more... – Michele Mariotti Nov 22 '11 at 17:06
  • it could be a valid solution (very dirty but still valid) if `out.print()` would throw Exception, because most of these loops i have are "reports" that use `out.print()` – Michele Mariotti Nov 22 '11 at 17:12