Quartz调度程序 - 使用相同类的不同作业?

时间:2015-01-05 09:25:23

标签: java quartz-scheduler

Quarts示例通常会这样做:

JobDetail job = newJob(PrintMessageJob.class)

如何创建在运行时定义了param的作业?这样的事情会很好但不允许:

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

由于

1 个答案:

答案 0 :(得分:2)

您必须做的是以下内容。

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");
        ...
    }
}