Spring task定时任务
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.umgsai</groupId>
<artifactId>spring-cron</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>
Spring配置如下
<?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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 配置注解扫描 -->
<context:component-scan base-package="com.umgsai.spring.task"/>
<task:scheduler id="taskScheduler" pool-size="100" />
<task:scheduled-tasks scheduler="taskScheduler">
<!-- 每半分钟触发任务 -->
<task:scheduled ref="task" method="task1" cron="30 * * * * ?"/>
<!-- 每小时的10分30秒触发任务 -->
<task:scheduled ref="task" method="task2" cron="30 10 * * * ?"/>
<!-- 每天1点10分30秒触发任务 -->
<task:scheduled ref="task" method="task3" cron="30 10 1 * * ?"/>
<!-- 每月20号的1点10分30秒触发任务 -->
<task:scheduled ref="task" method="task4" cron="30 10 1 20 * ?"/>
<!-- 每年10月20号的1点10分30秒触发任务 -->
<task:scheduled ref="task" method="task5" cron="30 10 1 20 10 ?"/>
<!-- 每15秒、30秒、45秒时触发任务 -->
<task:scheduled ref="task" method="task6" cron="15,30,45 * * * * ?"/>
<!-- 15秒到45秒每隔1秒触发任务 -->
<task:scheduled ref="task" method="task7" cron="15-45 * * * * ?"/>
<!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 -->
<task:scheduled ref="task" method="task8" cron="15/5 * * * * ?"/>
<!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 -->
<task:scheduled ref="task" method="task9" cron="15-30/5 * * * * ?"/>
<!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 -->
<task:scheduled ref="task" method="task10" cron="0 0/3 * * * ?"/>
<!-- 星期一到星期五的10点15分0秒触发任务 -->
<task:scheduled ref="task" method="task11" cron="0 15 10 ? * MON-FRI"/>
</task:scheduled-tasks>
</beans>
package com.umgsai.spring.task;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* Created by shangyidong on 16/10/23.
*/
@Component
public class Task {
public void task1(){
System.out.printf("Task: %s, Current time: %s\n", 1, new Date().toLocaleString());
}
public void task2(){
System.out.printf("Task: %s, Current time: %s\n", 2, new Date().toLocaleString());
}
public void task3(){
System.out.printf("Task: %s, Current time: %s\n", 3, new Date().toLocaleString());
}
public void task4(){
System.out.printf("Task: %s, Current time: %s\n", 4, new Date().toLocaleString());
}
public void task5(){
System.out.printf("Task: %s, Current time: %s\n", 5, new Date().toLocaleString());
}
public void task6(){
System.out.printf("Task: %s, Current time: %s\n", 6, new Date().toLocaleString());
}
public void task7(){
System.out.printf("Task: %s, Current time: %s\n", 7, new Date().toLocaleString());
}
public void task8(){
System.out.printf("Task: %s, Current time: %s\n", 8, new Date().toLocaleString());
}
public void task9(){
System.out.printf("Task: %s, Current time: %s\n", 9, new Date().toLocaleString());
}
public void task10(){
System.out.printf("Task: %s, Current time: %s\n", 10, new Date().toLocaleString());
}
public void task11(){
System.out.printf("Task: %s, Current time: %s\n",11, new Date().toLocaleString());
}
}
public class Main {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
}
}
启动spring容器即可。
Spring task定时任务的更多相关文章
- Spring task定时任务执行一段时间后莫名其妙停止的问题
前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...
- Spring Task 定时任务
所谓定时任务.就是依据我们设定的时间定时运行任务,就像定时发邮件一样,设定时间到了.邮件就会自己主动发送. 在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task.同 ...
- Spring Task定时任务的配置和使用详解
spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.or ...
- Quartz和Spring Task定时任务的简单应用和比较
看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...
- Spring Task定时任务Scheduled
Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...
- spring boot 之 spring task(定时任务)
cron:通过表达式来配置任务执行时间cron表达式详解 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素.按顺序依次为: 秒(0~59)分钟(0~59)3 小时(0~23)4 天(0 ...
- Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)
原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...
- Spring Task中的定时任务无法注入service的解决办法
1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
随机推荐
- java请求https地址如何绕过证书验证?
原文http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两种 ...
- 【转】根据中国气象局提供的API接口实现天气查询
本文转载自 老三 的 三叶草 中国气象局提供了三个天气查询的API接口: [1]http://www.weather.com.cn/data/sk/101190101.html [2]http://w ...
- 【Windows编程】系列第七篇:Menubar的创建和使用
上一篇我们学习了利用windows API创建工具栏和菜单栏,与上一篇紧密联系的就是菜单栏,菜单栏是一个大多数复杂一些的Windows应用程序不可或缺的部分.比如下图就是Windows自带的记事本的菜 ...
- centos 7 install python spynner
yum install python-devel yum install libXtst-devel pip install autopy pip install spynner import spy ...
- Facebook不相信所谓的员工能力等级。《长效商业英雄》(《哈佛商业评论》2016年11期),4星。
老牌管理杂志.本期我给4星.以下是书中一些信息的摘抄: 1:爱因斯坦曾说:“任何傻瓜都能让事情更复杂,只有天才能让事情变简单.”单就这一点来看,乔布斯无疑是天才中的天才.#137 2:通过让苹果聚焦于 ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- Spring AOP深入剖析
一.通过代理工厂模式配置通知 ①.前置通知.后置通知: 定义某接口:ISomeService,并自定义方法 public interface ISomeService { public void tr ...
- AR(增强现实) 知识篇
增强现实(Augmented Reality,简称AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动.这种技术估计由1990年 ...
- jquery替换所有逗号
代码: var aa= $("input[name=aa]").val().replace(/\,/g,""); 原文:http://blog.csdn.net ...
- 【跟着子迟品 underscore】JavaScript 中如何判断两个元素是否 "相同"
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...