Spring集成Quartz定时器

Spring集成Quartz定时器


  • 导包

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    
    
  • 创建spring-quartz.xml文件,并且配置web.xml扫描

    • 配置web.xml

      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:spring/spring-quartz.xml</param-value>
      </context-param>
      
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      
    • 创建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:p="http://www.springframework.org/schema/p"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xmlns:mvc="http://www.springframework.org/schema/mvc"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
          
           <!--注入任务处理类bean  -->   
           <bean id="quartzTask" class="com.xxx.MyQuartzTask"></bean> 
           
           <!-- 2.任务触发器详细信息bean -->
           <bean id="myJobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
           	<!-- 设置任务执行对象 -->
           	<property name="targetObject" ref="quartzTask"></property>
           	<!-- 设置任务执行对象中对应的执行方法 -->
           	<property name="targetMethod" value="doCronTask"></property>
           	<!-- 设置任务是否可并发执行,默认为不并发 -->
           	<property name="concurrent" value="false"></property>
           </bean>
           
           <!-- 2.任务触发器 -->
           <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
           	<!-- 设置任务详细信息 -->
           	<property name="jobDetail" ref="myJobDetail2"></property>
           	<!-- 设置quartz任务执行表达式 ,每天0点执行一次-->
               <!-- 可通过在线生成Cron表达式的工具:http://cron.qqe2.com/ 来生成自己想要的表达式-->
              <property name="cronExpression" value="0 0 0 * * ?"></property>
           </bean>
           
           <!-- 设置触发器调度工厂 -->   
           <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
           	<property name="triggers">
           		<list>
           			<!-- 触发器调度工厂调度简单触发器 -->
           			<ref bean="cronTrigger2"/>
      			<!--<rel bean=""> 多个触发器可以这样配置-->
           		</list>	
           	</property>
           </bean> 
      </beans>
      
      
  • 创建任务处理MyQuartzTask.java

public class MyQuartzTask {
    
    
    public void doCronTask(){
    
    
        System.out.println("doCronTask正在运行...");
    }
}
  • 启动Tomcat(发送请求),定时任务就会启动。

猜你喜欢

转载自blog.csdn.net/weixin_43556773/article/details/113686288