springMVC中实现定时器可在Controller中配置定时器

本章讲解springMVC中实现定时器,此时基于上一章eclipse搭建springmvc项目框架

文件结构

springMVC中实现定时器可在Controller中配置定时器_第1张图片

定时器所需jar

aopalliance-1.0.jar

步骤

1、springMVC-servlet.xml

[html] view plain copy print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"  
  6.     xsi:schemaLocation="    
  7.     http://www.springframework.org/schema/beans     
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  9.     http://www.springframework.org/schema/context    
  10.     http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  11.     http://www.springframework.org/schema/mvc    
  12.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  13.     http://www.springframework.org/schema/task     
  14.     http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  15.       
  16.     <context:component-scan base-package="*" />  
  17.   
  18.       
  19.     <mvc:annotation-driven />  
  20.   
  21.       
  22.     <bean id="viewResolver"  
  23.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  24.         <property name="prefix" value="/WEB-INF/views/" />  
  25.         <property name="suffix" value=".jsp" />  
  26.     bean>  
  27.   
  28.     <task:executor id="executor" pool-size="5" />  
  29.     <task:scheduler id="schedule" pool-size="10" />  
  30.     <task:annotation-driven executor="executor"  
  31.     scheduler="schedule" />  
  32.   
  33. beans>  

注:添加了task

2、VendorTask.java

[java] view plain copy print ?
  1. package net.csdn.blog.springmvc.timer;  
  2.   
  3. import org.springframework.scheduling.annotation.Scheduled;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. public class VendorTask {  
  8.   
  9.     // private static Logger log = Logger.getLogger(VendorTask.class);  
  10.   
  11.     @Scheduled(fixedDelay = 5000)  
  12.     public void doSomethingWithDelay() {  
  13.         System.out.println("I'm doing with delay now!");  
  14.     }  
  15.   
  16.     @Scheduled(fixedRate = 5000)  
  17.     public void doSomethingWithRate() {  
  18.         System.out.println("I'm doing with rate now!");  
  19.     }  
  20.   
  21.     @Scheduled(cron = "0/5 * * ? * *")  
  22.     public void doSomethingWithCron() {  
  23.         System.out.println("I'm doing with cron now!");  
  24.     }  
  25.   
  26. }  

注:cron效果为服务器启动之后每过5s钟执行一次,具体的写法可参考Spring定时器配置方式

3、效果

springMVC中实现定时器可在Controller中配置定时器_第2张图片



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@Component
@Controller
@RequestMapping(value="/serverinfo")
public class ServerInfoController extends BaseController implements ServletContextListener{

   @Scheduled(fixedDelay = 50000)  
    public void doSomethingWithDelay()  {

   

}

}

你可能感兴趣的:(springmvc)