关键思想是对“周一和周二上午 10:00”使用 cron 触发器。由于该 cron 触发器每周都会触发,因此您必须自己实现“每两周”部分。因此,为了计算某个日期后的下一次触发时间,您可以让 cron 触发器为您提供下一次触发时间,并检查您是否处于正确的周内。如果不是,您添加一个偏移量以获得正确的星期。
这一切都可以通过重写 AbstractTrigger 来完成。作为示例,我将展示关键方法之一 getFireTimeAfter:
public class MyTrigger extends AbstractTrigger<MyTrigger> {
//your cron trigger for Monday and Tuesday at 10:00am
private OperableTrigger trigger;
//initialize this with the first fire time in method computeFirstFireTime
private Date firstFireTime;
//fire every N weeks
private int nboWeeks;
//...
@Override
public Date getFireTimeAfter(Date afterTime) {
//Get the fire time of the cron trigger after afterTime
Date cronFireTime = trigger.getFireTimeAfter(afterTime);
//we have to determine whether we're in the correct week
int weeksSinceFirstFire = ...; //calculate the nbo weeks since firing for the first time
int weekOffset = weeksSinceFirstFire % nboWeeks;
//we won't fire any more or are in the correct week
if (cronFireTime == null || weekOffset == 0) {
return cronFireTime;
}
int weeksToAdd = weeksSinceFirstFire - weekOffset;
Date fireTime = ...; // add weeksToAdd to cronFireTime
return fireTime;
}
//...
}
我是一名优秀的程序员,十分优秀!