使用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.");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值