Announcement Announcement Module
Collapse
No announcement yet.
Spring's Quartz Integration: how to schedule new jobs programmatically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring's Quartz Integration: how to schedule new jobs programmatically

    In my application (a web application) I need to schedule jobs. I am thinking of using Quartz for scheduling jobs. Since my application is based on the Spring Framework and most of the functionality is available through Spring beans I thought of integrating Quartz using the Spring Framework.

    There quite a few good tutorial and blogs around for Spring Quartz integration:

    http://cse-mjmcl.cse.bris.ac.uk/blog...370280435.html
    http://jroller.com/JamesGoodwill/ent...spring_example
    http://static.springframework.org/sp...eduling-quartz

    In all the tutorials the jobs are hard wired in the Spring application context. However I will need to create new jobs and schedule them programmatically. To start a Quartz scheduler I have added the following line to my application context:

    Code:
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
          <property name="configLocation">
             <value>classpath:quartz.properties</value>
          </property>
          <property name="applicationContextSchedulerContextKey">
          	<value>applicationContext</value>
          </property>
    </bean>
    But how can I now schedule jobs programmatically?

    An example of adding jobs in Quartz (not integrated with Spring) can be found in http://www.onjava.com/pub/a/onjava/2...tz.html?page=2

    Code:
    SchedulerFactory schedFact 
           new org.quartz.impl.StdSchedulerFactory();
          Scheduler sched  schedFact.getScheduler();
          sched.start();
          JobDetail jobDetail 
            new JobDetail(
              "Income Report",
              "Report Generation",
              QuartzReport.class
            );
          jobDetail.getJobDataMap().put(
                                    "type",
                                    "FULL"
                                   );
          CronTrigger trigger  new CronTrigger(
            "Income Report",
            "Report Generation"
          );
          trigger.setCronExpression(
            "0 0 12 ? * SUN"
          );
          sched.scheduleJob(jobDetail, trigger);

    However there is no method for scheduling a new job in the SchedulerFactoryBean.

    Best Regards,

    Anette

  • #2
    Thanks to the thread http://forum.springframework.org/sho...lerFactoryBean I found the answer to my question:

    To get the Scheduler instance call getObject() method in SchedulerFactoryBean.

    Anette

    Comment


    • #3
      I know this is a very old post, but my case is very similar, and I am unable to get it working

      In my case QuartzReport execute () method has some logic which depends on property defined in this class. For Ex:
      Code:
      public class QuartzReport implements Job 
      {
      private DeliveryHandler deliveryHandler;
      public void execute(JobExecutionContext context) throws JobExecutionException {
      // some code
      deliveryHandler.createReport();
      }
      I have defined QuartzReport as a spring bean with DeliveryHandler dependency, but seems like Quarz creates a new instance every time before triggering the job, so my deliverHanlder is being null and I get a NullPointerException. How can I get arround this case.

      Comment


      • #4
        I got this working with the following.
        a) Let QuartzReport extend QuartzJobBean instead of implementing Job Interface.
        b) Define JobDetailBean programatically and set deliveryHandler as an entry of jobdataMap
        Code:
        JobDetailBean jobDetailBean = new JobDetailBean();
        Map jobDataMap = new HashMap()
        // some code...
        jobDataMap.put("deliveryHandler",deliveryHandler);
        jobDetailBean.setJobDataMap(jobDataMap)
        As mentioned in spring-reference guide "JobDetailBean also maps the properties from the job data map to properties of the actual job".

        Comment

        Working...
        X