异常
[2021-11-08 17:03:57] [ERROR] ErrorLogger: An error occured instantiating job to be executed. job= 'DEFAULT.helloJob'
org.quartz.SchedulerException: Problem instantiating class 'HelloJob' [See nested exception: java.lang.IllegalAccessException: Class org.quartz.simpl.SimpleJobFactory can not access a member of class HelloJob with modifiers ""]
at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:58)
at org.quartz.simpl.PropertySettingJobFactory.newJob(PropertySettingJobFactory.java:69)
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:392)
Caused by: java.lang.IllegalAccessException: Class org.quartz.simpl.SimpleJobFactory can not access a member of class HelloJob with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:56)
... 3 more
[2021-11-08 17:03:57] [INFO] RAMJobStore: All triggers of Job DEFAULT.helloJob set to ERROR state.
错误代码
public class Test05 {
public static void main(String[] args) throws SchedulerException {
// 创建一个JobDetail实例,并且与HelloJob任务绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("helloJob").build();
// 创建一个Trigger触发器实例,并且定义该job立即执行,并且每2秒执行一次,一直执行
SimpleTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity("helloTrigger")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.build();
// 创建Schedule实例
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
}
}
/**
* 创建任务
*/
class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
// 打印当前的时间
System.out.println("当前的时间是:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// 具体在定时任务中要执行的操作,比如打印一段话
System.out.println("Hello Quartz.");
}
}
原因
其实提示信息Class SimpleJobFactory can not access a member of class HelloJob with modifiers ""
说得很明白了,即HelloJob类的修饰符不正确。
由于需要写两个类,出于方便的考虑,把两个类放在一个java文件中,而一个java文件中只允许有一个public
修饰符修饰的类,所以我就去掉了HelloJob类的public
修饰符。
解决
将两个类分别写到两个java文件中,都使用public
修饰符来修饰类。
正确代码
Test05.java
/**
* 执行任务(或叫触发任务)
*/
public class Test05 {
public static void main(String[] args) throws SchedulerException {
// 创建一个JobDetail实例,并且与HelloJob任务绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("helloJob").build();
// 创建一个Trigger触发器实例,并且定义该job立即执行,并且每2秒执行一次,一直执行
SimpleTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity("helloTrigger")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.build();
// 创建Schedule实例
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
}
}
HelloJob.java
/**
* 创建任务
*/
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
// 打印当前的时间
System.out.println("当前的时间是:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// 具体在定时任务中要执行的操作,比如打印一段话
System.out.println("Hello Quartz.");
}
}