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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

阿木侠   2021-03-24 我要评论
想了解关于spring中定时器的使用教程的相关内容吗,阿木侠在本文为您仔细讲解spring 定时器使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,定时器,spring使用定时器,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("检测用户是否掉线"); 
 } 
 
} 

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

<?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:p="http://www.springframework.org/schema/p" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
  
 <bean id="mainTask" class="org.time.MainTask"></bean> 
 
 <!-- 注册定时器信息 --> 
 <bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
  <!-- 延迟1秒执行首次任务 --> 
  <property name="delay" value="1000"></property> 
  <!-- 每隔2秒执行一次任务 --> 
  <property name="period" value="2000"></property> 
  <!-- 具体执行的任务 --> 
  <property name="timerTask" ref="mainTask"></property> 
 </bean> 
 <!-- 配置任务调度器工厂 --> 
 <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
  <property name="scheduledTimerTasks"> 
   <list> 
    <ref bean="springTask"/> 
   </list> 
  </property> 
 </bean> 
</beans> 

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

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://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"> 
 <display-name></display-name> 
 <welcome-file-list> 
 <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
 <listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener> 
 <context-param> 
 <param-name>contextConfigLocation</param-name> 
 <param-value>/WEB-INF/applicationContext.xml</param-value> 
 </context-param> 
</web-app>

这样就完成了定时器的配置,这时启动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可以配置各种时段任务:

字段

                   值

特殊表示字符

   一般为空,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" 

配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:task="http://www.springframework.org/schema/task" 
 xmlns="http://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"> 
 
 <context:annotation-config /> 
 <!-- spring扫描注解的配置 --> 
 <context:component-scan base-package="org.time" /> 
  
 <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> 
 <task:scheduler id="qbScheduler" pool-size="10"/> 
 
</beans>

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

xmlns:task="http://www.springframework.org/schema/task" 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd 

<task:annotation-driven scheduler="qbScheduler" mode="proxy"/> 

<task:scheduler id="qbScheduler" pool-size="10"/>

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

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

 

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

总结

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们