SpringMVC中定时任务配置
在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等。
以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的。
现在使用SpringMVC之后,一起都变得简单了o(∩_∩)o
有两种配置方式,我都分别讲讲,但是看了后你肯定只会选择后面那种,没错! 我也是用后面那种方式
第一种配置方式:这个比较复杂,配置的地方有点多,稍不留意就不成功,具体看代码了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 每隔一个小时重新获取一次token -->
<!-- 1.要调用的工作类 -->
<bean id="refreshAccessTokenJob" class="com.xiao.weixin.quartz.RefreshAccessToken"/> <!-- 2.定义调用对象和调用对象的方法 -->
<bean id="refreshAccessTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="refreshAccessTokenJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean> <!-- 3.定义触发时间 -->
<bean id="refreshAccessToken" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="refreshAccessTokenTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 */2 * * ?</value>
</property>
</bean> <!-- 4.总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="refreshAccessToken"/>
</list>
</property>
</bean>
</beans>
第二种配置方式:强烈推荐的这种
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<task:scheduled-tasks>
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
</task:scheduled-tasks> <bean id="quartzTestBean" class="xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/>
</beans>
这里面很简单,直接调用service接口实现类中的方法就可以了,
maven配置中加上
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
比如我的实现类是这样的:
package xiaochangwei.zicp.net.service; import java.util.Date; import org.springframework.stereotype.Service; import xiaochangwei.zicp.net.common.tools.DateUtil; @Service
public class QuartzTestServiceImpl implements QuartzTestService { public void quartzJobTestMethod() {
System.out.println("定时任务执行:" + DateUtil.getFormatDate(new Date()));
} }
每隔五秒打印一个当前时间
执行结果如下:
定外配置任务多久执行也很简单:
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
上面这个表示五秒钟执行一次
总共有五个*,从前往后表示秒,分,时。。。。。。。
多少秒执行一次说了,说多少分执行一次
cron="* */2 * * * ?" 对么? 这样不对哈,要将前面的星改为0 cron="0 */2 * * * ?" 表示两分执行一次 同理 如果两小时执行一次,则前面两个都是0 哦 具体规则请百度cron吧 so easy !
SpringMVC中定时任务配置的更多相关文章
- 【转】SpringMVC中DispatcherServlet配置中url-pattern 配置/*和/的区别
原文地址:http://m.blog.csdn.net/blog/liuxiao723846/43733287 在使用springmvc时,都会在web.xml中配置一个dispatchservlet ...
- Velocity 语法及其在springMVC中的配置
强烈推荐具体的整合博客:http://blog.csdn.net/duqi_2009/article/details/47752169 整合文章中有几处问题: xml中配置的vm视图解析器,应该按照本 ...
- SpringMVC 中xml 配置多数据源
1,配置jdbc.properties jdbc.driver_one=... jdbc.url_one=..... jdbc.username_one=... jdbc.password_one=. ...
- springMVC中Dispatcher中的/和/*的区别
1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个Dispatc ...
- JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术
在上篇博客中,我们聊了<JavaEE开发之SpringMVC中的自定义拦截器及异常处理>.本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js.css这些静态文件的加载配置 ...
- springmvc两种配置方法
基于配置文件xml方式, 配置springmvc步骤: 1.在pom文件中引入jar包: <!--导入springmvc的jar包--> <dependency> <gr ...
- 在springmvc中配置jedis:
主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...
- 第五节 关于SpringMVC中Ajax的配置和应用[下午]
成熟,不是学会表达,而是学会咽下,当你一点一点学会克制住很多东西,才能驾驭好人生. 还有一周,祥云19就算结算了,一个半月的相处希望,胖先生算一个合格的老师 小白,小蔡,2婷婷,小猴,小恒,小崔,小龙 ...
- SpringMVC中采用简洁的配置实现文件上传
文件上传我们一般会有两种策略,一种是通过IO流上传,还有一种是通过表单上传,其实这两种在客户端实现起来都是很简单的,在服务端处理会略有差别,个人感觉IO上传代码简单,但是也有很多硬伤,还是表单上传更合 ...
随机推荐
- TODO:Laravel 内置简单登录
TODO:Laravel 内置简单登录 1. 激活Laravel的Auth系统Laravel 利用 PHP 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- 05.LoT.UI 前后台通用框架分解系列之——漂亮的时间选择器
LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...
- iOS逆向工程之App脱壳
本篇博客以微信为例,给微信脱壳."砸壳"在iOS逆向工程中是经常做的一件事情,,因为从AppStore直接下载安装的App是加壳的,其实就是经过加密的,这个“砸壳”的过程就是一个解 ...
- SDWebImage源码解读 之 SDWebImageCompat
第三篇 前言 本篇主要解读SDWebImage的配置文件.正如compat的定义,该配置文件主要是兼容Apple的其他设备.也许我们真实的开发平台只有一个,但考虑各个平台的兼容性,对于框架有着很重要的 ...
- [开发笔记]GCC 分支预测优化
#define likely(x) __builtin_expect(!!(x),1)#define unlikely(x) __builtin_expect(!!(x),0) 用于优化在做分支判断的 ...
- Postman - 功能强大的 API 接口请求调试和管理工具
Postman 是一款功能强大的的 Chrome 应用,可以便捷的调试接口.前端开发人员在开发或者调试 Web 程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的 Fi ...
- 安卓客户端a标签长按弹框提示解决办法
昨天工作时候发现一个bug,是关于a标签的,在安卓客户端中,如果是a标签的话,长按会出现一个弹框,如图所示 是因为安卓客户端的长按触发机制,以后进行wap端开发的时候,如果用到跳转页面尽量不要用a标签 ...
- HashMap的工作原理
HashMap的工作原理 HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间 ...
- .Net 初步学习笔记之一——.Net 平台与.Net FrameWork框架的关系
.Net 包含两部分 .Net平台 和.Net FrameWork 框架 1..Net FrameWork框架包含于.Net平台. .Net FrameWork提供环境和支撑保证.Net平台运行. 2 ...