亲测可用

原文网址:http://blog.csdn.net/wanglha/article/details/51026697

本博主注:xmlns:task="http://www.springframework.org/schema/task"

原文:

定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次。

以下用spring注解配置定时任务。
1、添加相应的schema

  

完整schema如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
</beans>

  

2、配置自动调度的包和定时开关

1
2
3
4
5
6
<!-- 注解扫描包 -->
<context:component-scan base-package="com.ljq.web.controller.annotation" />
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />

  

3、添加调度测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ljq.web.controller.annotation;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
/**
 * 调度测试类(每隔5秒执行一次)
 *
 * @author Administrator
 *
 */
@Service
public class TaskTest {
    @Scheduled(cron = "0/5 * * * * ? ")
    public void myTestWork() {
        System.out.println(System.currentTimeMillis());
    }
}

20180707

可用@Component代替@Service,以注入Service层

package com.bjbr.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import com.bjbr.service.SendWxService; @Component
@Lazy(false)
public class SendWxTask {
private static final Logger logger = LoggerFactory.getLogger(SendWxTask.class);
@Autowired
private SendWxService sendWxService;
/**
* @todo 每天七点
* @author zhangyanan
* @datetime 2018年7月3日下午5:09:31
*/
@Scheduled(cron = "0 0 7 * * ?")
public void everyDayWork() {
logger.debug("----------进入everyDayWork提醒------------");
}
}

可以看到cron表达式与之前的不同,新手可能不懂,解释一下

cron = "0 0 7 * * ?"
cron = "0/5 * * * * ? "

String []s=cron.split(" ");
s[0]:秒
s[1]:分
s[2]:时
s[3]:日
s[4]:月
s[5]:星期
【s[6]】:年
注意s[6],cron表达式在线生成器是可以解析运行的,但实际情况并不是
@这篇文章以源码的形式展示了spring cron表达式只支持6 fields,实际运用的时候不要出现
cron = "0 0 7 * * ? 2018"这样的形式,否则会报异常:
java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 7 in****** 附:cron表达式在线生成器

转载《spring定时任务轮询(spring Task)》的更多相关文章

  1. 转载《Android LayoutInflater详解》

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  2. Android LayoutInflater详解

      在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...

  3. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. Android LayoutInflater详解 (转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. Android——LayoutInflater详解

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...

  6. <转> Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  7. [ 转载 ] Android设计模式详解

    从Android再来认识23种设计模式 ReadyShow 关注  0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...

  8. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  9. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  10. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

随机推荐

  1. vue.js 源代码学习笔记 ----- helpers.js

    /* @flow */ import { parseFilters } from './parser/filter-parser' export function baseWarn (msg: str ...

  2. Linux:LNMP架构的搭建

    LNMP架构的搭建 centos6.8-i686 MySQL PHP Nginx 搭建前先安装一些必要的rpm和php组件(全新系统) yum install -y wget gcc vim* lib ...

  3. 故障处理:磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1

    按照阿里云官网教程对云服务器进行磁盘扩容,使用fdisk重新分区,最后使用e2fsck和resize2fs来完成文件系统层面的扩容 在执行“e2fsck -f /dev/vdb1”命令时报错,如果你的 ...

  4. word 使用中 上标符号的实现

    1.   首先在word 中打下一段话  如:   啦啦啦啦啦啦啦啦  然后加入你需要的上标   如   [2] 2.    选中你需要的上标,然后右击 3.   点击字体选项 出现下图: 4.  在 ...

  5. BZOJ4644: 经典傻逼题【线段树分治】【线性基】

    Description 这是一道经典傻逼题,对经典题很熟悉的人也不要激动,希望大家不要傻逼. 考虑一张N个点的带权无向图,点的编号为1到N. 对于图中的任意一个点集 (可以为空或者全集),所有恰好有一 ...

  6. WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)

    Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...

  7. [C#] DataTable转成List集合

    项目开发中,经常会获取到DataTable对象,如何把它转化成一个List对象呢?前几天就碰到这个问题,网上搜索整理了一个万能类,用了泛型和反射的知识.共享如下: public class Model ...

  8. python下的select模块使用 以及epoll与select、poll的区别

    python下的select模块使用 以及epoll与select.poll的区别 先说epoll与select.poll的区别(总结) 整理http://www.zhihu.com/question ...

  9. 【转】在Visual Studio中怎样快速添加代码段

    原文网址:http://blog.csdn.net/yl2isoft/article/details/9735527 以前一直只知道,键入prop,再按两次tab键,会生成自动属性代码. 今天闲着无事 ...

  10. Cacti不出图

    http://os.51cto.com/art/201404/434915.htmhttp://os.51cto.com/art/201109/289306.htmhttp://freeloda.bl ...