使用 Quartz.Net 和 Asp.Net 应用程序执行 JobSchedule 的好方法是什么?

What is the good way to execute JobSchedule with Quartz.Net and Asp.Net application?

我的 ASp.net 应用程序有问题,我测试了几种解决方案,但没有发现任何问题。预先感谢您的建议。

我已经安装 运行 Quartz.Net 作为 Windows 服务。 Quartz.Net 与我的 API 位于同一文件夹中,以便在其中执行作业。

下面是我的global.asaxApplication_Start

public static ISchedulerFactory schedFact;
public static IScheduler scheduler;

protected void Application_Start()
{
    NameValueCollection properties = new NameValueCollection();
    properties["quartz.scheduler.instanceName"] = "ServerScheduler";
    properties["quartz.scheduler.proxy"] = "true";
    properties["quartz.threadPool.threadCount"] = "10";
    properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler";


    schedFact = new StdSchedulerFactory(properties);
    scheduler = schedFact.GetScheduler();
    scheduler.Start();

    MyAPI.Services.ScheduleTasks.JobScheduler.StartGenerateContract();
}

这里是我的作业脚本所在的class:

public class ScheduleTasks
{
    public static Dictionary<string, string> report;

    public class JobScheduler
    {
        public static void StartGenerateContract()
        {
            try
            {
                MailService.SendMail("StartGenerateContract", "Scheduletask",
                    new Exception(DateTime.Now.ToString()));


                IJobDetail job_generate = JobBuilder.Create<GenerateAndSendContract>()
                    .WithIdentity("generateContractJob", "group1")
                    .Build();

                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger_contracts", "group1")
                    .ForJob("generateContractJob", "group1")
                    .StartNow()
                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(01, 00))
                    .Build();

                MyAPI.MvcApplication.scheduler.ScheduleJob(job_generate, trigger);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}

然后我的作业和class ScheduleTasks

在同一个文件中
public class GenerateAndSendContract : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        try
        {
            MailService.SendMail("GenerateAndSendContract", "Scheduletask");
                //my working code...
        }
        catch (Exception e)
        {
            MailService.SendErrorMail("GenerateAndSendContract", "ScheduleTasks", e);
        }
    }
}

我的 scheduleTask 完美执行,因为我收到了第一封电子邮件 (StartGenerateContract),时间间隔很好。但是作业没有执行导致 class generateandsendcontract 中的代码没有被触发(没有断点,没有邮件 GenerateAndSendContract 发送)。

我的代码有什么问题吗?感谢您的帮助。

在 MVC 应用程序端创建调度程序时,您只需要这些属性:

properties["quartz.scheduler.instanceName"] = "RemoteClient"; 
properties["quartz.scheduler.proxy"] = "true"; 
properties["quartz.threadPool.threadCount"] = "0"; 
properties["quartz.scheduler.proxy.address"] = address;

您也不需要在充当客户端的调度程序上调用 Start 方法。