zoukankan      html  css  js  c++  java
  • Quartz

     一、Quartz简介 

        Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等。Spring对Quartz的集成与其对JDK Timer的集成在任务、触发器和调度计划的声明式配置方面等都非常相似。

        Quartz的核心由两个接口和两个类组成:Job和Scheduler接口,JobDetail和Trigger类。不同于JDK Timer,任务不是从实现一个Job接口的类实例开始运行,实际上Quartz在需要的时候才创建job类实例。可以使用JobDetail类来包装任务状态,并传递一个信息给Job,或在一个Job的多次执行过程之间保存信息。 

    1.创建实体类

    package cn.happy.entity;
    
    public class Plan {
        private String date; //时间
        
        private String task; //任务
        
        
        public Plan(String date, String task) {
            this.date = date;
            this.task = task;
        }
        
    
        public Plan() {
            
        }
    
        @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;
        }
    }

    2.创建RemindService层

    package cn.happy.service;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import cn.happy.entity.Plan;
    
    public class RemindService {
        public List<Plan> getPlansforToday(){
            List<Plan> list=new ArrayList<Plan>();
            Plan plan1=new Plan("2016-12-17 23:02:32","Study makes me happy!!"); 
            Plan plan2=new Plan("2016-12-17 23:02:50","Really!!"); 
            list.add(plan1);
            list.add(plan2);
            return list;
        }
    
        
        public void printMessage(){
            
            
            List<Plan> list=getPlansforToday();
            for (Plan plan : list) {
                System.out.println("计划的时间:"+plan.getDate()+"	"+"计划的内容:"+plan.getTask()+"	"+ new Date());
                
            }
            
        }
    }

    3.quartz层

    package cn.happy.quartz;
    
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import cn.happy.service.RemindService;
    
    public class RemindJob implements Job {
    private RemindService service=new RemindService();
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            
            service.printMessage();
            
            
            
    
        }
    
        public RemindService getService() {
            return service;
        }
        public void setService(RemindService service) {
            this.service = service;
        }
    }

    4.测试类

    package cn.happy.test;
    
    import java.util.Date;
    
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    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;
    
    public class MyQuartzTest {
        
    public static void tool() throws Exception{
            
            //创建job
            JobDetail job = JobBuilder.newJob(RemindJob.class)//
                    .withIdentity("job1", "group1").build();
            
            //构建trigger
            /*
             CronTrigger trigger = TriggerBuilder.newTrigger().//
                     withIdentity("trigger1", "group1").//
                     withSchedule(CronScheduleBuilder.cronSchedule("0 40 18 ? * 7#3 2016"))
                     .build();
            */
            
         Trigger trigger = TriggerBuilder.newTrigger() 
                     .withIdentity("myTrigger", "myTriggerGroup")
                     .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                         .withIntervalInSeconds(3)//每隔三秒执行一次
                         .repeatForever())
                     .startAt(new Date(System.currentTimeMillis()))
                     .build();
            
            
            //创建调度者工厂
            SchedulerFactory sfc = new StdSchedulerFactory();
            //创建一个调度者
            Scheduler sched=sfc.getScheduler();    
            //注册并进行调度
            sched.scheduleJob(job, trigger);
            sched.start();
            //Thread.sleep(3000);
                    
        }
        
        public static void main(String[] args)  {
            try {
                tool();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("aa");
        }
    }

    效果:

  • 相关阅读:
    css控制英文内容自动换行問題
    jquery添加select option两种代码思路比较
    C++实现单例模式
    C++实现单例模式
    windows下socket编程:区分shutdown()及closesocket()
    windows下socket编程:区分shutdown()及closesocket()
    socket关闭
    socket关闭
    C++模板的一些巧妙功能
    C++模板的一些巧妙功能
  • 原文地址:https://www.cnblogs.com/Smile-123/p/6194537.html
Copyright © 2011-2022 走看看