Quartz JobDataMap 获取方法在Job 实现类中添加setter 方法

Posted satisfysmy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz JobDataMap 获取方法在Job 实现类中添加setter 方法相关的知识,希望对你有一定的参考价值。

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 {
        private String message;
        private Float FloatValue;
        private Double DoubleTriggerValue;
        
    public String getMessage() {
            return message;
        }

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

        

        public Float getFloatValue() {
            return FloatValue;
        }

        public void setFloatValue(Float floatValue) {
            FloatValue = floatValue;
        }

        public Double getDoubleTriggerValue() {
            return DoubleTriggerValue;
        }

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

    @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());
        
        System.out.println("Message is :"+message);
        System.out.println("FloatValue is :"+FloatValue);
        System.out.println("DoubleTriggerValue 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 获取方法在Job 实现类中添加setter 方法的主要内容,如果未能解决你的问题,请参考以下文章

QuartZ

Quartz定时基础

浅谈JobExecutionContext与JobDataMap

quartz

Quartz入门 Job管理

quartz作业调度的应用和原理