spring之任务调度

来源:互联网 发布:恒大淘宝 编辑:程序博客网 时间:2024/06/02 22:46

spring之任务调度: 在指定时间执行操作,或周期性的重复执行操作实现方式(两种方式):

1.   使用quartz 插件

2.   spring3.0后版本中集成


主要介绍第二种使用步骤

1.   引入命名空间

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-4.1.xsd

注意:有些时候spring-task-4.1.xsd这个版本不行,可以改成spring-task-4.0.xsd

版本 ,或者再改成其他版本试试。


2.在applicationContext.xml中配置

<!-- 调度任务方法所在的类 -->      <beanid="task" class="com.test.Task"/>      <!—执行任务的线程池(数) -->      <task:schedulerid="one" pool-size="10"/>      <!--调度任务 使用cron表达式描述执行任务时间,具体使用,百度一下很清晰-->      <task:scheduled-tasksscheduler="one">           <task:scheduledref="task" method="show" cron="0/2 * * * *?"/></task:scheduled-tasks>



3.测试类

package com.test;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");}public void show() {SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss:SSSS");System.out.println(format.format(new Date()));}}

测试结果:



通过以上操作, 实现了每隔两秒的任务调度  打印输出 日期


0 0
原创粉丝点击