You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Balažic <xe...@gmail.com> on 2009/11/16 18:43:58 UTC

Possible to do async processing?

Hi!

We are using tomcat 6.0 and now we have the need to trigger from the
service() method of a  servlet.

So:
 - a request arrives
 - the servlet triggers an async event
 - servlet sends response and closes
 - the async task is done (independent of servlet opeartion)

Is there a way to do this?

Or create threads by hand?

Regards,
David

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


Re: Possible to do async processing?

Posted by Pid <pi...@pidster.com>.
On 16/11/2009 18:19, George Sexton wrote:
> A pretty simple way of doing this is to have your context init create a
> background thread. Have the background thread monitor a queue for jobs.
> Then, have the servlet place the job in the queue.
 >
> The background thread can either process the job itself, or create a worker
> thread to process the job.

As George says, create a queue.  The Java 6 concurrency package is your 
friend here.

  java.util.concurrent.Executors
  java.util.concurrent.ExecutorService

Tomcat doesn't have any built-in functionality for this.


p



> George Sexton
> MH Software, Inc.
> http://www.mhsoftware.com/
> Voice: 303 438 9585
>
>
>> -----Original Message-----
>> From: David Balažic [mailto:xerces9@gmail.com]
>> Sent: Monday, November 16, 2009 10:44 AM
>> To: Tomcat Users List
>> Subject: Possible to do async processing?
>>
>> Hi!
>>
>> We are using tomcat 6.0 and now we have the need to trigger from the
>> service() method of a  servlet.
>>
>> So:
>>   - a request arrives
>>   - the servlet triggers an async event
>>   - servlet sends response and closes
>>   - the async task is done (independent of servlet opeartion)
>>
>> Is there a way to do this?
>>
>> Or create threads by hand?
>>
>> Regards,
>> David
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


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


RE: Possible to do async processing?

Posted by George Sexton <ge...@mhsoftware.com>.
A pretty simple way of doing this is to have your context init create a
background thread. Have the background thread monitor a queue for jobs.
Then, have the servlet place the job in the queue.

The background thread can either process the job itself, or create a worker
thread to process the job.

George Sexton
MH Software, Inc.
http://www.mhsoftware.com/
Voice: 303 438 9585
 

> -----Original Message-----
> From: David Balažic [mailto:xerces9@gmail.com]
> Sent: Monday, November 16, 2009 10:44 AM
> To: Tomcat Users List
> Subject: Possible to do async processing?
> 
> Hi!
> 
> We are using tomcat 6.0 and now we have the need to trigger from the
> service() method of a  servlet.
> 
> So:
>  - a request arrives
>  - the servlet triggers an async event
>  - servlet sends response and closes
>  - the async task is done (independent of servlet opeartion)
> 
> Is there a way to do this?
> 
> Or create threads by hand?
> 
> Regards,
> David
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org



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


Re: Possible to do async processing?

Posted by Elli Albek <el...@sustainlane.com>.
Hi,
The advices given above are good. If you make this job triggered by a
servlet, then an attacker can use it to easily bring down your system
with excessive load.

You also have problems of clean shutdown. The thread pool that you
start needs to have blocking shutdown with some context listener, to
make sure that all jobs are finished when you undeploy or shut down
the server. This is not hard to do, but you should test it by making
sure a job finishes when you shut down. Just add a slow job that, and
kill the server.

Slow job:
public void run(){
System.out.println("Starting slow job");
try{
  Thread.sleep(60000); // 1 min
}catch(Throwable e){
  e.printStachTrace();// possible kill, watch out for this
}
System.out.println("Finished slow job");
}

Add this job to the pool, shut down the server, and look at the logs
for the messages. You should also see the server hanging until the job
is completed.

A simpler option would be to write a java class with a main method,
and run it from cron. We have a bunch of those. Not dependent on
tomcat, no security problems. You can have many tomcats with the same
configuration, something that would be more difficult with a scheduler
that is part of your app.

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


Re: Possible to do async processing?

Posted by André Warnier <aw...@ice-sa.com>.
David Balažic wrote:
> Hi!
> 
> We are using tomcat 6.0 and now we have the need to trigger from the
> service() method of a  servlet.
> 
> So:
>  - a request arrives
>  - the servlet triggers an async event
>  - servlet sends response and closes
>  - the async task is done (independent of servlet opeartion)
> 
> Is there a way to do this?
> 
Hi.
I am not the greatest Java or Tomcat guru here, so I do not even know if 
Java allows you to start an external (think OS-level) task to do something.
But on the face of it, I would think of this kind of scheme :
- a separate daemon is running, whose job it is to execute these async 
tasks. That one listens on some port, and accepts only requests coming 
from your webapp.
- you applet connects to that external server via TCP, sends it the 
request, gets an acknowledgement for the receipt of the request, and 
then proceeds to answer the client.

Almost any introductory book on network programming, in any programming 
language, will have examples of how to create a TCP server program.
Even in Java, I would think.

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


Re: Possible to do async processing?

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

David,

On 11/16/2009 1:04 PM, David Balažic wrote:
> 2009/11/16 Joseph Morgan <jo...@ignitesales.com>:
>> Yes, there is a way, and I suspect you're doing "fire-and-forget"
>> processing, but, could you give us a better idea as to what you are
>> trying to do?
>> 
>> Tomcat will handle servlet requests in multiple threads if needed,
>> anyway.  So it may not be necessary.  I'm thinking you might want
>> to investigate a messaging system, such as OpenMQ.
>> 
>> BTW:  To create a thread by hand, look at the Java docs for the
>> Thread class and the Runnable interface. Keep in mind, this could
>> get really out of hand if there are a lot of requests and you don't
>> understand how to properly manage threads in this environment.
> 
> Yes, that is why I ask. (I know how to create threads).

I would advise you not to create your own threads. Instead, use a thread
pool and queue your jobs into that. 3rd-party thread pools exist, or you
can use the ones available in Java 1.5+ in the java.util.concurrent
package. You can get started by reading the javadoc for
java.util.concurrent.ThreadPoolExecutor and going from there.

> So Tomcat has nothing like this in itself? Any trick , shortcut?

Nope: Tomcat does not provide any services like this.

> It will be something triggered like a few times in an hour and would 
> take less than a minute to process, to there should be no overload 
> problems (famous last words...)

:)

That's why I recommend against managing your own threads: if you do it
wrong, you can build a denial-of-service vector into your webapp. Using
a managed thread pool can help save you from shooting yourself in the foot.

Another option would be to queue the job in a database, then run a
completely separate process to go and process them batch-style. This
solution can even process jobs that were queued but not processed before
you took the server down for some reason.

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

iEYEARECAAYFAksBvL4ACgkQ9CaO5/Lv0PDOYACfbdxU+QpIuwSz06Z0c/HHFUjs
Pq0An3fNU9rGzjc2YZnFrw4E4TY4ngf7
=xAyX
-----END PGP SIGNATURE-----

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


RE: Possible to do async processing?

Posted by Joseph Morgan <jo...@ignitesales.com>.
Sorry.. misunderstood your original when you asked if there was a way to create a Thread by hand....

No tricks/shortcuts.  Are you saying each request will take a minute to process and is triggered a few times an hour?  I suspect the client is anxiously awaiting a response like "OK, I got it", but not patient enough for a "The request was successful."  

I'm still thinking a messaging system, especially if you want to requests to be processed regardless of the time the message was delivered, like, when maybe many requests hit near the same time.   

A quick hack on a messaging system is to simply have the servlet throw a record into a DB, file or something used as a messaging queue.  Then, a program external to Tomcat simply monitors the queue, picks up any existing requests, handles them, and starts monitoring again.  This way, Tomcat could go down without affecting if the messages are processed, or the monitor could be down without affecting if Tomcat can push in new requests.  The only thing that can't go down is the queue.

Joe


-----Original Message-----
From: David Balažic [mailto:xerces9@gmail.com] 
Sent: Monday, November 16, 2009 12:05 PM
To: Tomcat Users List
Subject: Re: Possible to do async processing?

2009/11/16 Joseph Morgan <jo...@ignitesales.com>:
> Yes, there is a way, and I suspect you're doing "fire-and-forget" processing, but, could you give us a better idea as to what you are trying to do?
>
> Tomcat will handle servlet requests in multiple threads if needed, anyway.  So it may not be necessary.  I'm thinking you might want to investigate a messaging system, such as OpenMQ.
>
> BTW:  To create a thread by hand, look at the Java docs for the Thread class and the Runnable interface. Keep in mind, this could get really out of hand if there are a lot of requests and you don't understand how to properly manage threads in this environment.

Yes, that is why I ask. (I know how to create threads).

So Tomcat has nothing like this in itself? Any trick , shortcut?

It will be something triggered like a few times in an hour and would
take less than a minute to process, to there should be no overload
problems (famous last words...)

Regards,
David

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


Re: Possible to do async processing?

Posted by David Balažic <xe...@gmail.com>.
2009/11/16 Joseph Morgan <jo...@ignitesales.com>:
> Yes, there is a way, and I suspect you're doing "fire-and-forget" processing, but, could you give us a better idea as to what you are trying to do?
>
> Tomcat will handle servlet requests in multiple threads if needed, anyway.  So it may not be necessary.  I'm thinking you might want to investigate a messaging system, such as OpenMQ.
>
> BTW:  To create a thread by hand, look at the Java docs for the Thread class and the Runnable interface. Keep in mind, this could get really out of hand if there are a lot of requests and you don't understand how to properly manage threads in this environment.

Yes, that is why I ask. (I know how to create threads).

So Tomcat has nothing like this in itself? Any trick , shortcut?

It will be something triggered like a few times in an hour and would
take less than a minute to process, to there should be no overload
problems (famous last words...)

Regards,
David

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


RE: Possible to do async processing?

Posted by Joseph Morgan <jo...@ignitesales.com>.
Yes, there is a way, and I suspect you're doing "fire-and-forget" processing, but, could you give us a better idea as to what you are trying to do?  

Tomcat will handle servlet requests in multiple threads if needed, anyway.  So it may not be necessary.  I'm thinking you might want to investigate a messaging system, such as OpenMQ.

BTW:  To create a thread by hand, look at the Java docs for the Thread class and the Runnable interface. Keep in mind, this could get really out of hand if there are a lot of requests and you don't understand how to properly manage threads in this environment. 

Joe
 
-----Original Message-----
From: David Balažic [mailto:xerces9@gmail.com] 
Sent: Monday, November 16, 2009 11:44 AM
To: Tomcat Users List
Subject: Possible to do async processing?

Hi!

We are using tomcat 6.0 and now we have the need to trigger from the
service() method of a  servlet.

So:
 - a request arrives
 - the servlet triggers an async event
 - servlet sends response and closes
 - the async task is done (independent of servlet opeartion)

Is there a way to do this?

Or create threads by hand?

Regards,
David

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