在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等。

以前在从事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中定时任务配置的更多相关文章

  1. 【转】SpringMVC中DispatcherServlet配置中url-pattern 配置/*和/的区别

    原文地址:http://m.blog.csdn.net/blog/liuxiao723846/43733287 在使用springmvc时,都会在web.xml中配置一个dispatchservlet ...

  2. Velocity 语法及其在springMVC中的配置

    强烈推荐具体的整合博客:http://blog.csdn.net/duqi_2009/article/details/47752169 整合文章中有几处问题: xml中配置的vm视图解析器,应该按照本 ...

  3. SpringMVC 中xml 配置多数据源

    1,配置jdbc.properties jdbc.driver_one=... jdbc.url_one=..... jdbc.username_one=... jdbc.password_one=. ...

  4. springMVC中Dispatcher中的/和/*的区别

    1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个Dispatc ...

  5. JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术

    在上篇博客中,我们聊了<JavaEE开发之SpringMVC中的自定义拦截器及异常处理>.本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js.css这些静态文件的加载配置 ...

  6. springmvc两种配置方法

    基于配置文件xml方式, 配置springmvc步骤: 1.在pom文件中引入jar包: <!--导入springmvc的jar包--> <dependency> <gr ...

  7. 在springmvc中配置jedis:

    主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...

  8. 第五节 关于SpringMVC中Ajax的配置和应用[下午]

    成熟,不是学会表达,而是学会咽下,当你一点一点学会克制住很多东西,才能驾驭好人生. 还有一周,祥云19就算结算了,一个半月的相处希望,胖先生算一个合格的老师 小白,小蔡,2婷婷,小猴,小恒,小崔,小龙 ...

  9. SpringMVC中采用简洁的配置实现文件上传

    文件上传我们一般会有两种策略,一种是通过IO流上传,还有一种是通过表单上传,其实这两种在客户端实现起来都是很简单的,在服务端处理会略有差别,个人感觉IO上传代码简单,但是也有很多硬伤,还是表单上传更合 ...

随机推荐

  1. a标签点击跳转失效--IE6、7的奇葩bug

    一般运用a标签包含img去实现点击图片跳转的功能,这是前端经常要用到的东西. 今天遇到个神奇的bug:如果在img上再包裹一层div,而且div设置了width和height,则图片区域点击时,无任何 ...

  2. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  3. Python高手之路【二】python基本数据类型

    一:数字 int int(整型): 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...

  4. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  5. centos7+mono4+jexus5.6.2安装过程中的遇到的问题

    过程参考: http://www.linuxdot.net/ http://www.jexus.org/ http://www.mono-project.com/docs/getting-starte ...

  6. svn 常用命令总结

    svn 命令篇 svn pget svn:ignore // 查看忽略项 svn commit -m "提交说明" // 提交修改 svn up(update) // 获取最新版本 ...

  7. 深入理解CSS六种颜色模式

    前面的话 赏心悦目的颜色搭配让人感到舒服,修改元素颜色的功能让人趋之若鹜.但颜色规划不当,会让网站用户无所适从.颜色从<font color="">发展至今,保留了很多 ...

  8. 敏捷转型历程 - Sprint4 回顾会

    我: Tech Leader 团队:团队成员分布在两个城市,我所在的城市包括我有4个成员,另外一个城市包括SM有7个成员.另外由于我们的BA离职了,我暂代IT 的PO 职位.PM和我在一个城市,但他不 ...

  9. mysql-5.6.34 Installation from Source code

    Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...

  10. php利用root权限执行shell脚本

    vi /etc/sudoers , 为apache用户赋予root权限,并且不需要密码,还有一步重要的修改(我被困扰的就是这个地方) root  ALL=(ALL)  ALL apache  ALL= ...