我到处看了看。我查看了Timers和Quartz,我仍感到困惑。
我有一个庞大的班级表演不同的方法。其中一种方法是同步方法:
public void onSynch()
{
String environment = validateEnvironment(env2);
if (environment == null) return;
try
{
RAT.synchronize(environment);
onSelectEnv();
}
catch (Exception e)
{
// TODO: tell user, log
}
}
我需要能够创建一个计时器,在每个星期五和下午3:00的特定时间每周一次触发此方法。 我无法创建单独的类。
我需要能够将调度程序放在此方法中,或者在其旁边创建另一个创建调度程序并运行该同步方法的方法。
答案 0 :(得分:1)
我建议您使用QUARTZ
或Spring Batch
作为日程安排工作。
请尝试以下Quartz Example
。
参考:CronTrigger Tutorial,Example
YourJob.java
public class YourJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
//here your operation
}
}
MySchedule.java
public class MySchedule {
public void startUp() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
JobDetail yourJob = JobBuilder.newJob(YourJob.class).withIdentity("job1", "group1").build();
/*Run every 30 seconds on Weekdays (Monday through Friday)*/
Trigger trigger_1 = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0,30 * * ? * MON-FRI"))
.build();
/*Run at 10am on the 1st and 15th days of the month*/
Trigger trigger_2 = TriggerBuilder.newTrigger()
.withIdentity("trigger2", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 10am 1,15 * ?"))
.build();
/*Run at 3:30PM of every friday f month*/
Trigger trigger_3 = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 18 30 ? * 6"))
.build();
scheduler.start();
scheduler.scheduleJob(yourJob, trigger_1);
scheduler.scheduleJob(yourJob, trigger_2);
scheduler.scheduleJob(yourJob, trigger_3);
}
}