触发器通用属性 JoKey StartTime EndTime

时间:2021-10-07 03:01:00
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.Trigger;
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));
        Trigger currentTriger=context.getTrigger();
        System.out.println("Start Time is "+sdf.format(currentTriger.getStartTime()));
        System.out.println("End Time is "+sdf.format(currentTriger.getEndTime()));
        JobKey jobkey=currentTriger.getJobKey();
        System.out.println("JobKey info--jobName: "+jobkey.getName()+",jobGroup:"+jobkey.getGroup());
    }
    
    
    
         
}







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 {
           Date date=new Date();
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           System.out.println("current times is"+sdf.format(date));
           // 获取当前时间3 秒后的时间
           date.setTime(date.getTime()+3000);
           
           Date endDate=new Date();
           // 获取当前时间6 秒后的时间
           endDate.setTime(endDate.getTime()+6000);
        // 创建一个JobDetail实例,将该实例与HelloJob Class绑定
                    JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                            .withIdentity("myJob").
                            build();
                    
                    
            // 创建一个Trigger 实例,定义改Job 立即执行,并且每割2 秒重复执行一次,直到永远    
            Trigger trigger=TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").
                    startAt(date).
                    endAt(endDate).
                    withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
            // 创建Scheduler实例
            SchedulerFactory sfact = new StdSchedulerFactory();
            Scheduler scheduler =sfact.getScheduler();
            scheduler.start();
            
            scheduler.scheduleJob(jobDetail, trigger);
    }
}