Quartz代码示例
Quartz代码示例:
package com.huiandong; import org.quartz.*; import java.text.SimpleDateFormat; import java.util.Date; //实现Job,编写任务逻辑 public class QuartzJob implements Job{ private String message; private Float mathJobValue; private Double DoubleTriggerValue; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Float getMathJobValue() { return mathJobValue; } public void setMathJobValue(Float mathJobValue) { this.mathJobValue = mathJobValue; } public Double getDoubleTriggerValue() { return DoubleTriggerValue; } public void setDoubleTriggerValue(Double doubleTriggerValue) { DoubleTriggerValue = doubleTriggerValue; } public void execute(JobExecutionContext jec) throws JobExecutionException { //打印当前的执行时间,格式为2017-01-01 00:00:00 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date); System.out.println(format); JobDetail jobD =jec.getJobDetail(); JobKey key = jobD.getKey(); System.out.println(key.getName()); System.out.println(key.getGroup()); Trigger trig = jec.getTrigger(); TriggerKey key1 = trig.getKey(); System.out.println(key1.getName()); System.out.println(key1.getGroup()); // JobDataMap jdm = jec.getJobDetail().getJobDataMap(); // JobDataMap jdm2 = jec.getTrigger().getJobDataMap(); //覆盖掉相同的key的data // String msg1 = jdm.getString("message"); // float aFloat = jdm.getFloat("mathJobValue"); // // String msg2 = jdm2.getString("message"); // double aDouble = jdm2.getDouble("DoubleTriggerValue"); // System.out.println(msg1); // // System.out.println(aFloat); // System.out.println(msg2); // System.out.println(aDouble); JobDataMap jdm3 = jec.getMergedJobDataMap(); System.out.println(message); System.out.println(mathJobValue); System.out.println(DoubleTriggerValue); //具体的业务逻辑 } }
//设置Trigger开始时间结束时间
package com.huiandong;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QTest {
public static void main(String[] args) throws SchedulerException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
//获取当前时间后3s
date.setTime(date.getTime()+3000);
//获取当前时间后6s
Date date2 = new Date();
date2.setTime(date.getTime()+6000);
//创建一个JobDetail示例,将该实例与HelloJobClass绑定
JobDetail jobD = JobBuilder.newJob(QuartzJob.class)
.withIdentity("quartz1")
.build();
//创建一个Trigger实例,定义该job立即执行,每隔2s重复执行
SimpleTrigger trig = TriggerBuilder.newTrigger()
.withIdentity("Trigger", "group1")
.startAt(date)
.endAt(date2)
.withSchedule
(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(2000)
.repeatForever())
.build();
//创建scheduler实例
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sche = sf.getScheduler();
sche.start();
System.out.println(format);
sche.scheduleJob(jobD,trig);
}
}
//在任务中获取开始和结束时间并打印
package com.huiandong;
import org.quartz.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QuartzJob implements Job{
private String message;
private Float mathJobValue;
private Double DoubleTriggerValue;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Float getMathJobValue() {
return mathJobValue;
}
public void setMathJobValue(Float mathJobValue) {
this.mathJobValue = mathJobValue;
}
public Double getDoubleTriggerValue() {
return DoubleTriggerValue;
}
public void setDoubleTriggerValue(Double doubleTriggerValue) {
DoubleTriggerValue = doubleTriggerValue;
}
public void execute(JobExecutionContext jec) throws JobExecutionException {
//打印当前的执行时间,格式为2017-01-01 00:00:00
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
JobDetail jobD =jec.getJobDetail();
JobKey key = jobD.getKey();
Trigger trig = jec.getTrigger();
TriggerKey key1 = trig.getKey();
Trigger trig1 = jec.getTrigger();
System.out.println("start "+sdf.format(trig1.getStartTime()));
System.out.println("end "+sdf.format(trig1.getEndTime()));
JobKey jobKey = trig1.getJobKey();
System.out.println(jobKey.getName()+" "+jobKey.getGroup());
// JobDataMap jdm = jec.getJobDetail().getJobDataMap();
// JobDataMap jdm2 = jec.getTrigger().getJobDataMap();
//覆盖掉相同的key的data
// String msg1 = jdm.getString("message");
// float aFloat = jdm.getFloat("mathJobValue");
//
// String msg2 = jdm2.getString("message");
// double aDouble = jdm2.getDouble("DoubleTriggerValue");
// System.out.println(msg1);
//
// System.out.println(aFloat);
// System.out.println(msg2);
// System.out.println(aDouble);
// JobDataMap jdm3 = jec.getMergedJobDataMap();
// System.out.println(message);
// System.out.println(mathJobValue);
// System.out.println(DoubleTriggerValue);
//具体的业务逻辑
}
}