zoukankan      html  css  js  c++  java
  • Quartz 使用

    package s.simple;
    import org.quartz.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @Title: HelloJob
     * @ProjectName quartz
     * @date 2019/6/2513:52
     * 实现任务逻辑的接口Job
     */
    public class HelloJob implements Job {
        private String testParam;
    
        public void setTestParam(String testParam) {
            this.testParam = testParam;
        }
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            Date date = new Date();
            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sfd.format(date);
            System.out.println("当前系统时间: "+ format);
    
            // 值获取2种方式
            // 1.获取jobDetailMap 和 TriggerDataMap
            JobDataMap jobDataMap = jobExecutionContext.getTrigger().getJobDataMap();
            JobDataMap jobDataMap1 = jobExecutionContext.getJobDetail().getJobDataMap();
            // 合并后的Map Trgger会覆盖Detail中的key
            JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
            String message1 = mergedJobDataMap.getString("message");
            // 2.传值方式也可以通过属性方式,需要定义set方法
            System.out.println(testParam);
    
            // 获取开始和结束时间
            Trigger trigger = jobExecutionContext.getTrigger();
            Date startTime = trigger.getStartTime();
            String s_time = sfd.format(startTime);
            System.out.println("开始时间:"+startTime);
            Date endTime = trigger.getEndTime();
            System.out.println("结束时间:"+sfd.format(endTime));
    
    
        }
    }
    

     

    Scheduler
    package s.simple;
    
    import org.quartz.*;
    import org.quartz.impl.StdSchedulerFactory;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @Title: HelloScheduler
     * @ProjectName quartz
     * @date 2019/6/2513:58
     */
    public class HelloScheduler {
        public static void main(String[] args) {
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("testParam","JobDetail");
            JobDataMap jobDataMap2 = new JobDataMap();
            jobDataMap2.put("message","Trigger");
            // 打印当前时间
            Date date = new Date();
            Date date2 = new Date();
            Date endDate = new Date();
            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sfd.format(date);
            System.out.println("current exec time is "+ format);
            date.setTime(date.getTime()+3000);  // 当前时间3秒
            date2.setTime(date2.getTime()+4000);  // 当前时间4秒
            endDate.setTime(endDate.getTime()+6000);  // 当前时间6秒后停止
            // 创建jobDetail 并绑定helloJob
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").usingJobData(jobDataMap).build();
            /**
             * Trigger作用是执行Job,告诉调度程序什么时候触发
             * 创建Trigger 立即执行,并每隔2秒执行,重复永远
                Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1")
                        .usingJobData(jobDataMap2)
                        .startAt(date).endAt(endDate) // 开始时间和结束时间
                        .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(2)
                        .repeatForever())
                        .build();
             */
            /**
             * SimpleTrigger
             * 距离当前时间4秒后首次执行,之后每两秒重复执行一次
            SimpleTrigger trigger = TriggerBuilder
                         .newTrigger()
                         .withIdentity("myTrigger", "group1")
                            .usingJobData(jobDataMap2)
                            .startAt(date2) // 4秒后开始时间
                            .withSchedule(SimpleScheduleBuilder
                            .simpleSchedule()
                            .withIntervalInSeconds(2)  // 重复间隔
                            .withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY)) // 重复次数
                    .build();
            */
            /**
             * CronTrigger 使用
             *  1. (* * * * * ? *) 每秒触发一次
             *  2. (* 15 10 ? * * 2017)  2017年每天10.15触发一次
             */
            CronTrigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .usingJobData(jobDataMap2)
                    .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *"))
                    .build();
            // 创建Scheduler示例
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            try {
                Scheduler scheduler = schedulerFactory.getScheduler();
                scheduler.scheduleJob(jobDetail, trigger);
                scheduler.start();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    

      

  • 相关阅读:
    leetcode66 plusOne
    park/unpark 阻塞与唤醒线程
    leetcode55 jumpGame贪心算法
    ACID特性与事务的隔离级别
    PCB ODB++(Gerber)图形绘制实现方法
    PCB 所建不凡 AWS 技术峰会2018 • 深圳站 2018.9.20
    PCB SQL SERVER 位运算应用实例
    PCB SQL SERVER 枚举分割函数(枚举值分解函数)
    PCB SQL SERVER 正则应用实例
    PCB Genesis 外形加内角孔实现方法
  • 原文地址:https://www.cnblogs.com/412013cl/p/11084353.html
Copyright © 2011-2022 走看看