JobExecutionContext与JobDataMap

JobExecutionContext可以理解为Job实例的上下文,可以通过它获取job运行时的环境以及属性信息,一个Job对应一个JobExecutionContext的实例。当Scheduler调用一个Job时就会将JobExecutionContext传递给job的execute方法。

JobDataMap实现了Map接口,用法同Map类似,它可以装在任何可序列化的数据对象,执行Job时用来传递数据,JobDataMap存在JobExecutionContext中,要通过JobExecutionContext来获取。

创建测试方法,在JobDetail和Trigger中加入传输的数据

public class TestHelloJob2 
{
    public static void main( String[] args ) throws SchedulerException
    {
        /*创建JobDetail实例,并与HelloJob绑定;Job的名字是myjob,隶属于job_group组*/
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myjob", "job_group")
                .usingJobData("data1", "Data1 Information")
                .usingJobData("data2", 10000D)
                .build();

        /*创建Trriger实例,指定Job每隔3秒钟执行一次*/
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("mytrigger", "trigger_group")
                .usingJobData("data2", 20000D)
                .usingJobData("data3", "Data3 Information")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(3)
                        .repeatForever())
                .build();

        /*创建SchedulerFactory实例,把jobDetail与trigger绑定在一起*/
        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);

    }
}

创建作业,实现Job接口,在job中获取传输的数据

public class HelloJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sf.format(new Date()));

        /*获取job的属性*/
        JobKey key = context.getJobDetail().getKey();
        System.out.println("Job name : " + key.getName() + "; Job group : " + key.getGroup());

        /*获取Trigger的属性*/
        TriggerKey tKey = context.getTrigger().getKey();
        System.out.println("Trigger name : " + tKey.getName() + ": Trigger group : " + tKey.getGroup());

        /*获取JobDetail中的DataMap中属性*/
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        String jobData1 = dataMap.getString("data1");
        Double jobData2 = dataMap.getDouble("data2");
        System.out.println("jobData1 : " + jobData1 + " ; jobData2 : " + jobData2);

        /*获取Trigger中的DataMap属性*/
        JobDataMap tDataMap = context.getTrigger().getJobDataMap();
        Double triggerData2 = tDataMap.getDouble("data2");
        String triggerData3 = tDataMap.getString("data3");
        System.out.println("triggerData2 : " + triggerData2 + "; triggerData3 : " + triggerData3);

        System.out.println("This is hello job……");
        System.out.println("------------------------------------------");
    }

}

运行测试方法,输出

2018-04-01 18:11:40
Job name : myjob; Job group : job_group
Trigger name : mytrigger: Trigger group : trigger_group
jobData1 : Data1 Information ; jobData2 : 10000.0
triggerData2 : 20000.0; triggerData3 : Data3 Information
This is hello job……
------------------------------------------
2018-04-01 18:11:43
Job name : myjob; Job group : job_group
Trigger name : mytrigger: Trigger group : trigger_group
jobData1 : Data1 Information ; jobData2 : 10000.0
triggerData2 : 20000.0; triggerData3 : Data3 Information
This is hello job……
……

可见,在测试方法中放入的数据,在job中可以拿到。

你可能感兴趣的:(Quartz)