Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 利用石英进行数据检索_Java_Spring_Spring Mvc_Quartz Scheduler - Fatal编程技术网

Java 利用石英进行数据检索

Java 利用石英进行数据检索,java,spring,spring-mvc,quartz-scheduler,Java,Spring,Spring Mvc,Quartz Scheduler,我需要帮助从数据库检索数据使用石英。我正在从主类中的config.xml读取hibernate属性,并使用这些属性尝试从我的作业类(Quartz Process.java)检索数据,该作业类出现空指针异常 请帮我解决这个问题。感谢并预支 这是我的主要课程: @Component("TestProgram") public class TestProgram { static ClassPathXmlApplicationContext applicationContext=nu

我需要帮助从数据库检索数据使用石英。我正在从主类中的config.xml读取hibernate属性,并使用这些属性尝试从我的作业类(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();
         }
        }   
}
@组件(“QuartzProcess”)
公共类流程实现作业
{
@自动连线
私人万事达服务;
@凌驾
public void execute(JobExecutionContext jec)抛出JobExecutionException
{
尝试
{
List MasterVO=MasterService.findAll();
System.out.println(“MasterVO…”+MasterVO);

对于(int index=0;index如果您试图创建更新数据库的计划作业,那么使用Spring任务如何

以下是一个例子:


然后只需调用执行数据库更新的方法。

您将获得空指针异常,因为您的Quartz作业没有被Spring实例化,并且正在springContext之外运行,因此您在其中引用的所有bean都将为空。 现在有两种方法可以访问quartz作业中的SpringBean

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。 您不需要执行scheduler.start,因为计划程序将由spring Container启动

在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");

我没有试图更新数据库中的数据。我需要将主类的连接池传递给作业类(quartzprocess.java)检索数据。感谢您的回复。我不确定您在整个解决方案中要完成什么,但是创建一个Spring任务而不是在代码中创建它对您来说不起作用吗?另外,如果您尝试传递连接,您也可以将它作为Spring的bean注入。如果您喜欢,我可以举一个例子。
  ApplicationContext ctx = getApplicationContext(context);
  QuartzProcess quartzProcess = (QuartzProcess)ctx.getBean("quartzProcess");