spring 两种整合quartz的方法

    xiaoxiao2025-05-22  8

    spring 两种整合quartz的方法:

    方法1.通过配置文件xml整合:

    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:p="http://www.springframework.org/schema/p"       xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:context="http://www.springframework.org/schema/context"       xmlns:jee="http://www.springframework.org/schema/jee"       xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:task="http://www.springframework.org/schema/task"       xsi:schemaLocation="             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <!--要调度的对象  -->     <bean id="jobBean" class="com.java1234.schedule.TestQuertz" />      <bean id="testQuertz" class="com.java1234.schedule.TestQuertz2" />      <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="jobBean" />           <property name="targetMethod" value="execute" />           <!--将并发设置为false -->         <property name="concurrent" value="false" />       </bean>       <bean id="MytestQuertz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="testQuertz" />           <property name="targetMethod" value="execute" />           <!--将并发设置为false -->         <property name="concurrent" value="false" />       </bean>      <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">           <property name="jobDetail" ref="jobDetail" />           表达式,我的是每 30s 执行一次           <property name="cronExpression" value="0/5 * * * * ?" />      </bean>        <bean id="trigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">           <property name="jobDetail" ref="MytestQuertz" />           <!--表达式,我的是每 30s 执行一次>           <property name="cronExpression" value="0/6 * * * * ?" />      </bean>           <!--总管理类如果将lazy-init='false'那么容器启动就会执行调度程序-->        <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >           <property name="triggers">               <list>                   <!--作业调度器,list下可加入其他的调度器>                   <ref bean="trigger" />                    <ref bean="trigger2" />                </list>           </property>      </bean>  

    </beans>

    执行的class:

    public class TestQuertz { /**        * 业务逻辑处理        */      public void execute() {           // 执行业务逻辑           // ........           System.out.println("正在执行定时任务1");       }   }

    方法2:直接在执行的class中利用注解,进行设置定时任务

    xml:

    此时xml中的需要配置的内容很少,只需要配置如下:

    <!-- 自动扫描执行的类的路径 --> <context:component-scan base-package="com.java1234.schedule" /> <!-- 加载定时任务 --> <task:annotation-driven/>

    执行的class:

    package com.java1234.schedule; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class TestQuertz { /**        * 业务逻辑处理        */     @Scheduled(cron="0/5 * * * * ?")     public void execute() {           // 执行业务逻辑           // ........           System.out.println("正在执行定时任务1");       }   }

    完毕!

    转载请注明原文地址: https://ju.6miu.com/read-1299134.html
    最新回复(0)