scheduletask任务调度
1.导入jar包
2.创建entity、
package cn.happy.entity; public class Plan { //时间 private String date; //任务 private String task; public Plan() { } public Plan(String date, String task) { this.date = date; this.task = task; } @Override public String toString() { return "Plan [date=" + date + ", task=" + task + "]"; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTask() { return task; } public void setTask(String task) { this.task = task; } }
3、创建service
package cn.happy.service; import java.util.ArrayList; import java.util.List; import cn.happy.entity.Plan; public class RemindService { public void print(String name){ List<Plan> list = getPlan(name); System.out.println(name+"的提醒信息\n"); for (Plan plan : list) { System.out.println(plan); } } public void printPlan(String userName){ List<Plan> plansForToday = getPlansForToday(userName); System.out.print(userName + "的提醒信息:\n"); for(Plan plan:plansForToday){ System.out.print(plan +"\n"); } } public List<Plan> getPlansForToday(String userName){ //模拟数据库查询,仅为说明问题 List<Plan> list = new ArrayList<Plan>(); list.add(new Plan("2003-8-9 9:00","研讨会 | 地点:会议室C01")); list.add(new Plan("2003-8-9 14:00","汇报 | 地点:总裁办公室")); return list; } public List<Plan> getPlan(String name){ List<Plan> list = new ArrayList<Plan>(); list.add(new Plan("2016年12月18日10:59", "研讨会")); list.add(new Plan("2016-12-18 11:01","汇报")); return list; } }
4、创建quartz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package cn.happy.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import cn.happy.service.RemindService; /** * 工作调度类 quartz 类 需要实现 job接口 * @author SLEEP * */ public class RemindJob implements Job { //注入service对象 RemindService service = new RemindService(); @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { service.printPlan( "见见" ); } } |
5.创建测试类(关键)
package cn.happy.test; 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; import cn.happy.quartz.RemindJob; /** * 测试类 * 使用quartz类 * @author SLEEP * */ public class Test { public void doRemind() throws SchedulerException, InterruptedException { //创建一个任务 JobDetail job= JobBuilder.newJob(RemindJob.class) // .withIdentity("job1", "group1") .build(); //创建一个触发器 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "myTriggerGroup") .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5) .repeatForever()) .startAt(new Date(System.currentTimeMillis() + 1000)) .build(); //创建调度工厂 SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); //创建一个调度者 Scheduler scheduler = stdSchedulerFactory.getScheduler(); //注册并进行调度 scheduler.scheduleJob(job,trigger); //启动调度 scheduler.start(); //创建调度者工厂 SchedulerFactory sfc = new StdSchedulerFactory(); //创建一个调度者 Scheduler sched = sfc.getScheduler(); //注册并进行调度 sched.scheduleJob(job, trigger); //启动调度 sched.start(); /*//sleep10s Thread.sleep(10000); //关闭调度 sched.shutdown();*/ } /** * */ public static void main(String[] args) { try{ Test testJob = new Test(); testJob.doRemind(); }catch(Exception e){ e.printStackTrace(); } } }