使用Spring自带的定时执行任务工具执行任务
2015-12-02 13:48
645 查看
1.首先写一个任务类:
@Component("taskJob")
public class DeleteTableRecord {
@Scheduled(cron = "0 0 3 * * ?")
public void job() {
System.out.println("正在执行任务....");
//要执行的任务
System.out.println("任务执行完成.");
}
}
@Scheduled(cron = "0 0 3 * * ?")表示在每天凌晨三点执行任务
@Scheduled(cron = "0 15 3 * * ?")表示每天凌晨三点十五分执行任务
2.在Spring配置文件中加入如下配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<!-- 扫描Task所在包-->
<context:component-scan base-package="com.mis.task" />
<!-- Spring支持@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy" />
<!--Task的配置-->
<task:scheduler id="qbScheduler" pool-size="1" />
<bean>
...
</bean>
</beans>
@Component("taskJob")
public class DeleteTableRecord {
@Scheduled(cron = "0 0 3 * * ?")
public void job() {
System.out.println("正在执行任务....");
//要执行的任务
System.out.println("任务执行完成.");
}
}
@Scheduled(cron = "0 0 3 * * ?")表示在每天凌晨三点执行任务
@Scheduled(cron = "0 15 3 * * ?")表示每天凌晨三点十五分执行任务
2.在Spring配置文件中加入如下配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<!-- 扫描Task所在包-->
<context:component-scan base-package="com.mis.task" />
<!-- Spring支持@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy" />
<!--Task的配置-->
<task:scheduler id="qbScheduler" pool-size="1" />
<bean>
...
</bean>
</beans>
相关文章推荐
- Java并发编程系列之四:volatile和锁的内存语义
- day_31Java注解---Annotation
- Resultset转Bean工具类
- LoadRunner 调用webservice Internal error, please call customer support. Details: class java.lang.Objec
- JAVA类内存分配
- java线程wait,notify,yield,join方法
- Java编程艺术第三章
- 斐波那契数列(fabnacci)java实现
- MyEclipse (2015) 中创建简单的Maven项目的步骤(用于生成可执行jar文件)
- MyEclipse 2015优化技巧
- 在MyEclipse2015中上传项目到github的步骤
- Java开发环境
- java多线程同步 synchronized
- 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】
- 关于在java编程中遇到的一些细节学习(杂1)
- Spring-Struts2整合异常:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi
- Java语言基础之文件读写
- Java集合
- Java语言基础之正则表达式
- Java中继承thread类与实现Runnable接口的区别