开发者

Possible to fire a weekly trigger immediately then on a weekly basis?

I am wondering if it is possible to use weekly trigger开发者_JAVA百科 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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜