Using threads in a servlet

Dear friends,If I have a very time consuming task to do in doPost method can I execute that task in a thread. Can I use session, request and response objects in that thread.Will it lead to any problems.Thanking you,Chamal.
[264 byte] By [chamalsla] at [2007-10-2 3:28:25]
«« Layers
»» question
# 1

Hi:

The session object is the candidate to use in the thread, but be carefull and consider that it can be accesed by other servlets and jsp in the application.

In my opinion don磘 use request or response objects, take all info and put it in "temporal" objects , and the Thread must use these temporal objects. Then if you must send some info to the user, use the session object, but again be carefull.

Regards

Gabriel.

Gabriel_Garciaa at 2007-7-15 22:38:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> Dear friends,

>

> If I have a very time consuming task to do in doPost

> method can I execute that task in a thread.

>

> Can I use session, request and response objects in

> that thread.

> Will it lead to any problems.

>

> Thanking you,

> Chamal.

Bad idea to do it in a servlet at all, IMO.

A thread won't change the asynchronous behavior of your long running task.

HTTP is a synchronous, request/response protocol. I'd have the servlet hand the task off to another object to complete (or send a message queue) and send a response to the user telling them to check back for the result later or that their request is being processed or something like that.

Unless the user cannot proceed until the response comes back, forcing you to a synchronous request, I'd do it that way.

In either case, move the processing out of the servlet itself.

%

duffymoa at 2007-7-15 22:38:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
if you do start a thread, make sure you stop it when your servlet gets stopped.(override GenericServlet#destroy() if im not mistaken)otherwise you might end up with a pack of idle ownerless threads, if you restart just the webapp and not the whole server(jvm).
TheosTheona at 2007-7-15 22:38:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

You should not use the Request or Response objects in the separate thread, as these are typically pooled resources in the servlet container. Consider the following scenario:

1. User request 1 hits the servlet container.

2. Servlet container gets an unused Request object from the Request object pool and marks it as being in use. It clears out any old data and initializes it with the data for the current request.

3. (Ditto for Response object)

4. Servlet container gets an unused thread from the thread pool and marks it as being in use.

5. The servlet passed the Request, Response, and Servlet to the thread to process (the run( ) method calls service( request, response ))

6. Your servlet spawns another thread and finishes quickly

7. The servlet container reclaims the Request and Response object, marking them as unused and putting them back in the pool.

8. Another request comes in and gets the same Request and Response objects. They are cleared out and initialized with data for this request.

(*Note: HttpSessions are immune to this by nature--they cannot be reset between requests)

If your thread tries to access data from the request at or after (8) then it will be the wrong data. What you should to is extract the the data from the request and pass that to your thread. This is a good practice anyway. . . it sounds like your thread is executing background business logic and it is never a good idea to pass UI-specific objects (Request, Response, Session) to the model (business layer) as this creates a tight coupling of the presentation to the business layer.

If your thread must change your view, then you need to either:

1) Use <META HTTP-EQUIV="refresh" value="http://host:port/context/servlet; n"> to refresh the page every n seconds

2) Provide the user some way to periodically check (refresh) to see if the process has completed

3) Use an asynchronous view technology such as AJAX (XMLHttpRequest) to "push" updates from the controller to the view.

Hope that helps.

jsweetlanda at 2007-7-15 22:38:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...