Quartz - An error occured instantiating job to be executed

 

测试quartz的时候报了一个这样的错:

ERROR org.quartz.core.ErrorLogger - An error occured instantiating job to be executed. job= 'group.bbb'

 

 

原因是quartz是采用反射机制来实例化类的,而我把MyJob类作为内部类来写了,无法对其暴露,就不能正常实例化了。

解决方法:直接将MyJob作为一个单独的类文件来写就好了

 

 

public class TestQuartz{
	
	private static SchedulerFactory sf = new StdSchedulerFactory();  

	@Test
	public void test() throws Exception {
        try {
            // Grab the Scheduler instance from the Factory
            Scheduler scheduler = sf.getScheduler();
            
            JobDetailImpl jobDetail = new JobDetailImpl();
            jobDetail.setName("bbb");
            jobDetail.setGroup("group");
            jobDetail.setDescription("msn");
            jobDetail.setJobClass(new MyJob().getClass());
            
            CronTriggerImpl trigger = new CronTriggerImpl();
            trigger.setName("aaa");
            trigger.setGroup("group1");
            trigger.setCronExpression("0/2 * * * * ?");   //每隔2s执行一次
            
            scheduler.scheduleJob(jobDetail, trigger);
            
            if(!scheduler.isShutdown())
            	scheduler.start();

            //scheduler.shutdown();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
	
    public class MyJob implements Job{		
	@Override
	public void execute(JobExecutionContext context)
				throws JobExecutionException {
		System.out.println(context.getJobDetail().getDescription());
		System.out.println("执行任务 ....." + System.currentTimeMillis());
		}
    }
}

 

正常打印如下:

msn

执行任务 .....1462489647331

msn

执行任务 .....1462489647333

msn

执行任务 .....1462489648006

msn

执行任务 .....1462489650007

msn

 

执行任务 .....1462489652002

 

 

你可能感兴趣的:(杂记随笔)