springMVC中实现定时器可在Controller中配置定时器
2017-03-08 10:32
489 查看
本章讲解springMVC中实现定时器,此时基于上一章eclipse搭建springmvc项目框架
view plain
copy
print?
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="*" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<task:executor id="executor" pool-size="5" />
<task:scheduler id="schedule" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="schedule" />
</beans>
注:添加了task
view plain
copy
print?
package net.csdn.blog.springmvc.timer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class VendorTask {
// private static Logger log = Logger.getLogger(VendorTask.class);
@Scheduled(fixedDelay = 5000)
public void doSomethingWithDelay() {
System.out.println("I'm doing with delay now!");
}
@Scheduled(fixedRate = 5000)
public void doSomethingWithRate() {
System.out.println("I'm doing with rate now!");
}
@Scheduled(cron = "0/5 * * ? * *")
public void doSomethingWithCron() {
System.out.println("I'm doing with cron now!");
}
}
注:cron效果为服务器启动之后每过5s钟执行一次,具体的写法可参考Spring定时器配置方式
3、效果
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@Component
@Controller
@RequestMapping(value="/serverinfo")
public class ServerInfoController extends BaseController implements ServletContextListener{
@Scheduled(fixedDelay = 50000)
public void doSomethingWithDelay() {
}
}
文件结构
定时器所需jar
aopalliance-1.0.jar步骤
1、springMVC-servlet.xml
[html]view plain
copy
print?
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="*" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<task:executor id="executor" pool-size="5" />
<task:scheduler id="schedule" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="schedule" />
</beans>
注:添加了task
2、VendorTask.java
[java]view plain
copy
print?
package net.csdn.blog.springmvc.timer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class VendorTask {
// private static Logger log = Logger.getLogger(VendorTask.class);
@Scheduled(fixedDelay = 5000)
public void doSomethingWithDelay() {
System.out.println("I'm doing with delay now!");
}
@Scheduled(fixedRate = 5000)
public void doSomethingWithRate() {
System.out.println("I'm doing with rate now!");
}
@Scheduled(cron = "0/5 * * ? * *")
public void doSomethingWithCron() {
System.out.println("I'm doing with cron now!");
}
}
注:cron效果为服务器启动之后每过5s钟执行一次,具体的写法可参考Spring定时器配置方式
3、效果
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@Component
@Controller
@RequestMapping(value="/serverinfo")
public class ServerInfoController extends BaseController implements ServletContextListener{
@Scheduled(fixedDelay = 50000)
public void doSomethingWithDelay() {
}
}
相关文章推荐
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- SpringMVC实现Controller的两种方法:配置和约定
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- springMVC笔记(一) Controller的实现和配置(一)
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- (csdn内转载)使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法
- SpringMVC实现一个controller写多个方法
- 天易17----spring定时器配置与实现(很好用)
- 定时器的实现、java定时器介绍与Spring中定时器的配置
- quartz实现定时功能实例详解(servlet定时器配置方法)
- 实现springmvc 的controller级别的跳转的方法 return "redirect:/news/to";
- 定时器的实现、java定时器Timer和Quartz介绍与Spring中定时器的配置
- 简单实现JFinal注解配置Controller,Model
- 使用ControllerClassNameHandlerMapping实现SpringMVC的@RequestMapping后面不加url
- SpringMVC扫描controller配置
- ubuntu下使用eclipse开发springmvc项目--实现controller接口方式
- Spring mvc (四) [继承MultiActionController实现以方法为单位的controller][配置2种请求的指定方式]
- SpringMVC入门配置和简单实现