spring提供了定时任务功能,不需要第三方jar包支持,spring足以。
代码:
package com.inth.product.web.task; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.inth.product.service.impl.ContractServiceImpl; @Component("changeStateTask") public class ChangeStateTask{ @Autowired private ContractServiceImpl contractServiceImpl; /** * 定时更改合同状态 * 0 0 * * * ? 表示每次秒和分为0时触发一次(每小时一次) * "@Scheduled"时spring提供的定时任务标签 * 需要在applicationContext.xml中添加 * xmlns:task="http://www.springframework.org/schema/task" * xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd " * <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> * <task:scheduler id="qbScheduler" pool-size="10"/ * ChangeStateTask.doJob()<BR> * <P>Author : DingYinLong </P> * <P>Date : 2014年7月28日 </P> */ @Scheduled(cron = "0 0 * * * ?") public void doJob(){ this.contractServiceImpl.executeStateChange(); } /** * 固定每分钟执行一次 * ChangeStateTask.doJob1()<BR> * <P>Author : DingYinLong </P> * <P>Date : 2014年8月1日 </P> */ @Scheduled(fixedRate = 60*1000) public void doJob1(){ System.out.println(new Date() + "-----------------doJob1"); } /** * 上次任务结束后一分钟后再次执行 * ChangeStateTask.doJob2()<BR> * <P>Author : DingYinLong </P> * <P>Date : 2014年8月1日 </P> */ @Scheduled(fixedDelay = 60*1000) public void doJob2(){ System.out.println(new Date() + "-----------------doJob2"); } }
applicationContext.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <!-- 扫描包基础目录 --> <context:component-scan base-package="com.inth" /> <!-- 识别@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/> </beans>
1,beans 属性加上xmlns:task="http://www.springframework.org/schema/task"以及xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "
2,fixedRate和fixedDelay的区别写在注释上了。
以上情况不基于注解纯配置如下:
代码:
package com.inth.product.web.task; import java.util.Date; import com.inth.product.service.impl.ContractServiceImpl; public class ChangeStateTask{ private ContractServiceImpl contractServiceImpl; public void doJob(){ System.out.println(new Date() + "-----------------doJob"); // this.contractServiceImpl.executeStateChange(); } public void doJob1(){ System.out.println(new Date() + "-----------------doJob1"); } public void doJob2(){ System.out.println(new Date() + "-----------------doJob2"); } }applicationContext.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <!-- 扫描包基础目录 --> <context:component-scan base-package="com.inth" /> <bean name="taskJob" class="com.inth.product.web.task.ChangeStateTask"></bean> <task:scheduled-tasks> <task:scheduled ref="taskJob" method="doJob" cron="0/5 * * * * ?"/> <task:scheduled ref="taskJob" method="doJob1" fixed-rate="5000"/> <task:scheduled ref="taskJob" method="doJob2" fixed-delay="5000"/> </task:scheduled-tasks> </beans>