This works well in a proxy-ed environment with a session affinity to a JVM from the webtier.
Steps involve creating a worker thread which executes the long running process activity. Create a mechanism to look back on the process at regular intervals from the browser for completion. If the process did not complete kill the thread and report back as "timed out". If there are orphaned process kill them with the help of a maintenance thread initiated by a startup servlet.
Here is the example code samples.
class WorkingThread extends Thread implements Runnable {
String sessionId;
public WorkingThread () {
}
public WorkingThread (String sessionId) {
this.sessionId = sessionId;
}
public void run() {
System.out.println(" Working thread");
try {
synchronized (TestServletWorker.workerMap) {
HashMap
threadDetails.put("time", new Long( System.currentTimeMillis()));
}
Thread.sleep(1000 * 60 * 3); // implement the business logic
synchronized (TestServletWorker.workerMap) {
HashMap
threadDetails.put("Data", "my Data --> Done");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopThisThread () {
// Stop application functions here
this.interrupt();
try {
this.join ();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}