Quartz 之 JobDataMap 获取方法从 Map 中直接获取

Posted satisfysmy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz 之 JobDataMap 获取方法从 Map 中直接获取相关的知识,希望对你有一定的参考价值。

package com.satisfy;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.TriggerKey;

public class HelloJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("current times is"+sdf.format(date));
        JobKey  key=context.getJobDetail().getKey();
        System.out.println("My Job name and group are: "+key.getName()+":"+key.getGroup());
        
        TriggerKey tkey=context.getTrigger().getKey();
        
        System.out.println("My Trigger name and group are: "+tkey.getName()+":"+tkey.getGroup());
        JobDataMap dataMap=context.getJobDetail().getJobDataMap();
        JobDataMap tdataMap=context.getTrigger().getJobDataMap();
        String jobMsg=dataMap.getString("message");
        Float FloatValue=dataMap.getFloat("FloatValue");
        String TriggerjobMsg=tdataMap.getString("message");
        Double DoubleTriggerValue=tdataMap.getDouble("DoubleTriggerValue");
        System.out.println("JobMessage is :"+jobMsg);
        System.out.println("JobFloatValue is :"+FloatValue);
        System.out.println("JobTriggerjobMsg is :"+TriggerjobMsg);
        System.out.println("JobDoubleTriggerValue is :"+DoubleTriggerValue);
        
    }
         
}








package com.satisfy;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class HelloScheduler {
       public static void main(String[] args) throws SchedulerException {
        // 创建一个JobDetail实例,将该实例与HelloJob Class绑定
                    JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                            .withIdentity("myJob").
                            usingJobData("message", "hello myJob1").
                            usingJobData("FloatValue", 3.14f).build();
                    
                    
            // 创建一个Trigger 实例,定义改Job 立即执行,并且每割2 秒重复执行一次,直到永远    
            Trigger trigger=TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").
                    usingJobData("message", "hello trigger1").
                    usingJobData("DoubleTriggerValue", 2.0D).
                    startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
            // 创建Scheduler实例
            SchedulerFactory sfact = new StdSchedulerFactory();
            Scheduler scheduler =sfact.getScheduler();
            scheduler.start();
            Date date=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("current times is"+sdf.format(date));
            scheduler.scheduleJob(jobDetail, trigger);
    }
}

 

以上是关于Quartz 之 JobDataMap 获取方法从 Map 中直接获取的主要内容,如果未能解决你的问题,请参考以下文章

QuartZ

Quartz定时基础

Spring任务调度之Quartz集成

Quartz 之 Simple

c#之 quartz的学习

浅谈JobExecutionContext与JobDataMap