spring-task.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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <bean id="midTask" class="com.henu.task.MidTask" /> <!--注册定时器类,后面讲 --> <!--定时器1 begin--> <bean id="midDownTaskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="midTask" /> <property name="targetMethod" value="midownloadItemTask" /> <!--指定定时器任务类要执行的方法名称 这里是midownloadItemTask --> </bean> <bean id="midDownTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!--配置定时器任务的调度器 --> <property name="jobDetail" ref="midDownTaskInfo" /> <property name="cronExpression" value="0/5 * * * * ?" /> <!--每隔5秒执行 --> </bean> <!--定时器2 begin--> <bean id="midStatusTaskInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="midTask" /> <property name="targetMethod" value="midownloadStatusTask" /> </bean> <bean id="midStatusTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="midStatusTaskInfo" /> <property name="cronExpression" value="0/10 * * * * ?" /> <!--每隔10秒执行--> </bean> <!--注册监听器 --> <bean id="registerQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--注册定时器实体 集合 --> <property name="triggers"> <list> <ref local="midDownTrigger" /> <ref local="midStatusTaskTrigger" /> </list> </property> </bean> </beans>
web.xml(在web.xml中加入以下代码)
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-task.xml</param-value> </context-param>
MidTask.java
package com.henu.task; public class MidTask { public void midownloadItemTask(){ System.out.println("task1"); } public void midownloadStatusTask(){ System.out.println("task2"); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/15353.html