3

Recently upgraded my embedded Quartz.net scheduler to 2.x at which point I had to give up on giving my Zero Thread Schedulers unique names and now I have a problem that (very rarely) an object trying to create an instance of ZT scheduler throws an exception because another object already has an instance of ZT scheduler instantiated and since all my ZT schedulers now have the default 'QuartzScheduler' name this throws an exception...

I tried checking for scheduler count using MySchedFactory.AllSchedulers.Count after calling MySchedFactory = new StdSchedulerFactory(properties) but the StdSchedulerFactory creates an instance of ZT scheduler as soon as it's instantiated and not when GetScheduler() method is called so that is a dead end...

I could not find any other way of checking for existing schedulers before instantiating the StdSchedulerFactory and, as I mentioned already, as soon as it is instantiated, it creates an instance of ZT scheduler so I ended up using a while loop in my catch block which is just a horrible solution so I'm hoping someone knows a better way of checking for existing ZT schedulers...

        try
        {                
            //setting properties
            MySchedFactory = new StdSchedulerFactory(properties);
            BaseScheduler = schedFactory.GetScheduler();
        }
        catch (Exception ex)
        {
            var exMsg = ex.InnerException == null ?
                    ex.Message :
                    ex.Message + Environment.NewLine + ex.InnerException.Message;
            while (exMsg.Contains("Scheduler with name 'QuartzScheduler' already exists"))
            {
                try
                {
                    MySchedFactory = new StdSchedulerFactory(properties);
                    BaseScheduler = schedFactory.GetScheduler();
                }
                catch (Exception vex)
                {
                    exMsg = vex.InnerException == null ?
                        vex.Message :
                        vex.Message + Environment.NewLine + vex.InnerException.Message;
                }                    
            }
        }

Any ideas?

Community
  • 1
  • 1
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108

2 Answers2

2

How about keeping a reference to the scheduler factory as a singleton instead of creating a new one?

jvilalta
  • 6,679
  • 1
  • 28
  • 36
  • Since you cannot pass anything into the Execute method how are quartz jobs implementing the IJob interface that also need to schedule other jobs supposed to get this reference? – Dean Kuga Apr 09 '13 at 23:33
  • MySchedulerFactorySingleton.GetInstance() where MySchedulerFatorySingleton is a public static class. – jvilalta Apr 12 '13 at 22:45
1

Quartz.net scheduler must be singleton. You can read something here.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • If that is the case how are jobs implementing the IJob interface supposed to schedule other jobs? As you know, you can't pass anything into the Execute method... – Dean Kuga Apr 09 '13 at 23:31
  • You can use dependency injection http://asmiki.wordpress.com/2011/06/02/quartz-net-ijob-dependency-injection-with-structuremap/ – LeftyX Apr 10 '13 at 08:56