转载《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 ...
随机推荐
- cuteftp不能连接虚拟机的解决方法
使用cuteftp在PC和虚拟机之间传送文件 工具/原料 PC机中装有cuteftp软件和虚拟机 注意事项 首先要确保虚拟机中的ftp软件已安装,如未安装可以用如下命令安装 sudo apt-get ...
- Swapping eth0 and eth1 on OK335xS board
/******************************************************************************* * Swapping eth0 and ...
- bootstrap table教程--后台数据绑定、特殊列处理、排序
上一篇文章介绍了基本的使用教程.本节主要介绍Bootstrap的后台数据绑定.特殊列处理及列的排序功能 1.数据绑定 一般做程序设计,很少是使用json文件直接绑定数据.基本上我们都是使用编程语言进行 ...
- 设置Nginx日志
在nginx.conf文件或vhosts/*.conf文件中的access_log日志中指定级别为main. http { include mime.types; default_type appli ...
- 在Flask中使用Celery的最佳实践
写在前面 本最佳实践是基于作者有限的经验,欢迎大家共同讨论,可以持续维护此最佳实践.另本文中所使用的环境为Mac&Ubuntu环境,软件版本如下: Celery (4.1.0) Flask ( ...
- nexus docker 私有镜像处理
新版本的nexus 可以进行docker 镜像的存储处理 配置私有镜像(host 模式) 修改docker 非安全镜像处理 { "registry-mirrors": [" ...
- C#拦截系统消息
首先我们看下有哪几种拦截系统消息的方法: //一.截取系统消息//方法一://添加监视消息private void Form_Load(object sender, System.EventArgs ...
- sublime 下面开发
sublime 下面开发 开发 ptyon 简单环境 1. 下载sublime 3 https://download.sublimetext.com/Sublime%20Text%20Build%20 ...
- Linux下Moodle平台的快速安装方案
一种快速安装与配置Moodle平台的方案,基本步骤: 1.选择与安装Linux系统 2.配置网络,开启shh和网络端口 3.一键安装集成环境(使用oneinstack方案) 4.服务器配置,端口和PH ...
- Makefile中进行宏定义-***
实际上是gcc命令支持-D宏定义,相当于C中的全局#define: gcc -D name gcc -D name=definition Makefile中可以定义变量(和宏很像),但是是给make解 ...