亲测可用

原文网址: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. 解决Eclipse中新创建的Maven项目不自动创建web.xml文件

    1. 通过J2EE tools 2.项目右键-properties-project facets-勾选dynamic web moudle

  2. js字符串的裁剪

    一.JavaScript字符串的处理方法 1.split()  功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子:   str=”jpg|bmp|gif|ico|png”; arr=str. ...

  3. python线程的GIL问题(全局解释器锁)

    造成原因: python ---> 支持线程操作 --->IO的同步和互斥 --> 加锁 ----> 超级锁,给解释器加锁--->解释器同一时刻只能解释一个线程 造成的后 ...

  4. Ext.js 之MVC

    Ext.js 4.0之MVC

  5. strapi 开源api && 内容管理平台试用

    strapi 是一个开源的api && 内容管理平台,功能操作起来还是比较方便简单的. 安装 使用docker && docker-compose 代码clone gi ...

  6. DB2经常使用命令

    1.打开命令行窗体 #db2cmd 2.打开控制中心 # db2cmd db2cc 3.打开命令编辑器 db2cmd db2ce =====操作数据库命令=====   4.启动数据库实例 #db2s ...

  7. Spring集成Mybatis(Dao方式开发)

    Spring整成Mybatis注意事项:  1. 关键jar包不能少 2.可以单独整理好Mybatis框架,测试无误再集成Spring 3.集成时,参数级别的细节可以选择忽略,但思路必须清晰 代码如下 ...

  8. 【转】嵌入式Linux文件系统启动脚本及分析

    原文网址:http://www.linuxidc.com/Linux/2011-03/33728.htm 在内核初始化完成后,嵌入式linux 文件系统的启动过程主要包含以下几个步骤: 1. 执行/s ...

  9. wamp ssl配置https

    主要参考的这个博客 http://blog.csdn.net/sdq4700/article/details/36173665 配置完以后重启wamp , https不能正常访问 apache.log ...

  10. Android网络请求

    HTTP请求与响应 HTTP请求包结构 例: POST /meme.php/home/user/login HTTP/1.1 Host: 114.215.86.90 Cache-Control: no ...