亲测可用

原文网址: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. hosts 配置

    hosts 配置 在windows上比较好的方法就是在本地配制hosts,在windows/system32/drivers/etc/hosts 下,增加 127.0.0.1 m.t.XXXX.com ...

  2. SQL中注意数据类型对性能的影响

    在数据存储的时候有时我们不太注意字符编码对性能影响,但小问题往往造成很大的影响.在数据量小的时候感觉不出来,一旦上到百万级以上的时候就非常明显了 看下面两个SQL语句 ---SQL1 SELECT * ...

  3. LR 解压缩函数(wgzMemDecompressBuffer)失败 Code=-5

    用LR做压力测试的时候有时会报错 “解压缩函数(wgzMemDecompressBuffer)失败 返回Code=-5”. Google了一把,也没有解决掉. 因为有些脚本运行时没有问题,感觉可能和请 ...

  4. Unable to connect to any of the specified MySQL hosts.

    c#连接Mysql数据建立连接时提示:Unable to connect to any of the specified MySQL hosts. 出现此错误的原因是Server(数据库服务器IP地址 ...

  5. MQTT再学习 -- 搭建MQTT服务器及测试

    最近在搞 PM2.5 采集,需要用到 MQTT 传输协议.协议部分看了几天的,讲的七七八八.本身在 intel 上有 写好的MQTT 的源码,现在的工作其实也就是移植到单片机上或者DM368板卡上.不 ...

  6. [QT]问题记录-QPixmap::scaled 缩放不成功

    解答的帖子: http://bbs.csdn.net/topics/391850818 使用 pix.scaled(400,400)  之后需要将图片返回就可以了.

  7. C#方法(函数)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 从event loop规范探究javaScript异步及浏览器更新渲染时机

    异步的思考 event loops隐藏得比较深,很多人对它很陌生.但提起异步,相信每个人都知道.异步背后的“靠山”就是event loops.这里的异步准确的说应该叫浏览器的event loops或者 ...

  9. 排列算法(reverse...rotate...next_permutation)

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; i ...

  10. 【转】VC++ 也有 Refactoring 功能了

    原文网址:http://blog.csdn.net/yapingxin/article/details/18923095 分类: C++2014-02-04 14:00 2688人阅读 评论(0) 收 ...