views:

240

answers:

3

I have a question in scheduling jobs in web application. If we have to schedule jobs in web application we can either use java util Timer/TimerTask or Quartz(there are also other scheduling mechanism, but I considered Quartz). I was considering which one to use, when i hit the site http://oreilly.com/pub/a/java/archive/quartz.html?page=1 which says using timer has a bad effect as it creates a thread that is out of containers control in the last line. The other pages discuss Quartz and its capabilities, but I can read that Quartz also uses thread and/or threadpool to schedule tasks. My guess is that these threads are also not under the containers control

Can anybody clarify this to me Is it safe to use Quartz in my web applications without creating hanging threads or thread locking issues? Thanks in advance

+1  A: 

Both approaches created unmanaged threads. I use Quartz for scheduling rather than java Timer since it offer more flexability (cron expressions, for example) and it better managable.

Seffi
A: 

If I have to say in one line, I would say use Quartz as it will take care of managing scheduling related low level work for you. With Timer, you can do everything which quartz does (even make timer threads keep polling to check if web app is running and exit otherwise). But this needs to be done in your code by you. With Quartz all this you get out of the box.

Now details Quartz provides 1. Job persistence 2. Managed thread pool so you create appropriate number of threads and make the jobs wait after that. 3. Initialization servlet to be integrated with your web application. When app shuts down, I think it takes care of closing your threads, but I have not tried it. So I would not comment much on it. 4. RMI based scheduling, for clustered environments.

There are others as well, but these ones have been the biggest motivators why people use quartz more frequently.

Fazal
+1  A: 

Can anybody clarify this to me Is it safe to use Quartz in my web applications without creating hanging threads or thread locking issues?

Both quartz and the JDK Timer start unmanaged threads that do not have access to Java EE contextual information, that's the biggest issue. In addition, they can use resources without the [application server] knowing about it, exist without an administrator's ability to control their number and resource usage, and impede on the application server's ability to gracefully shutdown or recover resources from failure (see Unmanaged threads).

Having that said, I didn't face hanging threads or locking issues (I guess it depends on what you're doing with them though).

If really this is a concern, consider using a JSR-237 Timer and WorkManager implementation (that works with managed thread) like Foo-CommonJ instead of quartz or JDK Timer.

Pascal Thivent