Andrew
Andrew

Reputation: 1111

Quartz scheduler - Different jobs using same class?

Quarts examples usually do this:

JobDetail job = newJob(PrintMessageJob.class)

How can I create a job that has a param defined at runtime? Something like this would be nice but is not allowed:

JobDetail job = newJob( new PrintMessageJob("my message") );

Thanks

Upvotes: 1

Views: 1406

Answers (1)

shazin
shazin

Reputation: 21883

What you must do is the following.

JobDetail job = newJob(PrintMessageJob.class)
               .usingJobData("message", "my message")
               .build();

public class PrintMessageJob implements Job {

    public PrintMessageJob() {
    }

    public void execute(JobExecutionContext context) throws JobExecutionException
    {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();

        String message = dataMap.getString("message");
        ...
    }
}

Upvotes: 2

Related Questions