我需要帮助使用Quartz从数据库中检索数据。我正在从主类中的config.xml读取hibernate属性,并使用那些我尝试从我的作业类(Quartz Process.java)中检索数据的属性,这些数据正在获得Null Pointer Exception。
请帮我解决问题。谢谢并提前
这是我的主要课程:
@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();
}
}
}
答案 0 :(得分:3)
您正在获取Null指针异常,因为您的Quartz作业未由Spring实例化并且在springContext之外运行,因此您在其中引用的所有bean都将为null。 现在有几种方法可以访问石英Job中的spring bean。
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调度程序。测试类中的class.code将如下所示:
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 containter启动。
在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");
答案 1 :(得分:0)
如果您尝试创建一个更新数据库的预定作业,请使用Spring Task。
这是一个例子: How to stop jobs scheduled using spring task
然后只需调用执行数据库更新的方法。