Monday, November 12, 2012

Running long running JSP using a thread implementation


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 = (HashMap) TestServletWorker.workerMap.get(sessionId);
threadDetails.put("time", new Long( System.currentTimeMillis()));
}

Thread.sleep(1000 * 60 * 3); // implement the business logic

synchronized (TestServletWorker.workerMap) {
HashMap threadDetails = (HashMap) TestServletWorker.workerMap.get(sessionId);
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();
}
}
}

Create Environment variables in WebSphere 6

Define your websphere variable someVariable = someValue Go to (something like) Servers -> Server Types -> Websphere application servers -> YOUR_SERVER -> Java and process management -> Process definition -> Java virtual machine -> Custmo properties Define a new variable someVariable = ${someVariable}