如何正确的使用弹簧定时器

  介绍

如何正确的使用弹簧定时器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

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

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

包org.time;
  
  进口java.util.TimerTask;
  
  公开课MainTask延伸TimerTask {
  
  @Override
  公共空间run () {
  System.out.println(“检测用户是否掉线“);
  }
  
  }

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

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;   & lt;豆类   xmlns=癶ttp://www.springframework.org/schema/beans"   xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns: p=癶ttp://www.springframework.org/schema/p"   xsi: schemaLocation=? http://www.springframework.org/schema/beans   ,http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"的在      & lt; bean id=癿ainTask"类=皁rg.time.MainTask"祝辞& lt;/bean>      & lt; !——注册定时器信息——比;   & lt; bean id=皊pringTask"类=皁rg.springframework.scheduling.timer.ScheduledTimerTask"比;   & lt; !——延迟1秒执行首次任务——比;   & lt;属性名=癲elay"值=https://www.yisu.com/zixun/" 1000 ">      <属性名="时间" value=" 2000 ">      <属性名=" timerTask“ref=" mainTask ">            <属性名=" scheduledTimerTasks ">   <列表>      列表         

最后还需要在网络。xml中对配置信息进行注册:

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;=& lt; web版本“2.5”;   xmlns=癶ttp://java.sun.com/xml/ns/javaee"   xmlns: xsi=癶ttp://www.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"的在   & lt; display-name> & lt;/display-name>   & lt; welcome-file-list>   & lt; welcome-file> index.jsp   & lt;/welcome-file-list>   & lt; listener>   & lt; listener-class> org.springframework.web.context.ContextLoaderListener   & lt;/listener>   & lt; context-param>   & lt; param-name> contextConfigLocation   & lt; param-value>/web - inf/applicationContext.xml   & lt;/context-param>   & lt;/web-app>

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

如何正确的使用弹簧定时器”> </p> <p> <br/> </p> <p>(春天中一使用注解,感觉就是比其他方法方便了很多,代码减少了很多)</p> <p>这里需要用到AOP,需要引入AOP类库</p> <p>先看定时器的任务类:</p> <pre class=包org.time;      进口org.springframework.scheduling.annotation.Scheduled;   进口org.springframework.stereotype.Component;      @ component   公开课MainTask01 {   @Scheduled (cron=?/3 * * * *和# 63;“)   公共空间run () {   System.out.println(“推送消息来了“);   }      }

@Scheduled (cron=?/3 * * * *和# 63;“),表示三秒推送一次

<强>玉米可以配置各种时段任务:

字段

,,,,,,,,,,值

特殊表示字符

,,一般为空,1970 - 2099

,,,- */

,,1 - 12或者JAN-DEC

,,,- */

星期

,,1 - 7或者SUN-SAT

,,,- *和# 63;c#/L

,,

日至31日,,,- *和# 63;W/L C

,,0-23 

,,- */

,,0-59 

,,- */

,,0-59 

,,- */

如:,配置每个工作日的20触发:“0 10和20 # 63;* MON-FRI",

配置文件:

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;   & lt;豆类xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns: tx=癶ttp://www.springframework.org/schema/tx"   xmlns: aop=癶ttp://www.springframework.org/schema/aop"   xmlns:上下文=癶ttp://www.springframework.org/schema/context"   xmlns:任务=癶ttp://www.springframework.org/schema/task"   xmlns=癶ttp://www.springframework.org/schema/beans"   xsi: schemaLocation=? http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop.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"的在      & lt;上下文:annotation-config/比;   & lt; !——春天扫描注解的配置——比;   & lt;上下文:component-scan基础包=皁rg.time"/比;      null   null   null   null   null   null

如何正确的使用弹簧定时器