转载《spring定时任务轮询(spring Task)》
亲测可用
原文网址:http://blog.csdn.net/wanglha/article/details/51026697
本博主注:xmlns:task="http://www.springframework.org/schema/task"
原文:
定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次。
以下用spring注解配置定时任务。
1、添加相应的schema
|
1
2
3
4
|
xmlns:task="xsi:schemaLocation=" |
完整schema如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans</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 * */@Servicepublic 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)》的更多相关文章
- 转载《Android LayoutInflater详解》
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...
- Android LayoutInflater详解(转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater详解 (转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android——LayoutInflater详解
在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...
- <转> Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- [ 转载 ] Android设计模式详解
从Android再来认识23种设计模式 ReadyShow 关注 0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
随机推荐
- Winform创建解决方案
Winform的开发工具可以使用VS2005---VS2013,版本在不断升级,VS的功能也越来越强大.本系列文章采用VS2012(以后全称VS)演示. 窗体是winform开发的基础,需要掌握窗体的 ...
- Winform工具栏ToolStrip和状态栏StatusStrip
工具栏和状态栏的设置与前面的菜单基本相似 1.ToolStrip 从工具栏选项卡上拖一个放入窗体即可,位置也是通过其Dock设置的.先上一张图,红色部分就是工具栏,工具栏中可以放置如图下拉菜单中的Bu ...
- Zabbix(Windows)
Windows环境下只能装客户端, 而不能装服务端; zabbix官网 https://www.zabbix.com/ 下载并解压到本地 修改配置文件: # Hostname=Hostname=DES ...
- Nginx配置IPv6端口监听及务器设置IPV6及Https支持并通过AppStore审核
一.监听端口 从Nginx 1.3的某个版本起,默认ipv6only是打开的,所以,我们只需要在监听中加入ipv6监听即可,不过推荐都手动加上比较好,代码如下: listen [::]: ipv6on ...
- macOS --- 配置基于域名的虚拟主机
在终端运行 sudo vi /Applications/XAMPP/xamppfiles/etc/httpd.conf,打开apache配置文件. 在httpd.conf中找到"#Inclu ...
- php 自定义函数大全
1. call_user_func和call_user_func_array 以上两个函数以不同的参数形式调用函数.见如下示例: <?php class demo{ public static ...
- CentOS6.5 下Haproxy服务的安装与配置
参考网站: http://wenku.baidu.com/link?url=57AsCAL8TIv8NC3Vdnpd0hQ4fGNls8RFikjRWna3OaZb6qGHYTdV-4_wQPuzv8 ...
- memcached内存管理机制分析
memached是高性能分布式内存对象系统,通过在内存中存储数据对象来减少对磁盘的数据读取次数,提高服务速度. 从业务需求出发.我们通过一条命令(如set)将一条键值对(key,value)插入mem ...
- 黄聪:C#程序中判断是否处在DEBUG调试状态或者RELEASE发布状态
习惯了用老方式(注释的方式)来对程序进行调试,不过昨天才发现这样调试存在很大的隐患:在工程发布的时候如果忘记把该注释的代码注释掉,而让这些调试信息随工程一起发布,如果是可见的调试信息倒好发现,如果不是 ...
- 修改Gradle 和Maven本地仓库的位置方法
本文转载自:https://www.cnblogs.com/dwb91/p/6523541.html 关于Maven的配置: 用过Maven的开发人员应该知道Maven可以通过配置 conf文件夹下面 ...