Java_spring_定时执行任务
> 版本说明
<dependencies><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.webflow</groupId> <artifactId>spring-webflow</artifactId> <version>2.3.4.RELEASE</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.0</version> </dependency> </dependencies>
> 搭建最简单的Spring定时任务工程
Spring定时任务,给人的第一感觉就是简洁(>_<)
所需要的JAR,参考以上“版本说明”的POM文件,当然,不嫌麻烦,也可以一个个去下载。
把Spring通过web.xml注册进来
<context-param><param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/spring.xml</param-value> </context-param> <listener> <description>Spring Context</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
当然,需要告诉Spring去哪儿扫描组件。对了,也要告诉Spring我们是使用注解方式注册任务的
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:component-scan base-package="com.nicchagil.*"/> <task:annotation-driven/> </beans>
附logback的配置
<configuration><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --><encoder><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern></encoder></appender><appender name="FILE" class="ch.qos.logback.core.FileAppender"><file>D:/logs/application.log</file><encoder><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>
好了,注册一个简单的任务,它每逢0秒执行,打印一个语句
package com.nicchagil.springtask; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component; @Componentpublic class MyFirstSpringJob { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron = "0 * * * * ?") public void run() { logger.info("MyFirstSpringJob trigger..."); } }
如无意外,启动项目后,可见日志大致如下
22:42:00.024 [pool-1-thread-1] INFO c.n.springtask.MyFirstSpringJob - MyFirstSpringJob trigger...22:43:00.002 [pool-1-thread-1] INFO c.n.springtask.MyFirstSpringJob - MyFirstSpringJob trigger...
> 如果你同时执行多个任务,且某些任务耗时较长,要配线程池
如题。比如,你设置了12:00触发A任务、12:05触发B任务,如果A任务需耗时10分钟,并且没设置线程池,那么B任务有可能会被推迟到12:10以后再执行哦。
所以,这些情况,我们需设置线程池
<task:annotation-driven scheduler="myScheduler"/> <task:scheduler id="myScheduler" pool-size="20"/>
注:具体池的大小,视项目情况而定
将任务改为如下测试
第一个任务
package com.nicchagil.springtask;import java.util.concurrent.TimeUnit;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyFirstSpringJob { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron = "0 * * * * ?") public void run() { logger.info("MyFirstSpringJob trigger..."); /* 模拟此Job需耗时5秒 */ try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } }
第二个任务
package com.nicchagil.springtask;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MySecondSpringJob { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron = "3 * * * * ?") public void run() { logger.info("MySecondSpringJob trigger..."); } }
由日志可以看出,第一个任务由一个线程执行;而第二个任务启动时,由于第一个任务还未完成,则由另外一个线程执行
22:49:00.023 [myScheduler-1] INFO c.n.springtask.MyFirstSpringJob - MyFirstSpringJob trigger... 22:49:03.002 [myScheduler-2] INFO c.n.springtask.MySecondSpringJob - MySecondSpringJob trigger...
转载于:https://www.cnblogs.com/MarchThree/p/5004724.html
Java_spring_定时执行任务相关推荐
- NodeJS 使用redis实现定时执行方法
NodeJS 使用redis实现定时执行任务 文章目录 NodeJS 使用redis实现定时执行任务 场景 使用Redis定时器解决 Redis定时器 Redis发布订阅 操作 nodejs代码 主意 ...
- php如何定时执行任务
PHP的实现决定了它没有Java和.Net这种AppServer的概念, 而http协议是一个无状态的协议, php只能被用户触发, 被调用, 调用后会自动退出内存, 没有常驻内存, 就没有办法准确的 ...
- php 判断是不是前一天,PHP开发中,定时执行如何判断之前的脚本是否跑完?
本篇讲讲在PHP开发中,定时执行如何判断之前的脚本是否跑完? 在PHP开发中,有时我们有这样一种需求 一个脚本 定时每两分钟执行一次 可是在下一个脚本循环执行时 上面一个脚本还没跑完 我们就应该取消当 ...
- 使用sae定时执行Python脚本
使用sae定时执行Python脚本 使用sae定时执行Python脚本 12,May,2014 | 57 Views 毕设压力略大,必须是桂林游的锅.去之前放松了几天,回来又休闲了几天,加上桂林的一周 ...
- iOS: 零误差或极小误差的定时执行或延迟执行?
问题如下: 节奏类游戏需要执行很多的跟音乐节拍相关的操作,并且为了保证节奏感,需要让操作跟节拍的关系十分紧密.对两者间隔要求不能超过0.02秒或更低. 目前使用了 GCD 中的 asyncAfter( ...
- springboot定时执行任务
1.首先在启动类上加上注解 @EnableScheduling 2.在你想要定时执行的方法上加cron表达式著名 PS:看一下控制台打印 PS:把想要定时执行的任务写在此方法中即可 3.最后再附上co ...
- 利用Cache,asp.net 简单实现定时执行任务
利用Cache,让asp.net 简单实现定时执行任务 代码 private static CacheItemRemovedCallback OnCacheRemove = null; protect ...
- java web每天定时执行任务(四步轻松搞定)
第一步: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
- mysql怎么执行任务_Mysql怎么定时执行任务
我想实现每隔30秒执行以下下面sql update userinfo SET endtime=now()WHERE id='155'; 如何让mysql定时执行上面的sql语句呢! 一.查看event ...
最新文章
- 海南大学植物保护学院刘铜教授课题组招聘简介
- logging日志配置,day95下午
- shardingjdbc全局表_Sharding-JDBC动态分表实现
- iOS ASIHttpRequest 封装
- VTK:PolyData之ImplicitDataSetClipping
- OutOFMemoryError
- python3遍历选中文件夹下的文件【GUI编程】
- [WCF]利用net.tcp傳輸協定來建置WCF Service
- 关于eclipse项目的x号报错的一些问题
- 入门云虚拟主机,为你的业务快速实现数据备份和数据恢复
- cdh mysql sqoop 驱动_大数据技术之Sqoop学习——原理、安装、使用案例、常用命令...
- win7查看隐藏文件_Win8系统查看隐藏文件的操作方法是什么?
- 用.net实现按透明度生成水印文件
- 使用Sigar做后台服务器管理时,遇到的linux上的问题
- 数学类网站、代码(Matlab Python R)、编程站点
- Android开发环境搭建ADT-Bundle集成IDE及Hello World
- 中标麒麟linux系统安装打印机_安装国产Linux中标麒麟操作系统教程
- [clear] python 种子转磁力链
- 城市中心、华为、软通动力智慧城市联合解决方案发布
- Unity-ArrayList,List,HashTable,Dictionary
热门文章
- Vue默认插槽、具名插槽、作用域插槽及使用作用域插槽删除列表项
- Spring MVC前后台交互(前台ajax传递数据,后台controller接收数据返回json对象)
- Android底部导航栏的实现(RadioGroup和Fragment结合使用)
- Python创建多线程(join线程同步)
- VB 一个API方式存取日志文件的模块
- VB如何直接显示内存中的二进制图像数据
- SCPPO(十):网站发布中的问题锦集—手动发布网站
- 苹果被曝寻求收购Drive.ai:吴恩达参与运营,多家中国VC投资
- 蔚来2018年平均每天亏掉2641万,车辆交付预期环比腰斩
- 华为5G折叠屏手机,外媒猜长这样