spring @sheduled小例

可以用来做一些定时小任务。例子如下:

项目结构:

beans.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.xsd   
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/task
		http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<context:component-scan base-package="com.oliver.schedule" />

	<task:executor id="myExecutor" pool-size="5" />
	<task:scheduler id="myScheduler" pool-size="10" />
	<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />

	<context:annotation-config/>

</beans>  

ShowTime.java

package com.oliver.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ShowTime {
	
	static int i = 10;
	static int j = 10;
	
	/**
	 * web应用启动1秒后,每隔5秒运行以下方法
	 */
	@Scheduled(initialDelay=1000,fixedDelay=5000)
	public void show(){
		i--;
		if(i > 0) {
			System.out.println(getTime());
			System.out.println("show1 = "+i);
		}
	}
	
	
//	cron expression       自定义规则
//	参数顺序
//	秒  分   时   日  月  星期
//	"0 0 * * * *"          = the top of every hour of every day.
//	"*/10 * * * * *"       = every ten seconds.
//	"0 0 8-10 * * *"       = 8, 9 and 10 o'clock of every day.
//	"0 0 6,19 * * *"       = 6:00 AM and 7:00 PM every day.
//	"0 0/30 8-10 * * *"    = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
//	"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
//	"0 0 0 25 12 ?"        = every Christmas Day at midnight
	
	/**
	 * 10秒执行一次
	 */
	@Scheduled(cron="0/10 * * * * *")
	public void show2() {
		j--;
		if(j > 0) {
			System.out.println(getTime());
			System.out.println("show2 = "+j);
		}
	}
	
	public String getTime() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}	
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>tourguide</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

直接丢服务器,运行结果如下:

22:36:52
show1 = 9
22:36:57
show1 = 8
22:37:00
show2 = 9
22:37:02
show1 = 7
22:37:07
show1 = 6
22:37:10
show2 = 8
22:37:12
show1 = 5
22:37:17
show1 = 4
22:37:20
show2 = 7
22:37:22
show1 = 3
22:37:27
show1 = 2
22:37:30
show2 = 6
22:37:32
show1 = 1
22:37:40
show2 = 5
22:37:50
show2 = 4
22:38:00
show2 = 3
22:38:10
show2 = 2
22:38:20
show2 = 1

参考文档:

Scheduling Tasks



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值