• 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+"的提醒信息
    ");
            for (Plan plan : list) {
                System.out.println(plan);
            }
        }
         public void printPlan(String userName){
               List<Plan> plansForToday = getPlansForToday(userName);
                System.out.print(userName + "的提醒信息:
    ");
                for(Plan plan:plansForToday){
                    System.out.print(plan +"
    ");
                }
           }
           
           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();
            }
        }
    }
  • 相关阅读:
    MongoDB的安装与简单使用
    [SCOI2008]天平
    [ZJOI2008]树的统计
    [HEOI2015]兔子与樱花
    [HAOI2006]l旅行
    [ZJOI2008]泡泡堂BNB
    [ZJOI2007]时态同步
    [SCOI2005]栅栏
    [SCOI2008]着色方案
    [SCOI2005]互不侵犯King
  • 原文地址:https://www.cnblogs.com/zxtceq/p/7650287.html
Copyright © 2020-2023  润新知