其他分享
首页 > 其他分享> > SPRING-QUARTZ 定时器中一个类有多个定时任务

SPRING-QUARTZ 定时器中一个类有多个定时任务

作者:互联网

第一步

在pom.xml中增加quartz的依赖

<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>

 

第二步

新建一个spring-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"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.0.xsd">

    <!--spring扫描注解的配置-->
    <context:component-scan base-package="net.qdedu.task"/>

    <!-- 添加调度的任务bean 配置对应的class-->
    <bean id="myPrintSchedule" class="net.qdedu.task.WeeHoursTask" />
    <!--配置调度具体执行的方法一-->
    <bean id="myStopActivity"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myPrintSchedule" />
        <property name="targetMethod" value="collectWorkGradeData" />
        <property name="concurrent" value="false" />
    </bean>
    <!--配置方法一的调度执行的触发的时间-->
    <bean id="myStopPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myStopActivity" />
        <property name="cronExpression">
            <!-- 每天早上1点执行任务调度 -->
            <!--<value>0 0 3 * * ?</value>-->
            <value>30 * * * * ?</value>
        </property>
    </bean>
<!--配置调度具体执行的方法二-->
<bean id="myStartActivity" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myPrintSchedule" /> <property name="targetMethod" value="collectKnowledgeAbilityData" />
<property name="concurrent" value="false" />
</bean>
<!--配置方法二的调度执行的触发的时间-->
<bean id="myStartPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myStartActivity" />
<property name="cronExpression">
<!-- 每天早上1点执行任务调度 -->
<!--<value>0 0 3 * * ?</value>-->
<value>30 * * * * ?</value>
</property>
</bean>
<!-- quartz的调度工厂 调度工厂只能有一个,多个调度任务在list中添加 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list> <!-- 所有的调度列表-->
<ref bean="myStopPrintTrigger"/>
<ref bean="myStartPrintTrigger"/>
</list>
</property>
</bean>
</beans>

 

第三步

在spring-context中增加扫描spring-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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd




        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
       default-lazy-init="true">
    <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
    <!-- base-package 如果多个,用“,”分隔 -->
    <context:component-scan base-package="com.we,com.tfedu,net.qdedu">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!-- 打开aop 注解 -->
    <aop:aspectj-autoproxy />

    <!-- 引入配置文件 -->
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:config/*.properties</value>
            </list>
        </property>
        <!-- 设置编码格式 -->
        <property name="fileEncoding" value="UTF-8"></property>
    </bean>


    <!-- apollo 配置中心 -->
    <bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>


    <import resource="database.xml" />
    <import resource="jedis.xml" />
    <import resource="ftl.xml" />
    <import resource="aop.xml" />
    <import resource="dubbo-common.xml" />
    <import resource="dubbo-idgen-consumer.xml" />
    <import resource="dubbo-work-consumer.xml" />
    <import resource="dubbo-area-consumer.xml" />
    <import resource="spring-produce-kafka.xml" />
    <import resource="spring-quartz.xml" />


    <!-- 启动加载 -->
    <bean id="SpringContextUtil" class="com.we.core.web.util.SpringContextUtil" />
    <bean id="WeSetup" class="net.qdedu.Setup" />

    <!-- 异常处理 -->
    <bean id="handlerExceptionResolver" class="com.we.core.web.exception.ExceptionResolver" />

    <!-- druid spring monitor setting -->
    <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>net.qdedu.service.*</value>
                <value>net.qdedu.dao.*</value>
            </list>
        </property>
    </bean>
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    </aop:config>



    <bean id="idGen" class="com.we.core.idgen.service.SnowflakeIdGen">
        <property name="workerIdBaseService" ref="workerIdBaseService"/>
        <property name="serviceName" value="${service.name:${service.name}}"/>
        <property name="serviceVersion" value="${service.version:${service.version}}"/>
    </bean>

</beans>  

 

标签:xml,QUARTZ,SPRING,30,quartz,spring,类有,qdedu,net
来源: https://www.cnblogs.com/houpengwei/p/10967400.html