使用Quartz报错“Class SimpleJobFactory can not access a member of class HelloJob with modifiers “““
  TEZNKK3IfmPf 2023年11月12日 44 0

异常

[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.");
    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   20天前   43   0   0 java
  TEZNKK3IfmPf   2024年05月31日   54   0   0 java
TEZNKK3IfmPf