Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
    }
}
like image 618
user2348721 Avatar asked Oct 21 '25 12:10

user2348721


1 Answers

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

like image 178
Stefaan Avatar answered Oct 23 '25 02:10

Stefaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!