关于spring中定时器的使用教程

网友投稿 186 2023-05-05


关于spring中定时器的使用教程

前言

在很多实际的web应用中,都有需要定时实现的服务,如每天12点推送个新闻,每隔一个小时提醒用户休息一下眼睛,隔一段时间检测用户是否离线等等。

spring框架提供了对定时器的支持,通过配置文件就可以很好的实现定时器,只需要应用启动,就自动启动定时器。下面介绍一下具体做法。

第一种,使用XML配置的方法

前期工作,配置spring的开发环境(这里用到了spring的web应用包,需要导入)

首先创建定时器的任务类,定时器要做什么工作,就在这里写什么方法。

package org.time;

import java.util.TimerTask;

public class MainTask extends TimerTask{

@Override

public void run() {

System.out.println("检测用户是否掉线");

}

}

接着在配置文件中对定时器进行配置。

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

最后还需要在web.xml中对配置信息进行注册:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

/WEB-INF/applicationContext.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

/WEB-INF/applicationContext.xml

这样就完成了定时器的配置,这时启动tomcat,观察控制台输出的结果:

第二种,使用注解的形式

(spring中一使用注解,感觉就是比其他方法方便了很多,代码减少了很多)

这里需要用到AOP,需要引入AOP类库

先看定时器的任务类:

package org.time;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

public class MainTask01{

@Scheduled(cron = "0/3 * * * * ?")

public void run() {

System.out.println("推送消息来了");

}

}

@Scheduled(cron = "0/3 * * * * ?")  表示三秒推送一次

corn可以配置各种时段任务UVegdg:

字段

                   值

特殊表示字符

   一般为空,1970-2099

   , - * /

    1-12 或者 JAN-DEC

   , - * /

星期

    1-7 或者 SUN-SAT

   , - * ? / L C #

    1-31

    , - * ? / L W C

    0-23 

    , - * /

    0-59 

    , - * /

    0-59 

    , - * /

如:  配置每个工作日的10:20触发 :"0 20 10 ? * MON-FRI"

配置文件:

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:task="http://springframework.org/schema/task"

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task.xsd">

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:task="http://springframework.org/schema/task"

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task.xsd">

配置文件的头部信息中比上一个引入了

xmlns:task="http://springframework.org/schema/task"

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task.xsd

这两句配置信息是必须要写的,这是spring识别@Scheduled注解的关键

这这样简单的几句配置之后,开启服务,运行结果:

spring中使用注解的方法完成定时器,不需要集成其他父类定时器,使用简单方便!代码量少,功能也很强大!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis choose when用法实例代码
下一篇:java版mock工具(mockup java)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~