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
- spring任务调度 注解+配置文件
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- Spring quartz 任务调度(注解方式)
- spring 使用注解来调度定时任务
- spring 使用注解来调度定时任务
- quartz任务调度配置文件
- Spring 的@Scheduled注解实现定时任务执行和调度
- Spring 注解式定时调度任务与cron表达式
- 使用spring quartz实现任务调度(注解方式)
- 使用Spring 注解实现调度任务及cron表达式详解
- spring 任务调度总结
- Spring 调度任务
- Spring调度任务
- Spring 任务调度
- Spring 调度任务
- spring quartz任务调度
- Android 获取机身内存,SD卡内存,Ram内存。
- android studio常用配置
- Core Data入门
- Java基础--函数和数组
- 第四周 项目1三角形类的构造函数(3)---参数初始化表
- spring任务调度 注解+配置文件
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- iOS开发知识点总结(三)
- HTML总结
- EXT4中恢复使用rm命令误删除的文件
- Java基础-12
- iphone移动产品开发之屏幕自适应
- java多线程例子
- 面向对象设计原则之概述