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 ...
随机推荐
- redis 集群创建常见几个问题
Redis配置集群遇到问题及解决方法 配置完所有主节点后,报" ERR Invalid node address specified" 由于Redis-trib.rb 对域名或 ...
- 教你看懂 OpenStack 日志 - 每天5分钟玩转 OpenStack(29)
instance 从创建到删除的整个生命周期都是由 Nova 管理的. 后面各小节我们以 instance 生命周期中的不同操作场景为例,详细分析 Nova 不同组件如何协调工作,并通过日志分析加深大 ...
- x01.Weiqi.8: 一点改进
原来的代码全部删除,进行了深层次重构,得其意而忘其言.得意之处有二: 1.关于显示 以 StoneSize 属性为依托,在 set 中加了一句:Width = Height = m_StoneSize ...
- Redis学习笔记3-Redis5个可执行程序命令的使用
在redis安装文章中,说到安装好redis后,在/usr/local/bin下有5个关于redis的可执行程序.下面关于这5个可执行程序命令的详细说明. redis-server Redis服务器的 ...
- JS魔法堂:ES6新特性——GeneratorFunction介绍
一.前言 第一次看koajs的示例时,发现该语句 function *(next){...............} ,这是啥啊?于是搜索一下,原来这是就是ES6的新特性Generator ...
- 用CSS开启硬件加速来提高网站性能
国外一篇文章,有点意思,转载过来,准备尝试下~ 中文地址:http://www.cnblogs.com/rubylouvre/p/3471490.html 原文地址:http://blog.teamt ...
- CH Round #30 摆花[矩阵乘法]
摆花 CH Round #30 - 清明欢乐赛 背景及描述 艺术馆门前将摆出许多花,一共有n个位置排成一排,每个位置可以摆花也可以不摆花.有些花如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看 ...
- Spring远程调用技术<2>-Hessian和Burlap
上篇谈到RMI技术,加上Spring的封装,用起来很方便,但也有一些限制 这里的Hessian和Burlap解决了上篇提到的限制,因为他们是基于http的轻量级远程服务. Hessian,和RMI一样 ...
- no-proxy 和proxy 的区别
Child <- many-to-one ->Parent class Child { private Parent paren ...
- eclipse自动提示功能没了的解决办法(转载)
eclipse自动提示功能没了的解决办法 标签: eclipse联想 2012-08-09 14:32 24687人阅读 评论(7) 收藏 举报 分类: Android(38) 版权声明:本文为博 ...