我正在尝试为我的网站维护任务运行Quartz.net服务器。
我在WCF应用程序(托管在IIS中)中创建作业和触发器。因此它们可以存储在数据库(SQL Server)中。

现在我无法理解ADO.NET作业存储。
这是我的Quartz.net的web.config部分:

<configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<quartz>
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
        <add key="quartz.threadPool.threadCount" value="10" />
        <add key="quartz.threadPool.threadPriority" value="Normal" />
        <add key="quartz.jobStore.misfireThreshold" value="60000" />
        <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
        <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.**SqlServerDelegate**, Quartz" />
        <add key="quartz.jobStore.tablePrefix" value="QRTZ_" />
        <add key="quartz.jobStore.dataSource" value="ConnectionString" />
        <add key="quartz.dataSource.ConnectionString.connectionString" value="Server=*;Database=*;Uid=*;Pwd=*" />
        <add key="quartz.dataSource.ConnectionString.provider" value="SqlServer-20" />
        <add key="quartz.scheduler.instanceName" value="PaymentService" />

        <add key="quartz.jobStore.useProperties" value="true" />
</quartz>


这是我的global.asax:

public class Global : System.Web.HttpApplication
    {
        public static ISchedulerFactory Factory;
        public static IScheduler Scheduler;

        protected void Application_Start(Object sender, EventArgs e)
        {
            Factory = new StdSchedulerFactory();
            Scheduler = Factory.GetScheduler();

            JobKey JobKey = new JobKey("GetOrderInfoJob", "Project");
            if (Scheduler.CheckExists(JobKey))
                Scheduler.DeleteJob(JobKey);

            IJobDetail job = JobBuilder.Create<PaymentServiceLogic>()
                .WithIdentity(JobKey)
                .StoreDurably()
                .RequestRecovery()
                .Build();

            TriggerKey triggerKey = new TriggerKey("GetOrderInfoTrigger", "Project");

            TriggerBuilder tb = TriggerBuilder.Create();
            tb.WithIdentity(triggerKey );
            tb.WithSimpleSchedule(a => a.WithIntervalInMinutes(1));
            tb.StartNow();
            tb.ForJob(job);
            ITrigger trigger = tb.Build();
            Scheduler.ScheduleJob(trigger);

            Scheduler.Start();
        }
}


一旦我在本地主机上启动WCF服务,SQL Server中用于作业的QRTZ_JOB_DETAILS表就有1个条目,但是没有触发器。
我已经对该代码进行了几次测试,现在没有任何作业正在存储,因此我有以下异常:
无法存储触发器作业:触发器引用的作业不存在。

Job的构建或AdoJobStore是否存在一些错误?

第二个问题是关于如何在global.asax中正确关闭。现在,我决定了这种方法:

protected void Application_End(object sender, EventArgs e)
        {
            ICollection<IScheduler> all = Factory.AllSchedulers;
            foreach (IScheduler item in all)
            {
                item.Shutdown(true);
            }
        }


并在Application_Error中实现我自己的日志记录。

最佳答案

请注意,确定同时提出两个问题是否是一个好主意,但第一个问题的答案是您需要进行更改

Scheduler.ScheduleJob(trigger);




Scheduler.ScheduleJob(job, trigger);


前者是先前已将作业添加到调度程序中的时间,而后者是同时添加了作业和触发器的时间。

关于c# - Quartz.net JobStoreTX作业保存问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17026143/

10-13 06:20
查看更多