1

I need help in retrieving the data from database using Quartz. I am reading the hibernate properties from config.xml in the main class and using those properties I tried to retrieve the data from my job class (Quartz Process.java) which is getting Null Pointer Exception.

Please help me in resolving the issue. Thanks and advance

This is my main class:

@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();
         }
        }   
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1710910
  • 41
  • 1
  • 3

2 Answers2

3

You are getting Null pointer exception because your Quartz job is not instantiated by the Spring and is running outside the springContext so all the beans that you are referencing inside it will be null. Now there are couple of ways to access the spring beans inside the quartz Job.

1)define the below bean in the applicationContext

<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>

get the above bean scheduler in your test class.code in your test class will become like below:

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);
}

scheduler bean you need get in the main class in the usual way. you need not do the scheduler.start as the scheduler will be started by the spring containter.

In your QuartzProcess class,you need to add below method to get the 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;
    }

Then in your xecute method of the quartzprocess ,you need to do teh below code to get the required bean

  ApplicationContext ctx = getApplicationContext(context);
  QuartzProcess quartzProcess = (QuartzProcess)ctx.getBean("quartzProcess");
Rips
  • 1,964
  • 1
  • 23
  • 45
0

If you are trying to created a scheduled job that updates your database how about using Spring Task.

Here is an example: How to stop jobs scheduled using spring task

Then just call your method that performs your database update.

Community
  • 1
  • 1
haju
  • 1,278
  • 4
  • 20
  • 38
  • I am not trying to update the data in database. I need to pass the connection pool for the main class to job class(quartzprocess.java) to retrieve the data. Thanks for your reply. – user1710910 Oct 03 '12 at 11:38
  • I'm not sure what you are trying to accomplish in your overall solution, but creating a Spring task would that not work for you instead of creating it in the code? Also if you are trying to pass connection, you can inject it as a bean from spring as well. If you like I can put up an example. – haju Oct 03 '12 at 12:06