码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-06-18 10:26:37      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:exce   star   ack   with   actor   ali   context   UI   rev   

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 中直接获取

标签:exce   star   ack   with   actor   ali   context   UI   rev   

原文地址:https://www.cnblogs.com/satisfysmy/p/9194264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!