[java]quartz开源任务调度框架

1.什么是quartz?
quartz百度百科
quartz是一种较为成熟的开源任务调度框架,大致有四部分组成Job,JobDetail,Triagger(触发),Schedule(调度)
思路就是 触发事件—>调度(核心)—>执行任务

2.java样例
导入pom依赖

  
    <dependency>
    <groupId>org.quartz-schedulergroupId>
    <artifactId>quartzartifactId>
    <version>2.3.0version>
    dependency>
package com.dongshuo.test.cronjob;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author dongshuo
 * @data 2018/7/27 14:24
 * @Description quartz 由4大部分组成 job jobdetail schedule trigger(触发)
 */
public class QuartzJob implements Job{
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("QuartzJob start");
        //业务逻辑
        StringBuilder sb = new StringBuilder("任务开始执行啦" + Thread.currentThread().getName());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sb.append(sdf.format(date));
        System.out.println(sb);
        System.out.println("QuartzJob end");
    }
}
package com.dongshuo.test.cronjob;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author dongshuo
 * @data 2018/7/27 14:41
 * @Description
 */
public class QuarzSchedule {
    public static void main(String[] args) throws SchedulerException {
        //构造JobDetail 绑定业务逻辑QuartzJob.class
        JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class).build();
        //创建一个Trigger触发器的实例,定义该job立即执行,并且每2秒执行一次,一直执行
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger").startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever()).build();
        //每日的9点40触发任务
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 40 9 * * ? ")).build();
        //创建schedule实例
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail,trigger);
    }
}

以上内容实现了最简单的使用

3.结合Spring


    <bean id ="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        
        <property name ="jobClass" value="com.zx.quartz.MyJob" />
        
        <property name="name" value="myJob" />
        
        <property name="group" value="jobs" />
        
        <property name="durability" value="true"/>
        
        <property name="applicationContextJobDataKey" value="applicationContext"/>
    bean>

    

    
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        
        <property name="cronExpression" value="0/5 * * * * ?" />
    bean>

    
    <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        
        <property name="cronExpression" value="0 */1 * * * ?" />
    bean>

    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
                <ref bean="cronTrigger2" />
            list>
        property>
    bean>

4.

你可能感兴趣的:(java高级)