• java框架---->quartz整合spring(一)


      今天我们学习一下quartz的定时器的使用。年轻时我们放弃,以为那只是一段感情,后来才知道,那其实是一生。

    quartz的简单实例

    测试的项目结构如下:

    一、pom.xml中定义quartz的依赖

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.3</version>
    </dependency>

     二、定义配置文件quartz.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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.xsd">
    
        <!-- 要调用的工作类 -->
        <bean id="quartzJob" class="com.linux.huhx.quartz.QuartzJob"></bean>
        <!-- 定义调用对象和调用对象的方法 -->
        <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject" ref="quartzJob" />
            <!-- 调用类中的方法 -->
            <property name="targetMethod" value="work" />
        </bean>
        <!-- 定义触发时间 -->
        <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="jobtask"/>
            <!-- cron表达式 -->
            <property name="cronExpression" value="10,15,20 * * * * ?" />
        </bean>
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list><ref bean="doTime"/></list>
            </property>
        </bean>
    </beans>

    三、定义任务的执行主体类QuartzJob

    package com.linux.huhx.quartz;
    
    import org.springframework.context.Lifecycle;
    import org.springframework.context.SmartLifecycle;
    
    /**
     * Created by huhx on 2017-05-22.
     */
    public class QuartzJob implements Lifecycle, SmartLifecycle {
    
        public void work() {
            System.out.println("Quartz的任务调度!!!");
        }
    
        @Override
        public void start() {
            System.out.println("I am start");
        }
    
        @Override
        public void stop() {
            System.out.println("I am stop");
        }
    
        @Override
        public boolean isRunning() {
            return false;
        }
    
        @Override
        public boolean isAutoStartup() {
            return true;
        }
    
        @Override
        public void stop(Runnable callback) {
    
        }
    
        @Override
        public int getPhase() {
            return 0;
        }
    }

    四、定义测试的主体类Main

    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("config/quartz.xml");
        }
    }

    五、运行的结果如下:

    I am start
    21:00:05.864 [main] DEBUG o.s.c.s.DefaultLifecycleProcessor - Successfully started bean 'quartzJob'
    21:00:05.864 [main] INFO  o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 2147483647
    21:00:05.864 [main] DEBUG o.s.c.s.DefaultLifecycleProcessor - Starting bean 'startQuertz' of type [class org.springframework.scheduling.quartz.SchedulerFactoryBean]
    21:00:05.865 [main] INFO  o.s.s.quartz.SchedulerFactoryBean - Starting Quartz Scheduler now
    21:00:05.865 [main] INFO  org.quartz.core.QuartzScheduler - Scheduler startQuertz_$_NON_CLUSTERED started.
    21:00:05.865 [main] DEBUG o.s.c.s.DefaultLifecycleProcessor - Successfully started bean 'startQuertz'
    21:00:05.870 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
    21:00:05.881 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    21:00:10.010 [startQuertz_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    21:00:10.010 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    Quartz的任务调度!!!
    21:00:15.001 [startQuertz_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    21:00:15.001 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    Quartz的任务调度!!!
    21:00:20.001 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
    21:00:20.001 [startQuertz_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    Quartz的任务调度!!!
    21:00:45.140 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    21:01:10.002 [startQuertz_Worker-4] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    21:01:10.002 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    Quartz的任务调度!!!
    21:01:15.001 [startQuertz_Worker-5] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    21:01:15.001 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    Quartz的任务调度!!!
    21:01:20.001 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
    21:01:20.001 [startQuertz_Worker-6] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    Quartz的任务调度!!!
    21:01:47.376 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    21:02:10.011 [startQuertz_Worker-7] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    Quartz的任务调度!!!
    21:02:10.012 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    21:02:15.001 [startQuertz_Worker-8] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    21:02:15.001 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    Quartz的任务调度!!!
    21:02:20.002 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
    21:02:20.002 [startQuertz_Worker-9] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.jobtask
    Quartz的任务调度!!!
    .............

    友情链接

  • 相关阅读:
    SpringBoot集成Mybatis
    springboot通过slf4j配置日志
    SpringBoot导入jsp依赖始终报错
    shiro小记
    阿里开发手册华山版——(编程规约篇)记录目前自己不合理的地方
    [网络流24题] 2. 太空飞行计划问题 解题报告
    LOJ 6089 小 Y 的背包计数问题 解题报告 (动态规划)
    UVA 10599 Blocks 解题报告 (动态规划)
    Comet OJ#12E Ternary String Counting 解题报告
    [WC2016]挑战NPC 解题报告
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusespringquartz1.html
Copyright © 2020-2023  润新知