quartz触发不同时段的任务(springboot)

1:引入jar包

<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>

2:简单测试代码

import org.omg.CORBA.PUBLIC_MEMBER;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import static org.quartz.CalendarIntervalScheduleBuilder.calendarIntervalSchedule;
import static org.quartz.DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

public class TestJob {

public static Scheduler scheduler ;

public static Scheduler getSchedulerSingle(){
if (scheduler == null){
try{
scheduler = StdSchedulerFactory.getDefaultScheduler();
System.out.println("应当只出现一次");
}catch (SchedulerException e){
e.printStackTrace();
}
}
return scheduler;
}


public static void main(String[] args) {

test(convertToTimes(add("2019-05-06 15:19:45",20)),"name1","group1");
test(convertToTimes(add("2019-05-06 15:23:00",20)),"name1","group2");
test(convertToTimes(add("2019-05-06 15:21:23",20)),"name1","group3");
test(convertToTimes(add("2019-05-06 15:21:25",20)),"name1","group4");
test(convertToTimes(add("2019-05-06 15:21:00",20)),"name1","group5");
test(convertToTimes(add("2019-05-06 15:22:00",20)),"name1","group6");
}



public static void test(Date t,String tname,String tgroup) {
try{
// 调度器
scheduler = getSchedulerSingle();

// 任务
Map<String,Object> param = new HashMap<>();
param.put("name","xll");
param.put("age",21);
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity(tname,tgroup).usingJobData(new JobDataMap(param)).build();

// 触发器
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(tname,tgroup)
.withSchedule(simpleSchedule()).startAt(t).build();

scheduler.scheduleJob(jobDetail,trigger);

scheduler.start();

}catch (Exception e){
e.printStackTrace();
}
}


private static String add( String date, int num) {
String st_return = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.parse(date);
Calendar calendar = sdf.getCalendar();
calendar.add(Calendar.SECOND, num);
String st_m = "";
String st_d = "";
String st_h = "";
String st_mi = "";
String st_s = "";
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH) + 1;
int d = calendar.get(Calendar.DAY_OF_MONTH);
int h = calendar.get(Calendar.HOUR_OF_DAY);
int mi = calendar.get(Calendar.MINUTE);
int s = calendar.get(Calendar.SECOND);
if (m <= 9) {
st_m = "0" + m;
} else {
st_m = "" + m;
}
if (d <= 9) {
st_d = "0" + d;
} else {
st_d = "" + d;
}
if (h <= 9) {
st_h = "0" + h;
} else {
st_h = "" + h;
}
if (mi <= 9) {
st_mi = "0" + mi;
} else {
st_mi = "" + mi;
}
if (s <= 9) {
st_s = "0" + s;
} else {
st_s = "" + s;
}
st_return = y + "-" + st_m + "-" + st_d+" "+st_h+":"+ st_mi+":"+ st_s;
} catch (ParseException e) {
e.printStackTrace();
}
return st_return;
}

public static Date convertToTimes(String value) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}

posted @ 2019-05-06 16:15  weworld  阅读(513)  评论(0编辑  收藏  举报