user2348721
user2348721

Reputation: 41

How to pass a job object instance to quartz scheduler

What I want to be able to do is read a file with a bunch of batch files (and args) and create a quartz job for each of those entries. I know I'm missing something obvious, but I can't seem to find how to do this anywhere on google. Everything I'm finding says a new class has to be coded for each job which can't be externally constructed. I can't seem to find how to create an instance of a class which I can pass into the scheduler.

public class MyJob implements Job{
    private String[] jobArgs = null;

    public MyJob(String[] jobArgs){
        this.jobArgs = jobArgs;
    }

    public void execute(JobExecutionContext arg0) throws JobExecutionException{
        ExternalProcess ep - new ExternalProcess();
        try{
            ep.runExecutableCommand(jobargs);
        }catch(Exception e){...}
    }

}

public class JobScheduler {
    ...
    List<String[]> jobArgList = loadJobListFromDisk();
    List<MyJob> = new ArrayList<MyJob>();
    for(String[] jobArgs : jobList){
        MyJob myJob = new MyJob(jobArgs);
        // Is it possible to pass in a reference to an instance somehow
        // instead of letting the scheduler create the instance based on
        // the class definition?  I know this syntax doesn't work, but this
        // is the general idea of what I'm trying to do.
        JobDetail jobDetail = JobBuilder.newJob(myJob).withIdentity...
    }
}

Upvotes: 4

Views: 2778

Answers (3)

Asaf Magen
Asaf Magen

Reputation: 1104

you can create a custom class and pass it to the job using the jobDataMap. then on the job execution get that object and do what ever you like with it parameters.

  • make sure your customClass (CustomClassParams) implements Serializable.

      public class CustomClassParams implements Serializable {
              int id;
              String name;
              String age;
      }
    
    
    
      public class MyJob implements Job  {
      @Override
      public void execute(JobExecutionContext context) throws JobExecutionException {
    
      CustomClassParams customClassParams = (CustomClassParams)context.getJobDetail().getJobDataMap().get("customClassParams");
      customClassParams.getName();
      customClassParams.getAge();
      System.out.println("Name: "+customClassParams.toString());
    
        }
      }
    
    
          JobDataMap jobDataMap = new JobDataMap();
      CustomClassParams customClassParams = new CustomClassParams();
      customClassParams.setId(1);
      customClassParams.setName("John");  
      customClassParams.setAge("30");
    
      jobDataMap.put("customClassParams", customClassParams);
      JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myJob")
              .usingJobData(jobDataMap)
              .build();
    

Upvotes: 0

Daniel Hajduk
Daniel Hajduk

Reputation: 336

From what ive had quality time with Quartz Scheduler or rather JobBuilder, its written in such a dumb way it only accepts Class object as parameter. I was not able to find a way to pass Job Object into the builder.

It's a very very bad design, resulting in future problems with writing generic solutions for Quartz in version 1.X at least.

Upvotes: 0

Stefaan
Stefaan

Reputation: 21

In quartz 2, you need to use a JobDataMap (stored in the jobDetail) to transfer parameters to the execute method. It will be available in the context.getJobDetail().getJobDataMap()

Upvotes: 2

Related Questions