Quartz默认的JobFactory实现不会调用setter

时间:2017-09-08 13:06:51

标签: java scheduled-tasks quartz-scheduler

根据Quratz documentation2.x Here

  

如果向作业类添加与JobDataMap中的键名相对应的setter方法(例如上例中数据的setJobSays(String val)方法),那么Quartz的默认JobFactory实现将自动调用那些在实例化作业时设置setter,从而无需在execute方法中显式地从地图中获取值。

现在在我的情况下,虽然我的所有值都是原始值,但不会自动调用setter。同样在execute方法中,我能够从jobDataMap获取值。 我正在使用Quartz 2.2.1,这是我的示例代码。我该怎么办?提前谢谢。

这是我的Job课程。

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class Db2Db implements Job {


  public static final String SOURCE_HOST = "sourceHost";
  public static final String DESTINATION_HOST = "destinationHost";

  protected String sourceHost;
  protected String destinationHost;


  public Db2Db() {
    System.out.println("created object of job");
  }

  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
  //sourceHost is still null
    system.out.println(sourceHost);


  }


  public void setSourceHost(String sourceHost) {
    this.sourceHost = sourceHost;
  }

  public void setDestinationHost(String destinationHost) {
    this.destinationHost= destinationHost;
  }

}

这是我的主要方法

  public class JobDriver {

  public static void main(String[] args) throws SchedulerException {

    /*Starting quartz server remotely*/
    QuartzServer server = QuartzServer.getInstance();
    server.start();



    JobDetail job = newJob(Db2Db.class)
        .usingJobData(SOURCE_HOST, "132.168.0.12")
        .usingJobData(DESTINATION_HOST, "localhost")
        .withIdentity("DailyRead91", "group1")
        .build();

    Trigger trigger = newTrigger().withIdentity("DailyTrigger91", "group1").startNow()
        .withSchedule(simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();

    //Quartz Server Properties
    Properties prop = new Properties();
    prop.put("org.quartz.scheduler.rmi.proxy", "true");
    prop.put("org.quartz.scheduler.rmi.registryHost", "localhost");
    prop.put("org.quartz.scheduler.rmi.registryPort", "1099");
    prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
    prop.put("org.quartz.threadPool.threadCount", "10");

    Scheduler scheduler = new StdSchedulerFactory(prop).getScheduler();
    scheduler.scheduleJob(job, trigger);
  }


}

0 个答案:

没有答案