如何配置quartz定时器?

首先创建配置文件:applicationContext_job.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"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">  
   
            <!-- 线程执行器配置,用于任务注册 -->   
     <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">    
	     <property name="corePoolSize" value="20" />    
	     <property name="maxPoolSize" value="50" />    
	     <property name="queueCapacity" value="500" />    
    </bean>    
<!--       业务对象==== 要添加定时任务调度的类配置到这里 -->  
     <bean id="jobController" class="com.boot.controller.JobController" />  
       
     <!-- ============= 调度业务=============  -->  
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <!-- 要调用的对象 -->  
        <property name="targetObject" ref="jobController" />  
        <!-- 要执行的方法名称 -->  
        <property name="targetMethod" value="showTime" />  
        <!-- 如果前一个任务还没有结束第二个任务不会启动 false -->  
        <property name="concurrent" value="false" />  
    </bean>  
       
       
    <!-- 调度触发器-->  
    <bean id="mailTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail" ref="jobDetail" />  
                            <!-- 每隔3秒钟审批一次 -->  
        <property name="cronExpression" value="0/3 * * * * ?" />  
    </bean>  
       
<!-- 总管理容器 -->
	<bean id="startQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
		<property name="triggers">
			<list>
				<ref bean="mailTrigger"/>
			</list>
		</property>
	</bean>
   
</beans>  

添加定时任务调度的类JobController

package com.boot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

public class JobController{

	public void showTime() {
		System.out.println("当前时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
}

测试类

package com.boot.controller;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {

	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("classpath:applicationContext_job.xml");
	}
}

springboot测试类

在app类上加上此注解
@ImportResource(locations= {"classpath:applicationContext_job.xml"})

猜你喜欢

转载自blog.csdn.net/weixin_44465729/article/details/89554731
今日推荐