On Jan 29, 2007, at 4:30 AM, Daniel López wrote:

> The only think is you have to be careful to respect the lifecycle of
> your web application, shutting down the threads when your applications
> shuts down etc.

> I think some containers also offer you the possibility to use a
> container-managed pool of threads that you can use, and they care  
> of the
> rest, but that's container dependent, AFAIK, and I've never used it  
> so I
> have not opinion there ;).

Daniel's response is a good one.

You do need to be careful if you're launching threads, especially in  
taking care of closing them down at web-app restart (GC/classloader  
issues), but also making sure you never pass along request objects  
like ServletRequest/ServletResponse, etc.  So it takes more care and  
work than a straight servlet application, so you should try to figure  
out a simpler method if possible.

In 3.1.0, you can get access to Resin's thread pool with:

    @Resource Executor _threadPool;

In 3.1.1, you'll be able to use:

   @Resource ExecutorService _threadPool;
   @Resource ScheduledExecutorService _timer;

Resin will stop those services on web-app restart, so if you have a  
Future to the runnable task, you can have your task stop at the  
proper time.

At some point, I'll add a tutorial to show how to use the Future  
returned from the schedule() call to make sure your service shuts  
down property.

-- Scott

> In short, with plain servlets, no EJB, it's as simple/complicated  
> as you
> want it.

> Cheers!
> D.
> -------------------------------------------
> Daniel Lopez Janariz ([EMAIL PROTECTED])
> Web Services
> Centre for Information and Technology
> Balearic Islands University
> -------------------------------------------
> Danny Ayers escribió:
>> Hi,
>> I would be grateful is someone could answer these questions:
>> * Can servlets safely spawn threads?
>> * If so, under what conditions?
>> I tried to find the answers searching the web, but found conflicting
>> views. So I thought it worth asking about a specific servlet  
>> container
>> implemention.
>> I'm trying to make a very simple asynchronous messaging system on  
>> top of
>> HTTP. What I have in mind requires that the servlet called would
>> complete the request-response in "reasonable" time, yet may initiate
>> other processes that are potentially long-running. The easiest  
>> approach
>> would be to have the servlet spawning another thread in which to  
>> run the
>> other process, and return a response to the client immediately.  
>> But is
>> this possible without running straight into concurrency breakage?
>> More background at :
>> http://dannyayers.com/2007/01/28/a-servlets-problem
>> Thanks,
>> Danny.
> _______________________________________________
> resin-interest mailing list
> resin-interest@caucho.com
> http://maillist.caucho.com/mailman/listinfo/resin-interest

resin-interest mailing list

Reply via email to