【问题标题】:What is the good way to execute JobSchedule with Quartz.Net and Asp.Net application?使用 Quartz.Net 和 Asp.Net 应用程序执行 JobSchedule 的好方法是什么?
【发布时间】:2015-03-25 10:40:52
【问题描述】:

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

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

在我的 global.asax Application_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();
}

这里是我的工作被脚本化的课程:

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);
            }
        }
    }
}

然后我的工作在同一个文件和类 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),间隔时间很长。但是作业没有执行,因为 generateandsendcontract 类中的代码没有被触发(没有断点,没有邮件 GenerateAndSendContract 发送)。

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

【问题讨论】:

  • MyAPI.MvcApplication.scheduler背后的代码是什么?
  • 嗨,这是我的公共静态 IScheduler 调度器;这是在我的 application_start 中发起的

标签: asp.net quartz-scheduler quartz.net


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多