转载《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 ...
随机推荐
- 怎样使用visio画数据库模型图
怎样使用visio画数据库模型图 计算机专业的学生在做程序的需求分析文档或者毕业设计的时候,针对自己开发的系统,肯定少不了要画数据库的模型图.许多同学喜欢用word实现,当然,word可以满足 ...
- Python windows ping
# -*- coding: utf-8 -*- import os # 参考文档: # Ping to a specific IP address using python [duplicate] # ...
- Js 图片轮播渐隐效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- L3-013 非常弹的球 (30 分)
刚上高一的森森为了学好物理,买了一个“非常弹”的球.虽然说是非常弹的球,其实也就是一般的弹力球而已.森森玩了一会儿弹力球后突然想到,假如他在地上用力弹球,球最远能弹到多远去呢?他不太会,你能帮他解决吗 ...
- DesignPattern(三)结构型模式(上)
结构型模式 结构型模式,顾名思义讨论的是类和对象的结构 ,主要用来处理类或对象的组合.它包括两种类型,一是类结构型模式,指的是采用继承机制来组合接口或实现:二是对象结构型模式,指的是通过组合对象的方式 ...
- udev笔记
1.udevd的主配置文件是/etc/udev/udev.conf 2.使用udev来监听U的hot-plug事件 #include <stdio.h> #include <stdl ...
- deno学习一 安装试用&&几个问题解决
基本的依赖可以参考github 我的环境是centos 7 基本安装 需要golang 以及yarn安装 Protobuf 3 这是官方的方式,实际可以变通下 cd ~ wget https:// ...
- 华为OJ:2199 推断输入字符串中的括号匹配
依据不同的括号有个计数器.在遍历时.当计数器小于0则返回false或者当遍历完后,计数器仍旧不为零,也返回false. import java.util.Scanner; public class b ...
- php 数据类型转换与比较
<?php define("PI", 3.1415926); echo PI."<br>"; //定义一个常量 define("GR ...
- bitmapdata的知识点
flashplayer的cpu渲染 bitmapData占用的内存分两块,一块是原始数据区,另一块是解压后的内存区10秒内如果没有使用这个bitmapdata,解压后的内存区会被释放,当10秒后重新使 ...