简体   繁体   中英

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...
    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM