users@glassfish.java.net

Re: Interrupting a running Servlet

From: <glassfish_at_javadesktop.org>
Date: Wed, 02 Apr 2008 12:10:32 PST

Well, the fog has cleared indeed ;-)

The approach needs some adjustment on the servlet part - but other than that I think it is reasonable.

I think two things have to be fleshed out in more detail: The way your webapp communicates with the C-app and the way your servlet(s) can interact.

Communication between webapp and C-app:
I do not have any experience with JNI but AFAIK there still are three possibilities:
->The C-app calls callback methods in the Java code via JNI. According to your post you do not have control over the library so this option might not be possible at all. If it is possible it would allow to get the results of all measurements made until a certain point of time even when the C-app hasn't completed all operations.
-> The C-app returns a result-object via JNI. The result of all measurements is only available at the end which according to your post might last for some while. I would try to avoid that if possible.
-> The C-app writes to a file and your Servlet(s) read from this file any results already available.

Servlet-interaction:
For simplicity reasons I assume that for each remote task your client wants to be executed on the webserver a dedicated servlet is used.
Your client-app calls (e.g. via the HttpClient library) the servlet on the webserver that starts the C-app. The servlet calls "startService()" in a seperate thread and returns immediately.
Your client-app polls the server (e.g. once every second) by calling a servlet that calls "getServiceStatus()". This method could either read the file (for the third option given above) and report back any measurements that already have been made or return a status that was set earlier by the callback methods. The results can be reported back in XML, CSV or whatever format you like. After sending this response the servlet is done and gone.
You client-app might also invoke a third servlet on the webserver to stop the C-app by invoking "stopService()". This servlet also returns immediately.

Now since you seem to be a servlet novice some infos about these. Each servlet can be invoked multiple times. Concurrently and in succession. So it would be possible for more than just one client to poll the server for results. _But_ because of this you also must assure that the C-app is started only once.
One request is totally unrelated to another request. Even two requests for the same servlet. To exchange data between servlets you can use the session or the application scope or (normally a bad idea) instance variables of your servlet. I think for what you've outlined here the application scope might be the best (which is pretty rare). Only thereby you could stop a process another client has started.

I hope this helps and we might be able to find a viable approach for your app.
[Message sent by forum member 'writtmeyer' (writtmeyer)]

http://forums.java.net/jive/thread.jspa?messageID=267396