quartz管理xml配置文件中的定时任务

项目的依赖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后,管理任务就很简单了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值