美文网首页定时任务
Quartz 源码解析(一) —— 基本介绍

Quartz 源码解析(一) —— 基本介绍

作者: icameisaw | 来源:发表于2018-08-31 15:48 被阅读323次

    Quartz是什么

    Quartz提供了一些Scheduler(调度策略),以便我们管理和执行Job(任务)。

    官网介绍

    Quartz是一个功能丰富、开源的任务调度库,它可以被集成到所有的Java程序,无论是很小的单节点还是规模庞大的商业系统。Quartz可以被用来创建简单或者复杂的调度策略,以执行成千上万的任务。任务一般是指一个标准的Java组件,实际上可以是你写代码指定的任何逻辑。Quartz Scheduler还包括很多企业级的特性,例如JTA事务控制和集群。

    怎么使用Quartz

    大致步骤

    1. 创建SchedulerFactory
    2. 创建Scheduler
    3. 创建JobDetail
    4. 创建Trigger
    5. 注册到Scheduler:scheduler.scheduleJob(jobDetail, trigger)
    6. 启动Scheduler:scheduler.start()
    

    实例代码

    以非集群的RamJob为例

    • RAMJob.java
    public class RAMJob implements Job {
    
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("Hello Quartz: " + new Date() + " " + Thread.currentThread().getName());
        }
    
    }
    
    • RAMJobTest.java
    public class RAMJobTest {
    
        @Test
        public void testExecute() throws SchedulerException, InterruptedException {
            // 1.创建Scheduler的工厂
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            // 2.从工厂中获取调度器实例
            Scheduler scheduler = schedulerFactory.getScheduler();
            // 3.创建JobDetail
            JobDetail jobDetail = JobBuilder.newJob(RAMJob.class)
                    .withDescription("this is a ram job") //job的描述
                    .withIdentity("ramJob", "ramGroup") //job 的name和group
                    .build();
            // 4.创建Trigger
            Trigger trigger = TriggerBuilder.newTrigger().withDescription("")
                    .withIdentity("ramTrigger", "ramTriggerGroup")
                    .startAt(new Date()) // 默认当前时间启动
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) // 两秒执行一次
                    .build();
            // 5.注册任务和定时器
            scheduler.scheduleJob(jobDetail, trigger);
            // 6.启动调度器
            scheduler.start();
            System.out.println("启动时间 : " + new Date() + " " + Thread.currentThread().getName());
            Thread.sleep(60000);        
            System.out.println("done");
        }
    
    }
    
    • 程序主要输出
    2018-08-31 15:14:03,769 INFO [org.quartz.core.QuartzScheduler] - Scheduler meta-data: Quartz Scheduler (v${version}.UNKNOWN.UNKNOWN) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
      Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
      NOT STARTED.
      Currently in standby mode.
      Number of jobs executed: 0
      Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
      Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
    
    2018-08-31 15:14:03,769 INFO [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
    2018-08-31 15:14:03,769 INFO [org.quartz.impl.StdSchedulerFactory] - Quartz scheduler version: ${version}.UNKNOWN.UNKNOWN
    2018-08-31 15:14:03,820 INFO [org.quartz.core.QuartzScheduler] - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
    启动时间 : Fri Aug 31 15:14:03 CST 2018 main
    Hello Quartz: Fri Aug 31 15:14:04 CST 2018 DefaultQuartzScheduler_Worker-1
    Hello Quartz: Fri Aug 31 15:14:06 CST 2018 DefaultQuartzScheduler_Worker-2
    Hello Quartz: Fri Aug 31 15:14:08 CST 2018 DefaultQuartzScheduler_Worker-3
    Hello Quartz: Fri Aug 31 15:14:10 CST 2018 DefaultQuartzScheduler_Worker-4
    Hello Quartz: Fri Aug 31 15:14:12 CST 2018 DefaultQuartzScheduler_Worker-5
    Hello Quartz: Fri Aug 31 15:14:14 CST 2018 DefaultQuartzScheduler_Worker-6
    Hello Quartz: Fri Aug 31 15:14:16 CST 2018 DefaultQuartzScheduler_Worker-7
    Hello Quartz: Fri Aug 31 15:14:18 CST 2018 DefaultQuartzScheduler_Worker-8
    Hello Quartz: Fri Aug 31 15:14:20 CST 2018 DefaultQuartzScheduler_Worker-9
    Hello Quartz: Fri Aug 31 15:14:22 CST 2018 DefaultQuartzScheduler_Worker-10
    Hello Quartz: Fri Aug 31 15:14:24 CST 2018 DefaultQuartzScheduler_Worker-1
    ...
    
    done
    

    在Spring使用Quartz

    在Spring里面使用Quartz,你的Job逻辑可以是任意类的任意方法。

    • 依赖
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.2</version>
    </dependency>
    
    • Spring配置文件
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <bean name="cronRAMJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <bean class="org.fahai.quartz.RAMJob"/>
            </property>
            <property name="targetMethod" value="execute"/>
            <property name="concurrent" value="false"/>
        </bean>
    
        <!--Cron表达式触发器-->
        <bean id="cronRAMJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="cronRAMJob"/>
            <property name="cronExpression" value="0/2 * * * * ?"/>
        </bean>
    
        <!--配置调度工厂-->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="cronRAMJobTrigger"/>
                </list>
            </property>
        </bean>
    </beans>
    

    系列文章

    相关文章

      网友评论

        本文标题:Quartz 源码解析(一) —— 基本介绍

        本文链接:https://www.haomeiwen.com/subject/ltzswftx.html