简体   繁体   English

在.net中配置quartz.net调度程序

[英]Configuring quartz.net scheduler in .net

I have an application in .Net framework and I'm using quartz scheduler. 我在.Net框架中有一个应用程序,我正在使用石英调度程序。 I need to configure quartz. 我需要配置石英。 Now I have one method which is fired every 15 minutes. 现在我有一种方法,每15分钟一次。 These method is used to do some work with database. 这些方法用于对数据库进行一些工作。 I want, in case, that work of procedure is complete, then start waiting period and after that period again start these database method. 我希望,万一程序的工作完成,然后开始等待期,然后再次启动这些数据库方法。 For procedure there will be maximum time which cannot be longer. 对于程序,将有最长的时间,不能再长。 For examplpe 60 minutes. 例如60分钟。 Do you have any ideas how to configure length of working procedure, how to stop when work is finished and how to define waiting time between? 您是否有任何想法如何配置工作程序的长度,如何在工作完成时停止以及如何定义两者之间的等待时间?

// configure Quartz
var stdSchedulerProperties = new NameValueCollection
{
    { "quartz.threadPool.threadCount", "10" },
    { "quartz.jobStore.misfireThreshold", "60000" }
};
var stdSchedulerFactory = new StdSchedulerFactory(stdSchedulerProperties);
var scheduler = stdSchedulerFactory.GetScheduler().Result;
scheduler.Start();

// create job and specify timeout
IJobDetail job = JobBuilder.Create<JobWithTimeout>()
    .WithIdentity("job1", "group1")
    .UsingJobData("timeoutInMinutes", 60)
    .Build();

// create trigger and specify repeat interval
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x.WithIntervalInMinutes(15).RepeatForever())            
    .Build();

// schedule job 
scheduler.ScheduleJob(job, trigger).Wait();

/// <summary>
///     Implementation of IJob. Represents the wrapper job for a task with timeout
/// </summary>
public class JobWithTimeout : IJob
{
    public Task Execute(IJobExecutionContext context)
    {
        return Task.Run(() => Execute(context));
    }

    public void Execute(IJobExecutionContext context)
    {
        Thread workerThread = new Thread(DoWork);
        workerThread.Start();

        context.JobDetail.JobDataMap.TryGetValue("timeoutInMinutes", out object timeoutInMinutes);
        TimeSpan timeout = TimeSpan.FromMinutes((int)timeoutInMinutes);
        bool finished = workerThread.Join(timeout);
        if (!finished) workerThread.Abort();
    }

    public void DoWork()
    {
        // do stuff
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM