quartz详解5-quartz与spring结合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/answer100answer/article/details/81383348

慕课网_《Java定时任务调度工具详解之Quartz篇》学习总结

1. spring中quartz依赖

quartz需要事务tx依赖,以及support依赖。
以及本身的一个quartz包。

<!-- ======== quartz 额外依赖 ========== -->
<!--quartz需要tx和support-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

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

springmvc结合时,springmvc依赖正常引入。

配置参考
SpringMVC最小依赖+Junit单元测试+log解析

2. quartz配置作业的两种方式及配置文件

  • MethodInvokingJobDetailFactoryBean
  • JobDetailFactoryBean(更加灵活)

先看有哪些配置文件:

web.xml
   |
   |-(加载)spring-context.xml
   |
  (自动寻找资源)log4j的配置文件(几种固定名称)

xml配置中配置jobbean,通常是spring-context.xml中配。这里单独提出来。
这里写图片描述
spring-context.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <context:component-scan base-package="cn.whbing.pro.web"/>
    <mvc:annotation-driven enable-matrix-variables="true" />
    <task:annotation-driven /><!--定时任务注解-->

    <!--spring文件引入-->
    <import resource="/spring/spring-config-quartz.xml"/>
</beans>

spring-config-quartz.xml中仅仅配置bean

<?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">


</beans>

/ 1 / 新建bean类(job)并允许扫描

package cn.whbing.pro.quartz.bean;
import org.springframework.stereotype.Component;

@Component
public class MyBean {
    public void printMessage(){
        System.out.println("My bean execute!"
                +DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
    }
}

/ 2 / 在 spring-config-quartz.xml 中配置 bean

<!--1.通过方式1创建bean-->
    <bean id="mySimpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean"/>
        <property name="targetMethod" value="printMessage"/>
    </bean>
    <!--2.创建触发器-->
    <bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="mySimpleJobDetail"/>
        <property name="startDelay" value="1000"/>
        <property name="repeatInterval" value="2000"/>
    </bean>
    <!--3.scheduler -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="mySimpleJobDetail"/>
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="mySimpleTrigger"/>
            </list>
        </property>
    </bean>

/ 3 / 直接在tomcat中运行即可

是web程序,结果会在控制台中打印出来
结果:

My bean execute!2018-08-03 15:23:52
My bean execute!2018-08-03 15:23:54
My bean execute!2018-08-03 15:23:56

改进:使用JobDetailFactoryBean创建更复杂的jobdetail(可以传入jobDataMap),并使用CronTrigger触发器。

猜你喜欢

转载自blog.csdn.net/answer100answer/article/details/81383348