可以立即触发每周触发器然后每周触发吗?

发布于 2024-11-29 21:53:57 字数 279 浏览 0 评论 0原文

我想知道是否可以使用每周触发器触发一次,然后转到每周计划

Trigger trigger = TriggerUtils.MakeWeeklyTrigger(
   "TriggerName",
   dayOfweek, 
   timeOfDay.Hours,
   timeOfDay.Minutes);

所以我基本上希望当我的服务第一次启动触发器时触发并运行它的工作。之后,我希望它根据用户设置每周工作一次(所以也许在第一次运行后,用户将其设置为星期一上午 5:00)。

I am wondering if it is possible to use weekly trigger to fire once and then go to a weekly schedule

Trigger trigger = TriggerUtils.MakeWeeklyTrigger(
   "TriggerName",
   dayOfweek, 
   timeOfDay.Hours,
   timeOfDay.Minutes);

So I basically want when my service first starts my trigger to fire and run it's job. After that I want it to work on a weekly basis based on user settings(So maybe after first run a user set it to Monday 5:00am).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-06 21:53:57

您可以将多个触发器与一项作业关联。一个将立即触发,并且仅触发一次,另一个将每周触发:

var jobDetail = new JobDetail("MyJob", "MyGroup", typeof(T));

// run in ten seconds, only once.
var once = new SimpleTrigger("Once",
                             null,
                             DateTime.UtcNow.AddSeconds(10),
                             null,
                             0,
                             TimeSpan.Zero); 

// run weekly, start in 5 minutes to give 'once' trigger some time to run
Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
    "Weekly", 
    dayOfweek, 
    timeOfDay.Hours, 
    timeOfDay.Minutes);
weekly.StartTimeUtc = DateTime.UtcNow.Add(TimeSpan.FromMinutes(5));

once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;
once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;

_quartzScheduler.ScheduleJob(jobDetail, once);
_quartzScheduler.ScheduleJob(weekly);

或者您可以安排立即触发,并将触发时间重新安排为每周:

private static volatile Boolean firstRun = false;

public void Execute(JobExecutionContext context) {
    if(firstRun) {
        Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
            "Weekly", 
            dayOfweek, 
            timeOfDay.Hours, 
            timeOfDay.Minutes);

        context.Scheduler.RescheduleJob("Once", "MyGroup", weekly);
        firstRun = false;
    }
}

另一种选择是在启动时运行代码,而不涉及 Quartz。只需使用线程池即可。然后让 Quartz 只处理每周的日程安排。如果您的代码不依赖于 Quartz,这是可能的,这通常是一个好主意。

You can have multiple triggers associated with one job. One would fire immediately and only once and the other will fire weekly:

var jobDetail = new JobDetail("MyJob", "MyGroup", typeof(T));

// run in ten seconds, only once.
var once = new SimpleTrigger("Once",
                             null,
                             DateTime.UtcNow.AddSeconds(10),
                             null,
                             0,
                             TimeSpan.Zero); 

// run weekly, start in 5 minutes to give 'once' trigger some time to run
Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
    "Weekly", 
    dayOfweek, 
    timeOfDay.Hours, 
    timeOfDay.Minutes);
weekly.StartTimeUtc = DateTime.UtcNow.Add(TimeSpan.FromMinutes(5));

once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;
once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;

_quartzScheduler.ScheduleJob(jobDetail, once);
_quartzScheduler.ScheduleJob(weekly);

Or you can schedule immediate trigger and when it fires reschedule to weekly:

private static volatile Boolean firstRun = false;

public void Execute(JobExecutionContext context) {
    if(firstRun) {
        Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
            "Weekly", 
            dayOfweek, 
            timeOfDay.Hours, 
            timeOfDay.Minutes);

        context.Scheduler.RescheduleJob("Once", "MyGroup", weekly);
        firstRun = false;
    }
}

Another option is to just run your code on start up without involving Quartz. Just use ThreadPool. And then let Quartz handle weekly schedule only. This is possible if your code does not depend on Quartz which might be a good idea in general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文