Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to dispose of Quartz.NET?

I am using Quartz.NET in an application. What is the proper way to dispose of Quartz.NET.

Right now I am just doing

    if (_quartzScheduler != null)
    {
        _quartzScheduler = null;
    }

Is that enough or should I implement a dispose or something in the jobType class?

Seth

like image 278
Seth Spearman Avatar asked Jun 07 '10 19:06

Seth Spearman


People also ask

What is quartz net?

Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. It's an old staple of many ASP.NET developers, used as a way of running background tasks on a timer, in a reliable, clustered, way.

How many jobs can quartz handle?

The actual number of jobs that can be running at any moment in time is limited by the size of the thread pool. If there are five threads in the pool, no more than five jobs can run at a time.

What is misfire in quartz?

A misfire occurs if a persistent trigger “misses” its firing time because of the scheduler being shutdown, or because there are no available threads in Quartz's thread pool for executing the job. The different trigger types have different misfire instructions available to them.

How do you stop a quartz job in Java?

deleteJob(jobKey(<JobKey>, <JobGroup>)); This method will only interrupt/stop the job uniquely identified by the Job Key and Group within the scheduler which may have many other jobs running. scheduler. shutdown();


2 Answers

scheduler.Shutdown(waitForJobsToComplete: true);

Of course, if you're not on C# 4.0 yet, named parameters don't work:

scheduler.Shutdown(true);
like image 132
jvilalta Avatar answered Nov 15 '22 19:11

jvilalta


This is not a complete example but might get you on the right path. I would implement something like this:

class customSchedulerClass : IDisposable
{

    private Component component = new Component();
    private bool disposed = false;

    public void scheduleSomeStuff()
    {
        //This is where you would implement the Quartz.net stuff
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SupressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if(!this=disposed)
        {
            if(disposing)
            {
                component.dispose;
            }
        }
        disposed = true;
    }       
}

Then with this you can do cool stuff like using statements:

public static void Main()
{
    using (customSchedulerClass myScheduler = new customSchedulerClass())
    {
        c.scheduleSomeStuff();
    }
    console.WriteLine("Now that you're out of the using statement the resources have been disposed");
}

So basically by implementing you code while inheriting the functionality of IDisposable you can then us the using statement and when you're done it will cleanly dispose your resources and keep things nice and clean. (Disclaimer, again this is not a complete example, just to get you in the right direction).

like image 27
Tim C Avatar answered Nov 15 '22 17:11

Tim C