一、增加所依赖的JAR包
1、增加Spring的Maven依赖
<!--Quartz的Maven依赖 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.4</version> </dependency>
二、增加spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--配置定时器--> <bean id="TimerService" class="com.fh.service.system.appuser.TimerService" /> <!--注册定时器类--> <!--定时器1 begin--> <bean id="midDownTaskInfo"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="TimerService" /><!--ref中是新建的TimeService类--> <property name="targetMethod" value="timer1" /><!--value是TimeService类中的具体方法名--> <!--指定定时器任务类要执行的方法名称 这里是timer1 --> </bean> <bean id="midDownTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!--配置定时器任务的调度器 --> <property name="jobDetail" ref="midDownTaskInfo" /> <!--<property name=""></property>--> <property name="cronExpression" value="0/5 * * * * ?" /> <!--每隔5秒执行 --> </bean> <!--定时器2 begin--> <bean id="midStatusTaskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="TimerService" /> <property name="targetMethod" value="timer2" /> </bean> <bean id="midStatusTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="midStatusTaskInfo" /> <property name="cronExpression" value="0/10 * * * * ?" /> <!--每隔10秒执行--> </bean> <!--注册监听器--><!--quartz默认的线程数是10个,如果我们要修改这个线程数需要做一个配置文件,在配置文件内修改线程--> <bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--注册定时器实体 集合 --> <property name="triggers"> <list> <ref bean="midDownTrigger"></ref><!--定时器任务的调度器id名 --> <ref bean="midStatusTaskTrigger"></ref> </list> </property> <property name="configLocation" value="classpath:quartz.properties"/> <!--quartz配置文件的配置 --> </bean> </beans>
三、quartz配置文件
新建quartz.properties文件
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=15//配置线程数 org.quartz.threadPool.threadPriority=5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true org.quartz.jobStore.misfireThreshold=60000 org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
四、TimerService.java
package com.fh.service.system.appuser; import com.fh.util.PageData; import javax.annotation.Resource; import java.util.Date; /** * Created by hu 2018/11/30. */ public class TimerService { public void timer1(){//每五秒执行一次 Date date=new Date(); System.out.println(date); } public void timer2(){//每十秒执行一次 System.out.println("task2"); } }