您的位置:首页 > 编程语言 > Java开发

使用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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: