1.4-Quartz大哥(JobExecutionContext)

  • JobExecutionContext是啥?
    当Scheduler调用一个Job,就会将JobExecutionContext传递给Job的execute()方法;
    Job能通过JobExecutionContext对象访问到Quartz运行时候的环境以及Job本身的明细数据;

一.JobDataMap

1.是什么?

在进行任务调度时JobDataMap存储在JobExecutionContext中,非常方便获取.
JobDataMap可以用来装载任何可序列化的数据对象,当job实例对象被执行时这些参数对象会传递给它.
JobDataMap实现了JDK的Map接口,并且添加了一些非常方便的方法用来存取基本数据类型.

2.获取的两种方式
  • 从Map中直接获取
    放参数
public static void main(String[] args) {
        //创建一个JobDetail实例,将该实例与HelloJob Class绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "JobGroup1")
                .usingJobData("message", "hello myJob1")
                .usingJobData("myJobValue", 3.14F)
                .build();

        //创建一个Trigger实例,用来触发Job来执行的
        //立即执行,并且每隔两秒执行一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "TriggerGroup1")
                .usingJobData("message", "hello myTrigger1")
                .usingJobData("DoubleTriggerValue", 4.1D)
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)
                        .repeatForever())
                .build();
        //创建scheduler实例,有很多工厂,这个是最简单的
        SchedulerFactory sfact = new StdSchedulerFactory();
......
}

取参数之分俩dataMap取

public void execute(JobExecutionContext context) throws JobExecutionException {
        //打印当前的执行之间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Exec Time is : " + sf.format(date));
        //编写具体的业务逻辑
        JobKey key = context.getJobDetail().getKey();
        System.out.println("My name and group is:"+ key.getName() + key.getGroup());

        TriggerKey triggerKey = context.getTrigger().getKey();
        System.out.println("My trigger name and group is:" + triggerKey.getName() + triggerKey.getGroup());

        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        JobDataMap triggerDataMap = context.getTrigger().getJobDataMap();

        String jobMsg = jobDataMap.getString("message");
        float jobFloatValue = jobDataMap.getFloatValue("myJobValue");

        String triggerMsg = triggerDataMap.getString("message");
        Double triggerDoubleValue = triggerDataMap.getDoubleValue("DoubleTriggerValue");
        System.out.println("JobMsg is:" + jobMsg + "JobF is:" + jobFloatValue);
        System.out.println("TriggerMsg is:" + triggerMsg + "TriggeD is:" + triggerDoubleValue);
    }

用一个dataMap取

public void execute(JobExecutionContext context) throws JobExecutionException {
        //打印当前的执行之间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Exec Time is : " + sf.format(date));
        //编写具体的业务逻辑
        JobKey key = context.getJobDetail().getKey();
        System.out.println("My name and group is:"+ key.getName() + key.getGroup());

        TriggerKey triggerKey = context.getTrigger().getKey();
        System.out.println("My trigger name and group is:" + triggerKey.getName() + triggerKey.getGroup());
        //获取到trigger和job合并的dataMap
        JobDataMap dataMap = context.getMergedJobDataMap();
        //这里获取当trigger和job的key值相同的时候,这里取到的是trigger中的值.
        String msg = dataMap.getString("message");
        float jobFloatValue = dataMap.getFloatValue("myJobValue");
        Double triggerDoubleValue = dataMap.getDoubleValue("DoubleTriggerValue");
        System.out.println("JobMsg is:" + msg + "JobF is:" + jobFloatValue);
        System.out.println("TriggerMsg is:" + msg + "TriggeD is:" + triggerDoubleValue);
    }
  • Job实现类中添加setter方法对应JobDataMap的键值(Quartz框架默认的JobFactory实现类在初始化job实例对象时会自动地调用这些setter方法)
    放参数
同从Map中直接获取

取参数

public class HelloJob implements Job {

    //注意要和key值一样
    private String message;
    private Float myJobValue;
    private Double DoubleTriggerValue;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Float getMyJobValue() {
        return myJobValue;
    }

    public void setMyJobValue(Float myJobValue) {
        this.myJobValue = myJobValue;
    }

    public Double getDoubleTriggerValue() {
        return DoubleTriggerValue;
    }

    public void setDoubleTriggerValue(Double doubleTriggerValue) {
        DoubleTriggerValue = doubleTriggerValue;
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        //打印当前的执行之间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Exec Time is : " + sf.format(date));
        //编写具体的业务逻辑
        JobKey key = context.getJobDetail().getKey();
        System.out.println("My name and group is:"+ key.getName() + key.getGroup());
        TriggerKey triggerKey = context.getTrigger().getKey();
        System.out.println("My trigger name and group is:" + triggerKey.getName() + triggerKey.getGroup());
        //只需要添加名字与key相同的成员变量以及setter和getter方法,就可以直接获取到传过来的值;
        System.out.println("JobMsg is:" + message + "JobF is:" + myJobValue);
        System.out.println("TriggerMsg is:" + message + "TriggeD is:" + DoubleTriggerValue);
    }
}

你可能感兴趣的:(1.4-Quartz大哥(JobExecutionContext))