I just heard that spawn your own threads in JavaEE container is a bad practice. I have been doing this for a while since I start learning JavaEE 6 development few months ago. What I have been doing was:
1.)From the web application main entry point(object that implements ServletContextListener),
2.)I create couples of threads to run some asynchronous backgrounds tasks for the web application backend in contextInitialized method.
3.)I clean up the resource in contextDestroyed method.
Is there a better way to do that? How to create asynchronous backgrounds tasks without spawning threads? What is the reason to not using thread?
I am using EJB 3.0 and JavaEE6
Spawning off threads manually is indeed a bad practice, since you want your Java container to manage your threadpool for you, external to your application code. JavaEE 6 has an annotation called @Asynchronous specifically for this purpose, and Oracle has an official tutorial.