user1710910
user1710910

Reputation: 51

Data retrieval using quartz

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

Upvotes: 2

Views: 6724

Answers (2)

Rips
Rips

Reputation: 2004

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

Upvotes: 3

haju
haju

Reputation: 1298

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.

Upvotes: 0

Related Questions