spring之任务调度

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值