spring任务调度 注解+配置文件

来源:互联网 发布:软件售后合同范本 编辑:程序博客网 时间:2024/05/12 02:31

本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面将分别介绍这两种方式。

1.配置文件方式

package com.htf.task;public class MyTask {public void say(){System.out.println("xml配置方式--调用定时任务成功。。。");}}
2.注解方式

package com.htf.task;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class MyTaskAnnotation { /**       * 定时计算。每天凌晨 01:00 执行一次       */        @Scheduled(cron="5/3 * * * * ?")      public void show(){          System.out.println("Annotation:is show run 注解 定时计算任务");      }          /**       * 心跳更新。启动时执行一次,之后每隔2秒执行一次       */        @Scheduled(fixedRate = 1000*10)       public void print(){          System.out.println("Annotation:print run 注解 心跳更新任务");      }  }
springmvc-servlet.xml   配置文件写在了一起。

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><!-- @Controller, @Service, @Configuration, etc. --><context:component-scan base-package="com.htf.controller" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/" /><property name="suffix" value=".jsp" /></bean><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- set the max upload size100MB --><property name="maxUploadSize"><value>104857600</value></property><property name="maxInMemorySize"><value>4096</value></property></bean><!-- 注解方式springTask --><task:annotation-driven/><context:component-scan base-package="com.htf.task" /><!-- 自动扫描 --><!-- 注解方式springTask --><!-- 配置方式springTask --><!-- <bean id="taskTest" class="com.htf.task.MyTask"></bean><task:scheduled-tasks>  <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />  </task:scheduled-tasks> --></beans>

cron 表达式配置  http://cron.qqe2.com/

spring 4.0.5测试过

 @Configuration @EnableScheduling public class AppConfig {     @Scheduled(fixedRate=1000)     public void work() {         // task execution logic     } }  //@EnabledScheduling确保后台任务执行器被创建,similar to functionality found in Spring's <task:*> XML namespace。 //@Scheduled去配置一个特定的方法。这个例子使用的是 fixedRate,表示方法开始调用的时间间隔; //@Schduled(cron="...")表达式执行更复杂的调度。 

配置文件形式
<beans><!--定时任务--><task:annotation-driven scheduler="taskScheduler"/><task:scheduler id="taskScheduler" pool-size="42"/><task:scheduled-tasks scheduler="taskScheduler"><task:scheduled ref="myTask" method="work" fixed-rate="1000"/></task:scheduled-tasks><bean id="myTask" class="com.foo.MyTask"/></beans>




0 0
原创粉丝点击