You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Captain Cid <ca...@indiatimes.com> on 2011/01/09 17:27:48 UTC

Need to create a new thread for async operations

I am having a CXF webservice impl class. In this I want to start a new thread
in service impl constructor. This is because I need to run a thread will
continuously do some operation in background using database. And I need to
be able to stop it when tomcat is shutdown.

I was searching net. Found that it can be done using executor etc. But still
I am unable to find it. Any help appreciated.
-- 
View this message in context: http://old.nabble.com/Need-to-create-a-new-thread-for-async-operations-tp30625163p30625163.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Need to create a new thread for async operations

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

Cid,

On 1/9/2011 11:27 AM, Captain Cid wrote:
> I am having a CXF webservice impl class. In this I want to start a new thread
> in service impl constructor. This is because I need to run a thread will
> continuously do some operation in background using database. And I need to
> be able to stop it when tomcat is shutdown.

If you only need a single thread to run all the time go ahead and create
it in a ServletContextListener (or via a Spring-manager object if you
want). Just remember to stop the thread, later.

> I was searching net. Found that it can be done using executor etc. But still
> I am unable to find it. Any help appreciated.

If you need to run more than one thread, I highly recommend using an
executor: you can submit a job to a ThreadPoolExecutor and it will be
run when there is a free thread. This helps limit your resource usage
even under load: that is, you won't be firing off hundreds of threads if
you get swamped by requests that need to schedule background jobs.
Instead, your job queue simply grows longer.

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

iEYEARECAAYFAk0ra9IACgkQ9CaO5/Lv0PDa4QCeMI3XSO399vP6Hsc040KuB3oo
pFgAoKzVj3Rb1R4bdiiX/NzbABsWWwca
=AFKb
-----END PGP SIGNATURE-----

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


Re: Need to create a new thread for async operations

Posted by Captain Cid <ca...@indiatimes.com>.
Thanks a lot. I was not aware of how to use servletcontextlistener.

- in contextinitialized I am creating my thread and stopping it in
contextdestroyed.
- I needed a spring bean for thread initialization, which I got in
contextinitialized function.



Juha Laiho wrote:
> 
> On 01/09/2011 09:30 PM, Captain Cid wrote:
>> I have moved the thread creation to constructor of a class which is
>> instantiated by spring.
>>
>> Can you elaborate how to spawn thread in it using
>> javax.servlet.ServletContextListener ?
> 
> As for ServlerContextListener, please have a look:
> http://www.google.com/search?btnG=1&pws=0&q=servletcontextlistener+example
> 
> ... there are of course some other things you might be tripping over,
> but unfortunately you didn't tell where it is you're having problems,
> so you're asking us to do the guesswork.
> 
> With the above, you should now have
> - a working ServletContextListener implementation
> - your Spring-initialized class that can create threads
> 
> What appears to be missing here is the bridge between the two,
> where a Google search with two very intuitive keywords concerning
> your issue should bring you very close to the solution.
> -- 
> ..Juha
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Need-to-create-a-new-thread-for-async-operations-tp30625163p30629655.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Need to create a new thread for async operations

Posted by Juha Laiho <Ju...@iki.fi>.
On 01/09/2011 09:30 PM, Captain Cid wrote:
> I have moved the thread creation to constructor of a class which is
> instantiated by spring.
>
> Can you elaborate how to spawn thread in it using
> javax.servlet.ServletContextListener ?

As for ServlerContextListener, please have a look:
http://www.google.com/search?btnG=1&pws=0&q=servletcontextlistener+example

... there are of course some other things you might be tripping over,
but unfortunately you didn't tell where it is you're having problems,
so you're asking us to do the guesswork.

With the above, you should now have
- a working ServletContextListener implementation
- your Spring-initialized class that can create threads

What appears to be missing here is the bridge between the two,
where a Google search with two very intuitive keywords concerning
your issue should bring you very close to the solution.
-- 
..Juha

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


Re: Need to create a new thread for async operations

Posted by Captain Cid <ca...@indiatimes.com>.
I have moved the thread creation to constructor of a class which is
instantiated by spring.

Can you elaborate how to spawn thread in it using
javax.servlet.ServletContextListener ? 


Captain Cid wrote:
> 
> I am having a CXF webservice impl class. In this I want to start a new
> thread in service impl constructor. This is because I need to run a thread
> will continuously do some operation in background using database. And I
> need to be able to stop it when tomcat is shutdown.
> 
> I was searching net. Found that it can be done using executor etc. But
> still I am unable to find it. Any help appreciated.
> 

-- 
View this message in context: http://old.nabble.com/Need-to-create-a-new-thread-for-async-operations-tp30625163p30625746.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Need to create a new thread for async operations

Posted by Michael Ludwig <mi...@gmx.de>.
Captain Cid schrieb am 09.01.2011 um 08:27 (-0800):
> I want to start a new thread in service impl constructor.

That's not necessarily the best place.

> This is because I need to run a thread will continuously do some
> operation in background using database. And I need to be able to stop
> it when tomcat is shutdown.

javax.servlet.ServletContextListener

-- 
Michael Ludwig

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