<?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定时任务的更多相关文章

  1. Spring task定时任务执行一段时间后莫名其妙停止的问题

    前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...

  2. Spring Task 定时任务

    所谓定时任务.就是依据我们设定的时间定时运行任务,就像定时发邮件一样,设定时间到了.邮件就会自己主动发送. 在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task.同 ...

  3. Spring Task定时任务的配置和使用详解

    spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.or ...

  4. Quartz和Spring Task定时任务的简单应用和比较

    看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...

  5. Spring Task定时任务Scheduled

    Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...

  6. spring boot 之 spring task(定时任务)

    cron:通过表达式来配置任务执行时间cron表达式详解 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素.按顺序依次为: 秒(0~59)分钟(0~59)3 小时(0~23)4  天(0 ...

  7. Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)

    原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...

  8. Spring Task中的定时任务无法注入service的解决办法

    1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...

  9. Spring 使用介绍(十二)—— Spring Task

    一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...

随机推荐

  1. 【小白的CFD之旅】06 流体力学基础

    从黄师姐那里了解到要学习CFD的话,需要先补充流体力学.数学以及计算机方面的常识,小白就一阵头大.想起当初自己已经把牛皮吹出去了,现在都不知道怎么收场,一个月入不了门多丢人.不过头大归头大,小白还是老 ...

  2. Struts2核心技术简介

    Struts2核心技术简介 使用Struts2框架,只要注重以下三大元素:配置文件.映射文件和Action: 全局属性文件struts.properties:保存系统运行的一些参数变量,整个系统只有一 ...

  3. 图像柔光效果(SoftGlow)的原理及其实现。

    图像柔光效果在很多商业软件中都有实现,比如美图秀秀,光影魔术手等.其能针对原始图像产生一副新的比较平滑感觉光线比较柔和的效果,给人一种朦胧美,如下面几幅图所示:                     ...

  4. BZOJ 3343: 教主的魔法 [分块]【学习笔记】

    3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1172  Solved: 526[Submit][Status][Discus ...

  5. NOIP2015斗地主[DFS 贪心]

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  6. Ubuntu14.0下安装Zend Framework 2

    Ubuntu14.0下安装Zend Framework 2为了安装这个东西,忙活了快一天了,参考中文博客一直没有安装成功,有些博客的时间也是已经很早了,后来google看英文版的才安装成功,这里记录一 ...

  7. [No000081]SVN学习笔记1-服务端搭建

    目录 一:SVN服务器搭建和使用. 1.首先来下载和搭建SVN服务器,地址http://subversion.apache.org/packages.html 2.安装完成后,启动VisualSVN ...

  8. 微信小程序之触控事件(四)

    [未经作者本人同意,请勿以任何形式转载] >>>什么是事件 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执 ...

  9. 使用工具安装,运行,停止,卸载Window服务

    WSWinForm.exe介绍 WSWinForm.exe是我自己开发的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说WSWinForm只是其注册程序的服务外壳,这个特性对 ...

  10. JavaScript定时器原理分析

    .header { cursor: pointer } p { margin: 3px 6px } th { background: lightblue; width: 20% } table { t ...