使用Quartz报错“Class SimpleJobFactory can not access a member of class HelloJob with modifiers “““

Posted 二木成林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Quartz报错“Class SimpleJobFactory can not access a member of class HelloJob with modifiers “““相关的知识,希望对你有一定的参考价值。

异常

[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.");
    }
}

以上是关于使用Quartz报错“Class SimpleJobFactory can not access a member of class HelloJob with modifiers “““的主要内容,如果未能解决你的问题,请参考以下文章

处理spring-boot-starter-quartz和spring-boot-starter-websocket同时使用报错

使用Quartz报错“Class SimpleJobFactory can not access a member of class HelloJob with modifiers “““

Spring 定时器 使用quartz 报错问题 Error creating bean with name 'reportTask' defined in file 求解

Spring 定时器 使用quartz 报错问题 Error creating bean with name 'reportTask' defined in file 求解

quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null

一文揭秘定时任务调度框架quartz