Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for existing Quartz.net schedulers?

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?

like image 540
Dean Kuga Avatar asked Aug 31 '25 03:08

Dean Kuga


2 Answers

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

like image 160
jvilalta Avatar answered Sep 04 '25 00:09

jvilalta


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

like image 23
LeftyX Avatar answered Sep 04 '25 00:09

LeftyX