发布于 2018-03-16 07:23:39 | 134 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


大家应该都有所体会,在很多实际的web应用中,都有需要定时实现的服务,下面这篇文章主要给大家介绍了关于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中使用注解的方法完成定时器,不需要集成其他父类定时器,使用简单方便!代码量少,功能也很强大!

总结

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务