其他分享
首页 > 其他分享> > 使用石英的数据检索

使用石英的数据检索

作者:互联网

我需要使用Quartz从数据库中检索数据的帮助.我正在从主类中的config.xml中读取休眠属性,并使用这些属性尝试从作业类(Quartz Process.java)中获取数据,该类正在获取空指针异常.

请帮助我解决问题.谢谢前进

这是我的主要课程:

@Component("TestProgram")

public class TestProgram
{
        static ClassPathXmlApplicationContext applicationContext=null;

    public void testMethod() throws SchedulerException
    {
        JobDetail job = new JobDetail();
        job.setName("Retriving The Master Details");
        job.setJobClass(QuartzProcess.class);


        SimpleTrigger trigger = new SimpleTrigger();
        trigger.setName("Trigger For Retriving The Master Details");
        trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        trigger.setRepeatInterval(5000);

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
    }

    public static void main(String[] args) throws Exception
    {
        String conf[] = {"Config.xml"};
        applicationContext= new ClassPathXmlApplicationContext(conf);
        TestProgram unittest=applicationContext.getBean(TestProgram.class);     
        unittest.testMethod();
    }

}

Quartz Process.java

@Component("QuartzProcess")

public class QuartzProcess implements Job
{
    @Autowired
    private MasterService MasterService;


    @Override   
        public void execute(JobExecutionContext jec) throws JobExecutionException
        {
         try
         {


             List<MasterVO> MasterVO=MasterService.findAll();
             System.out.println("MasterVO..."+MasterVO);
             for(int index=0;index<MasterVO.size();index++)
                 System.out.println(MasterVO.get(index));



         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
        }   
}

解决方法:

您将收到Null指针异常,因为您的Quartz作业没有被Spring实例化,并且在springContext之外运行,因此您在其中引用的所有bean将为null.
现在,有几种方法可以访问石英Job中的弹簧豆.

1)在applicationContext中定义以下bean

<bean id="scheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="configLocation">
     <value>classpath:quartz.properties</value>
  </property>
  <property name="applicationContextSchedulerContextKey">
    <value>applicationContext</value>
  </property>

在测试类中获取上述bean调度程序.测试类中的代码将如下所示:

public void testMethod() throws SchedulerException
{
    JobDetail job = new JobDetail();
    job.setName("Retriving The Master Details");
    job.setJobClass(QuartzProcess.class);


    SimpleTrigger trigger = new SimpleTrigger();
    trigger.setName("Trigger For Retriving The Master Details");
    trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
    trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    trigger.setRepeatInterval(5000);


    scheduler.scheduleJob(job, trigger);
}

您需要以通常的方式将调度器bean放入主类.
您不需要执行scheduler.start,因为调度程序将由spring容器启动.

在您的QuartzProcess类中,您需要添加以下方法来获取applicationContext:

    public ApplicationContext getApplicationContext(JobExecutionContext context) throws Exception {
        ApplicationContext applicationContext = null;
        applicationContext = (ApplicationContext) context.getScheduler().getContext().get(APPLICATION_CONTEXT_KEY);
        if (applicationContext == null) {
            throw new JobExecutionException("No application context available in scheduler context for key \""
                                            + APPLICATION_CONTEXT_KEY
                                            + "\"");
        }
        return applicationContext;
    }

然后在quartzprocess的xecute方法中,您需要执行以下代码以获取所需的bean

  ApplicationContext ctx = getApplicationContext(context);
  QuartzProcess quartzProcess = (QuartzProcess)ctx.getBean("quartzProcess");

标签:java,spring,spring-mvc,quartz-scheduler
来源: https://codeday.me/bug/20191012/1901793.html