我正在运行这个quartz-2.1.0\examples\src\main\java\org\quartz\examples\example3
示例代码,它运行得很好,但是如果我将main
代码CronTriggerExample.java
移到一个 junit 测试类中,该作业就不会运行。以下是石英示例代码(为简化起见,我将它们截断,您可以从石英网站获取完整代码)。
SimpleJob.java:
public class SimpleJob implements Job {
private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);
public void execute(JobExecutionContext context) throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
_log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
}
}
CronTriggerExample.java:
public class CronTriggerExample {
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(CronTriggerExample.class);
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = newJob(SimpleJob.class)
.withIdentity("job1", "group1")
.build();
CronTrigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0/3 * * * * ?"))
.build();
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getKey() + " has been scheduled to run at: " + ft
+ " and repeat based on expression: "
+ trigger.getCronExpression());
sched.start();
}
public static void main(String[] args) throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
}
}
上面的代码运行良好,如果我将方法中的两行代码移动main
到 junit 测试类(junit4),如下所示:
public class Test1 {
@Test
public void run() throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
}
}
作业没有运行。
我很困惑为什么相同的代码不能在junit中运行?