项目的依赖l如下:(注意quartz的版本号,不同版本quartz的管理任务方法可能不同)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.5</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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="test"/> <bean name="TaskOne" class="job.JobOne"></bean> <bean name="TaskTwo" class="job.JObTwo"></bean> <bean id="JobDetailOne" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="TaskOne" /> <property name="targetMethod" value="WorkTime" /> </bean> <bean id="JobDetailTwo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="TaskTwo" /> <property name="targetMethod" value="WorkTime" /> </bean> <bean id="TriggerOne" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="JobDetailOne" /> <property name="cronExpression" value="0/5 * * * * ? " /> </bean> <bean id="TriggerTwo" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="JobDetailTwo" /> <property name="cronExpression" value="0/5 * * * * ? " /> </bean> <bean id="SchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" destroy-method="destroy"> <property name="triggers"> <list> <ref bean="TriggerOne" /> <ref bean="TriggerTwo" /> </list> </property> </bean> </beans
你可以在java方法中这样管理已经配置好的定时任务:(只要能拿到scheduler,那么一切就迎刃而解。)
//方法一:现场加载spring-quartz.xml // ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring-quartz.xml"); // StdScheduler scheduler = (StdScheduler)context.getBean("SchedulerFactory"); //方法二:之前已经在其他地方加载过spring-quartz.xml, //例如在web.xml中通过ContextLoaderListener已经加载了配置 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); StdScheduler scheduler = (StdScheduler)wac.getBean("SchedulerFactory"); //注意必须通过容器的方式得到scheduler,才能达到效果。 //删除定时任务,使用是默认的分组名 scheduler.deleteJob("JobDetailTwo", "DEFAULT");
成功拿到scheduler后,管理任务就很简单了!
来源:CSDN
作者:white_nine
链接:https://blog.csdn.net/white_nine/article/details/103918697