Quartz

 1、Quartz簡介 

    Quartz是一個開放源碼項目,專一於任務調度器,提供了極爲普遍的特性如持久化任務,集羣和分佈式任務等。Spring對Quartz的集成與其對JDK Timer的集成在任務、觸發器和調度計劃的聲明式配置方面等都很是類似。

    Quartz的核心由兩個接口和兩個類組成:Job和Scheduler接口,JobDetail和Trigger類。不一樣於JDK Timer,任務不是從實現一個Job接口的類實例開始運行,實際上Quartz在須要的時候才建立job類實例。能夠使用JobDetail類來包裝任務狀態,並傳遞一個信息給Job,或在一個Job的屢次執行過程之間保存信息。 java

 

 

 

 

 

1.建立實體類app

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()+"\t"+"計劃的內容:"+plan.getTask()+"\t"+ new Date());
            
        }
        
    }
}

3.quartz層ide

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");
    }
}

效果:ui

相關文章
相關標籤/搜索