13

I am very new to Quartz and I have some doubts about the jobs lifecycle.

Let's suppose I have a single job configured to do some stuff.

The job fires and ends its work. When it fires again is it the same instance (maybe set to sleep and awaken by the scheduler) or is it a new job instance (once the job ends it is killed and when the trigger condition is met again a new job instance is created)?

I ask such question because when I debug my application (spring 3 mvc with quartz support) I see new instances of the job and new threads with SimpleThreadPool$WorkerThreadRun() opened for every time the job is fired so that the SimpleThreadPool$WorkerThreadRun() threads are piled up and never terminated.

I just want to know if this behaviour is allright or I'm bound to fill the memory ;-)

Can anyone give me some explanation? Thanks in advance.

MaVVamaldo
  • 2,505
  • 7
  • 28
  • 50

2 Answers2

16

Quartz creates new instance of your job class every time it wants to trigger that job. Suppose you have hundreds of thousands of jobs scheduled to trigger very infrequently - it would be a waste of memory to keep all those jobs in memory.

However if you are using Spring support for Quartz, especially the MethodInvokingJobDetailFactoryBean, Spring will handle the lifecycle of your job (it basically calls designated method of one of your beans). But seems not to be the case in your application.

Of course after the job is done and no other references are pointing to it (which is the normal case) garbage collector will eventually release the memory occupied by the job).

Finally about threads - Quartz creates a fixed pool of worker threads (see org.quartz.threadPool.threadCount configuration option). Every time you run a job Quartz may decide to use a different thread - but it won't create new thread per every trigger.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thank you so much. Your explanation is simple and clear and that's what I really needed! Actually in my application I'm using `JobDetailBean`, `CronTriggerBean` and `SchedulerFactoryBean`, but reading your answer I suppose the lifecycle is handled by spring as well, whith no danger of memory faults. Thnks again. – MaVVamaldo May 05 '12 at 16:09
  • About "MethodInvokingJobDetailFactoryBean". Spring will handle the lifecycle of your **bean** and method of this bean will be called from Quartz-job (implements "org.quartz.Job" interface, child of "QuartzJobBean" abstract class in Spring). And this Quartz-job instance will be created as described in my answer. – Vadim Ponomarev May 05 '12 at 16:43
  • Hello @tomasz, could you please tell me how to call the doIt() method (eg from Spring docs) on click of one button from JSP. I am using Quartz job but my execute() method of my Job class is not geting called. I am using MethodInvokingJobDetailFactoryBean with some triggers and jobDetails with SchedulerFactoryBean but they are called on some fix time interval not on demand. Thanks – Jaikrat Jun 19 '15 at 10:35
3

I will write about version 2.1.5 (latest version), but it could also be true for other versions.

Job-instance created by some instance of "JobFactory" with "newJob"-function (SimpleJobFactory, for example). Call to "newJob" executed in "initialize"-method of JobRunShell-class. JobRunShell-object held in local variable of "QuartzSchedulerThread.run" and not stored in any other list or field.

So, new Job-instance created for every trigger time and after execution it will be cleaned up normally by garbage collector.

Vadim Ponomarev
  • 1,346
  • 9
  • 15