Quartz Scheduler无法立即启动

时间:2015-06-07 16:24:09

标签: quartz.net

创建New Quartz之后(Quartz Enterprise Scheduler .NET) 使用Cron计划,NextFireTimeUtc将在一小时后开始。如何设置它立即启动?

TriggerBuilder triggerBuilder = TriggerBuilder
            .Create()
            .WithIdentity(TriggerName, GroupeId)
            .WithPriority(1)
            .StartNow();


        StringBuilder interval;

        string cronExpression = default(string);

        interval = new StringBuilder();

        switch (ruleScheduler.Schedulers.IntervalUnitType)
        {
            case Scheduler.IntervalUnitTypeInfo.Minute:
                {
                    interval.Append(string.Format("0 0/{0}", ruleScheduler.Schedulers.Interval));
                    break;
                }
            case Scheduler.IntervalUnitTypeInfo.Second:
                {
                    interval.Append(string.Format("0/{0} 0", ruleScheduler.Schedulers.Interval));
                    break;
                }
        }

        if (ruleScheduler.Schedulers.StartTimeOfDay.HasValue)
        {
            interval.Append(string.Format(" {0}", ruleScheduler.Schedulers.StartTimeOfDay.Value.Hour));

            if (ruleScheduler.Schedulers.EndTimeOfDay.HasValue)
            {
                interval.Append(string.Format("-{0}", ruleScheduler.Schedulers.EndTimeOfDay.Value.Hour));
            }
        }
        else
        {
            interval.Append("*");
        }

        string dayOfWeek = string.IsNullOrEmpty(ruleScheduler.Schedulers.DaysOfWeek) ? "?" : ruleScheduler.Schedulers.DaysOfWeek.ToString();

        cronExpression = string.Format("{0} * * {1} *", interval.ToString(), dayOfWeek);

        triggerBuilder.WithCronSchedule(cronExpression);

        trigger = triggerBuilder.Build();

1 个答案:

答案 0 :(得分:0)

首先,您应该重写您的触发构建代码,如下所示:

TriggerBuilder triggerBuilder = TriggerBuilder
            .Create()
            .WithIdentity(TriggerName, GroupeId)
            .WithPriority(1)
            .WithCronSchedule(cronExpression)
            .Build();

然后将触发器传递给调度程序:

scheduler.scheduleJob(job, triggerBuilder );