9

I've seen in this tutorial section of the Quartz.NET documentation that it should be possible to define the maximum number of threads the Quartz scheduler is going to use. In my special case I want to set this number to 1. But in the API doc I couldn't find a way to access the threadpool instance my scheduler is using and to set any properties on it.

Currently my code looks like this:

ISchedulerFactory schedFact = new StdSchedulerFactory();

IScheduler scheduler = schedFact.GetScheduler();
scheduler.Start();

// Setup jobs and triggers and then call scheduler.ScheduleJob...

Does somebody know how I can set the number of threads in the pool?

Thanks for help in advance!

walen
  • 7,103
  • 2
  • 37
  • 58
Slauma
  • 175,098
  • 59
  • 401
  • 420

3 Answers3

31

You can do this programmatically with the code below if you don't want to rely on the external quartz.config file for whatever reason:

    var properties = new NameValueCollection { {"quartz.threadPool.threadCount", "1"} };

    var schedulerFactory = new StdSchedulerFactory(properties);
    var scheduler = schedulerFactory.GetScheduler();

I agree with the comments in the accepted answer though that in this case you probably want to use [DisallowConcurrentExecutionAttribute] on your IJob class instead.

Scott Lerch
  • 2,620
  • 1
  • 23
  • 34
  • 1
    I have done this, but when my IJob class dont have `[DisallowConcurrentExecutionAttribute]` and I use a sleep in my job, like [this sample](http://pastebin.com/kzyJYqaj) you see `int a` multiple time before first job ends. that is because when my thread goes to sleep it began to do another scheduled job(**still there is only one thread here**). this was some thing I didnt understand at first and I though may help others, so I leave this comment here :D – Lrrr Jun 25 '15 at 08:41
3

It depends a bit on the pool you're using and the config file the scheduler is reading. But if you are using the standard SimpleThreadPool.cs then the amount of threads can be configured inside the quartz.config file, by default 10 threads are created:

alt text

jdecuyper
  • 3,934
  • 9
  • 39
  • 51
  • 2
    In the meantime I found another solution for my purpose: I have only one JobDetail with many parametrized triggers and with setting the ThreadCount to 1 I wanted to make sure that the jobs don't run concurrently. (My Execute method isn't threadsafe.) But the more straightforward-way for this seems to be to implement `IStatefulJob` instead of IJob what I have done now (seems to work as I want). Anyway: You definitely answered my question and it's good to know for the future that there is a config file (which I didn't know until now) and what I can do with it. Thank you! – Slauma Nov 05 '10 at 18:47
  • You're welcome! Bit you are right, implementing the IStatefulJob interface is the best solution to your requirement. – jdecuyper Nov 05 '10 at 19:11
  • 7
    @Slauma: The Quartz.Net 2.x way to do that is to decorate your class implementing `IJob` with `[DisallowConcurrentExecutionAttribute]` – Eric J. Mar 26 '13 at 19:16
  • @EricJ.: Thank you! Good to know for the next project with Quartz. – Slauma Mar 26 '13 at 20:09
  • @jdecuyper Was "Quartz.Simpl.SimpleThreadPool, Quartz" wasl "Simpl" a typo? – tdbeckett Dec 19 '16 at 16:17
  • I thought so but it is actually correct: http://quartznet.sourceforge.net/apidoc/2.0/html/html/9a91d01c-9221-f396-cd85-1827bfa5db39.htm – jdecuyper Dec 20 '16 at 18:46
0

In the web.config file add below value under quartz section.

<add key="quartz.threadPool.threadCount" value="20" />

Value represents the number of threads that are available for concurrent execution of jobs.

Tareq
  • 1,397
  • 27
  • 28
Yuvraj
  • 383
  • 1
  • 3
  • 9