Spring中整合Quartz插件【一】基于MethodInvokingJobDetailFactoryBean实现任务管理

7 篇文章 0 订阅
5 篇文章 0 订阅

1.引入除spring.jar外的Quartz的jar包quartz-all-1.8.6.jar

2.自定义实现任务类 QuartzJob#[普通的POJO]

publicclass QuartzJob {
    publicvoid work1(){
      System.out.println("Quartz的任务调度!!!work1启用……");
    }
    publicvoid work2(){
      System.out.println("Quartz的任务调度!!!work2启用……");
    }
}
<strong><span style="font-size:14px;">#3.Spring 配置 quartz-config.xml</span></strong>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
 
    <!-- 线程执行器配置,用于任务注册 -->
    <beanid="executor"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <propertyname="corePoolSize"value="10"/>
        <propertyname="maxPoolSize"value="100"/>
        <propertyname="queueCapacity"value="500"/>
    </bean>
 
    <!-- 要调用的工作类 -->
    <beanid="quartzJob"class="QuartzJob"></bean>
 
    <!--调度业务 : 定义调用对象和调用对象的方法 -->
    <beanid="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <propertyname="targetObject"ref="quartzJob"/>
        <!-- 调用类中的方法 -->
        <propertyname="targetMethod"value="work1"/>
    </bean>
 
    <beanid="jobtask2"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <propertyname="targetObject"ref="quartzJob"/>
        <!-- 调用类中的方法 -->
        <propertyname="targetMethod"value="work2"/>
    </bean>
     
    <!-- 定义触发时间 -->
    <beanid="doTime1"class="org.springframework.scheduling.quartz.CronTriggerBean">
        <propertyname="jobDetail"ref="jobtask1"/>
        <!-- cron表达式 --><!-- 每隔10秒执行一次 -->
        <propertyname="cronExpression"value="0/3 * * * * ?" />
    </bean>
     
    <beanid="doTime2"class="org.springframework.scheduling.quartz.CronTriggerBean">
        <propertyname="jobDetail"ref="jobtask2"/>
        <propertyname="cronExpression"value="0/5 * * * * ?" />
    </bean>
     
    <!--设置调度 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <beanid="startQuertz"lazy-init="false"autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <propertyname="triggers">
            <list>
                <refbean="doTime1"/>
                <refbean="doTime2"/>
            </list>
        </property>
         
        <propertyname="taskExecutor"ref="executor"/>
    </bean>
</beans>

4.Servlet的配置文件web.xml中加载Spring配置文件 

?
1
2
3
4
5
6
< context-param
         < param-name >contextConfigLocation</ param-name
         < param-value
             /WEB-INF/classes/quartz-config.xml 
         </ param-value
     </ context-param >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值