You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by keif <kf...@dyadem.com> on 2007/07/19 22:41:02 UTC

Interrupting tomcat threads.

Hi,

I'd like to find a way to stop the first request from processing when a
second one is recieved. So the first request comes in and the container
starts a thread to process it. Then the second request comes in and before
it is processed, I want it to cancel processing the first request.

Can this be accomplished by interrupting threads? Should I spawn new threads
for such processing and manage them instead of working with threads given by
the container?

Thank you,

keif.
-- 
View this message in context: http://www.nabble.com/Interrupting-tomcat-threads.-tf4113604.html#a11697335
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Interrupting tomcat threads.

Posted by DJ...@desknetinc.com.
However, even then, you may find that the requests are not as asynchronous 
as you'd like.  If the two HTTP requests underlying the AJAX requests 
happen to be sent over the same HTTP connection, then the HTTP protocol 
dictates that the responses must be returned in the same order, i.e. the 
response for request 2 cannot be returned, until the response for request 
1 has been returned.  If the two HTTP requests happen to be sent over 
separate HTTP connections, you will get the desired behavior.  While 
guidelines indicate 1-4 concurrent connections may be used by a client, 
most browsers will try to minimize the use of separate connections, and 
pipeline some of the requests. 
The bottom line is that you may, or may not, get the asynchronous behavior 
you seek, depending on decisions made within the browser under a given set 
of circumstances.


Please respond to "Tomcat Users List" <us...@tomcat.apache.org>

To:     Tomcat Users List <us...@tomcat.apache.org>
cc:     keif <kf...@dyadem.com> 
Subject:        Re: Interrupting tomcat threads.


On Fri Jul 20 15:23:45 CEST 2007 Tomcat Users List 
<us...@tomcat.apache.org> wrote:
>
>
> Christopher Schultz-2 wrote:
> >
> > So, multiple simultaneous AJAX requests are considered illegal?
> >
>
> Exactly. All requests that modify the session state will be 
synchronized.
>
>
> Christopher Schultz-2 wrote:
> >
> > I would also recommend vetoing the /second/ request, rather than
> > canceling the first request when a second one comes in.
> >
>
> Well, once the requests are synchronized, I'd like to give users an 
ability
> to change their mind and not wait until the first request finishes. That 
is
> the main justification for interrupting threads. It will allow for a 
more
> responsive interaction with the UI. There is an article with a good 
example
> of request synchronization:
> http://www.onjava.com/pub/a/onjava/2004/03/24/loadcontrol.html.
>
> Hope it's clear now why I'm asking about interrupting threads - vetoing 
the
> second request would make the user wait for the first one to complete.
>
The requests aren't synchronized.
Only updating the session is synchronized.
So, you can make multiple ajax requests to servlets if the browser 
supports it.

Ronald.



Re: Interrupting tomcat threads.

Posted by Ronald Klop <ro...@base.nl>.
On Fri Jul 20 15:23:45 CEST 2007 Tomcat Users List <us...@tomcat.apache.org> wrote:
> 
> 
> Christopher Schultz-2 wrote:
> > 
> > So, multiple simultaneous AJAX requests are considered illegal?
> > 
> 
> Exactly. All requests that modify the session state will be synchronized.
> 
> 
> Christopher Schultz-2 wrote:
> > 
> > I would also recommend vetoing the /second/ request, rather than
> > canceling the first request when a second one comes in.
> > 
> 
> Well, once the requests are synchronized, I'd like to give users an ability
> to change their mind and not wait until the first request finishes. That is
> the main justification for interrupting threads. It will allow for a more
> responsive interaction with the UI. There is an article with a good example
> of request synchronization:
> http://www.onjava.com/pub/a/onjava/2004/03/24/loadcontrol.html.
> 
> Hope it's clear now why I'm asking about interrupting threads - vetoing the
> second request would make the user wait for the first one to complete.
> 
The requests aren't synchronized. 
Only updating the session is synchronized.
So, you can make multiple ajax requests to servlets if the browser supports it.

Ronald.

Re: Interrupting tomcat threads.

Posted by keif <kf...@dyadem.com>.

Christopher Schultz-2 wrote:
> 
> So, multiple simultaneous AJAX requests are considered illegal?
> 

Exactly. All requests that modify the session state will be synchronized.


Christopher Schultz-2 wrote:
> 
> I would also recommend vetoing the /second/ request, rather than
> canceling the first request when a second one comes in.
> 

Well, once the requests are synchronized, I'd like to give users an ability
to change their mind and not wait until the first request finishes. That is
the main justification for interrupting threads. It will allow for a more
responsive interaction with the UI. There is an article with a good example
of request synchronization:
http://www.onjava.com/pub/a/onjava/2004/03/24/loadcontrol.html.

Hope it's clear now why I'm asking about interrupting threads - vetoing the
second request would make the user wait for the first one to complete.



 


-- 
View this message in context: http://www.nabble.com/Interrupting-tomcat-threads.-tf4113604.html#a11708046
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Interrupting tomcat threads.

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kief,

keif wrote:
> Christopher Schultz-2 wrote:
>> I assume you mean that you want to cancel an already-running thread /for a
>> given user/ if a
>> second one comes in.
>  
> Yes, that is correct - I'd like to process a single request at a time per
> session. The reason for that is that I have to maintain some state
> information in the session. This state information is then used to
> recieve/send dynamic updates to the client via ajax requests.

So, multiple simultaneous AJAX requests are considered illegal?

> If I let two requests run together then I might have a situation where the
> second request finishes and sends a response, then the first request
> processing finishes and updates the session state. In this case the user
> will see data on screen for the second request, but the server will have
> state for the first request.  

This is generally called a "race condition" and is okay if it is
tolerable for your user interface. I would recommend a healthy mix of
synchronization (to prevent data corruption) and serialization (that is,
putting events in order... not writing object data to streams) to solve
this problem, rather than trashing threads on the server side.

I would also recommend vetoing the /second/ request, rather than
canceling the first request when a second one comes in. You should have
each synchronous request report some kind of "success" state, so that
when the second request fails, the UI doesn't update inappropriately.

> Your reply has some valueable pointers, could you recommend any references
> for the subject of threading and perhaps the way it relates to tomcat? 

Nope, and you shouldn't rely on any information that you /can/ find by
reading Tomcat source code. Instead, stick to conforming to the servlet
spec (which basically says NOT to do what you are doing) and ignore
Tomcat's implementation.

> The way I understand is that I should not be touching the container provided
> thread(request thread), but instead spawn a new thread once the request
> comes in and manage it with Filter that you proposed.

You should not have to spawn any threads or anything like that. You'll
only make your life more complicated by doing that.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGn+Eu9CaO5/Lv0PARAnj7AKC+R6XY2Yxe6J7VprwHSeGJf6lnMQCgrcMo
XUdLMdvlbujhsUPtILewA+c=
=FhJv
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Interrupting tomcat threads.

Posted by keif <kf...@dyadem.com>.

Christopher Schultz-2 wrote:
> 
> I assume you mean that you want to cancel an already-running thread /for a
> given user/ if a
> second one comes in.
> 
 
Yes, that is correct - I'd like to process a single request at a time per
session. The reason for that is that I have to maintain some state
information in the session. This state information is then used to
recieve/send dynamic updates to the client via ajax requests.

If I let two requests run together then I might have a situation where the
second request finishes and sends a response, then the first request
processing finishes and updates the session state. In this case the user
will see data on screen for the second request, but the server will have
state for the first request.  

So this "single request at a time per session" scenario will only apply to
the requests that modify the session state. 

Your reply has some valueable pointers, could you recommend any references
for the subject of threading and perhaps the way it relates to tomcat? 

The way I understand is that I should not be touching the container provided
thread(request thread), but instead spawn a new thread once the request
comes in and manage it with Filter that you proposed.

Thank you for a quick reply,

keif.


-- 
View this message in context: http://www.nabble.com/Interrupting-tomcat-threads.-tf4113604.html#a11698350
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Interrupting tomcat threads.

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kief,

keif wrote:
> I'd like to find a way to stop the first request from processing when a
> second one is recieved. So the first request comes in and the container
> starts a thread to process it. Then the second request comes in and before
> it is processed, I want it to cancel processing the first request.

For the record, I think this is a terrible idea. Why would you want to
stop an existing thread when a new one comes in? (I assume you mean that
you want to cancel an already-running thread /for a given user/ if a
second one comes in).

> Can this be accomplished by interrupting threads? Should I spawn new threads
> for such processing and manage them instead of working with threads given by
> the container?

I think you can do this (but I still don't understand why you'd want
to). You could do something like this:

1. Writer a filter that stores the currently-running thread into the
user's session, then chains to the next filter, then removes the thread
from the session. (Remember to watch exception handling AND check to
make sure that it's the same thread you stored before you remove it!)

2. Modify the filter in #1 to check for an existing thread in the
session. If such a thread exists, grab it, interrupt() it, and then
proceed to store/chain/remove the thread.

A few notes:

1. If you are running Tomcat under a SecurityManager (or maybe even if
you're not!), you might not be able to call interrupt() on another
thread. Beware!

2. Tomcat might misbehave if you call interrupt() on a request
processing thread. Beware!

3. It might be perfectly reasonable for two threads to be executing at
once on behalf of a legitimate client. For instance, the browser decides
to launch two separate requests to request page components instead of
using a single keep-alive connection. Or, you might have some AJAX thing
going on while the main page is loading (or re-loading). In these cases,
you're going to get all kinds of weird errors and your users will be
unhappy.

Again... why do you want to do this?

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGn9MV9CaO5/Lv0PARAh0JAJ9B2VtmQDhGtV5ZQj/PhefLhTZnvQCghh2z
gXjGUHD6Ts3KJRcTjp46QJE=
=D9HM
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org