File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets & global stateful in-memory objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets & global stateful in-memory objects" Watch "Servlets & global stateful in-memory objects" New topic

Servlets & global stateful in-memory objects

Keith Tingle

Joined: Jan 12, 2004
Posts: 22
I have a web application that basically acts as a batch process starter.

I have long batch processes that I dispatch off to threads, the servlet call that kicks this process off returns after setting up the batch process and creating the 'worker' thread.

So long after the servlet has done its HTTP work, the worker thread the servlet created is running its little heart out processing the batch job.

I would like my users to be able to visit a JSP or servlet page and check on the thread, perhaps get a quick report on what record the batch is processing, maybe cancel the job if needed etc...

It occurs to me that the best way to this is some kind of in-memory hash table with an auto-generated key that I return to my users. If they want status, they ask for it with the key, I use the key to look up the Runnable object and query for status.

Where is the best place to keep this hash table? It basically needs to be a global object accessible by servlets and my worker thread alike (for clean up).

Is there a standard approach to this kind of global object? I assume JNDI and a servlet context is the normal place for this sort of thing, are there any drawbacks?

SCJP - 8/31/2004
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
The ServletContext would be a logical place to store things like this.

It's available to all servlets (and thus JSPs) running in the web application.

I agree. Here's the link:
subject: Servlets & global stateful in-memory objects
It's not a secret anymore!