spring定时任务(1):使用component注解实现静态定时任务

环境:myeclipse10.7+spring 3.1

一、在服务器端编写任务类

package com.conbao.component.task.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务作业类
 *
 * @author code陈
 *
 */
@Component("task")
public class Task {
	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Scheduled(cron = "0 10-30 9 * * ?")
	public void pushQuestionnaire() {

		System.out.println("定时任务1,自动执行:" + format.format(new Date()));
	}

	@Scheduled(cron = "0 10-30 9 * * ?")
	public void pushQuestionnaire2() {

		System.out.println("定时任务2,自动执行:" + format.format(new Date()));
	}
}

二、在服务器端配置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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/context
http://www.springframework.org/schema/jdbc/spring-jdbc-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
	default-lazy-init="false">

	<context:annotation-config />
	<!—spring扫描注解的配置 :能扫描到task所在的包就行,因为我的程序里还有其他注解,所以base-package是较外层的package  -->
	<context:component-scan base-package="com.conbao.component" />

<!—开启这个配置 -->
	<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
	<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

三、最后的运行效果

<解释>

(1)这是一个通过spring的@component和@Scheduled实现的定时任务,由于在代码中已经写死了定时任务的执行时间(cron="* * * * * *"),所以可以叫做静态定时任务,但是在很多时候,我们本身也不确定一个任务执行的时间,比如是在前台通过一个日历,选择执行一个任务的时间,那么我们就需要动态改变cron,这就是动态定时任务,具体实现,下一节再整理。

(2)其实也可以通过spring的其他配置方式或者其他途径比如java自带的timer类或者Quartz来实现定时任务,大家可以去网上查看,本人是由于项目的框架是spring,所以使用注解比较方便。

(3)cron的设置规则,从网上摘来,可根据自己需求设计。

字段   允许值   允许的特殊字符

   0-59    , - * /

   0-59    , - * /

小时    0-23    , - * /

日期    1-31    , - * ? / L W C

月份    1-12 或者 JAN-DEC    , - * /

星期    1-7 或者 SUN-SAT    , - * ? / L C #

年(可选)    留空, 1970-2099    , - * /

- 区间

* 通配符

? 你不想设置那个字段

下面只例出几个式子

CRON表达式    含义

"0 0 12 * * ?"    每天中午十二点触发

"0 15 10 ? * *"    每天早上10:15触发

"0 15 10 * * ?"    每天早上10:15触发

"0 15 10 * * ? *"    每天早上10:15触发

"0 15 10 * * ? 2005"    2005年的每天早上10:15触发

"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发

"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发

"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发

"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发

"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发

"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发

时间: 2024-08-02 07:03:12

spring定时任务(1):使用component注解实现静态定时任务的相关文章

在Spring项目中使用@Scheduled注解定义简单定时任务

如题所示,有时候我们需要在Web项目中配置简单的定时任务,而且因为任务并不复杂不想使用定时调度框架(PS:Quartz.ActiveMQ .Kafka等),这时就可以考虑使用@Scheduled注解来定义简单的定时任务.其全部配置如下: (1)在Spring的配置文件中添加定时任务相关配置: xml配置的头文件中添加: xmlns:task="http://www.springframework.org/schema/task" 以及在xsi:schemaLocation中添加: ht

spring注解scheduled实现定时任务

只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springframework.org/schema/task"以及在xsi:schemaLocation中加入 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

spring boot注解之@Scheduled定时任务实现

java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @Component // 启用定时任务 @EnableScheduling public class TagPushScheduler { private static Logger log = Logger.getLogger(TagPushScheduler.class); @Scheduled

Spring中@Component注解,@Controller注解详解(网摘)

在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式, 只需要添加几行自动注入的的配置,便可以完成Service层,Controller层等等的注入配置. 使用过程中,在Service层中的实现类头上加@Compopnet注解,在Controller类头加@Controller注解,便完成了配置. 例如 在Controller中当我们调用某个Service时就不需要Set方法了,直接通过@Autowried 注解对Service

Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqiang0316/article/details/52601292 @SpringBootApplication public class FavoritesApplication extends SpringBootServletInitializer{ /** * 如此配置打包后可以用tomcat下使

Spring @Component 注解的使用

使用说明 这个注解用于声明当前的类是一个组件类,Spring 会通过类路径扫描来自动侦测和自动装配这些组件,创建一个个 bean 后,注册到 Spring 容器中. 带 @Component 注解的类和自动创建的 bean 之间存在隐式的一对一映射关系.由于只需要声明一个注解,其他过程都是自动化的,所以对 bean 的创建过程可控程度较低. 该注解相当于: <bean id="useService" class="com.test.service.UserService

【spring springmvc】springmvc使用注解声明控制器与请求映射

目录 概述 壹:注解说明 贰:实现注解声明控制器与请求映射 一:使用controller 二:配置包扫描与视图解析器 1.配置包扫描 2.配置试图解析器 三:配置部署描述符 1.读取spring-mvc.xml文件 2.配置匹配映射 四:建立html文件 叁:配置tomcat 一:配置本地tomcat 二:配置maven内置tomcat 肆:结果及问题 一:tomcat启动示意图: 二:结果 三:问题 伍:结构及源码 一:目录结构 二:源码 作者有话 概述 注解: 在Spring中尽管使用XML

SSH(Struts2+Spring+Hibernate)框架搭建流程&lt;注解的方式创建Bean&gt;

此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblogs.com/wkrbky/p/5912810.html 一.Hibernate(数据层)的搭建: 实现流程 二.Spring(注入实例)的使用: 实现流程 三.Struts2(MVC)的搭建: 实现流程 这里注意一点问题: Struts2与Hibernate在一起搭建,antlr包,有冲突.MyE

spring Quartz基于配置文件和注解的实现

这里仅仅是做简单的记录怎样实现. 一.基于配置文件的实现 ①编写须要调度的类 package com.study; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; //@Component public class QuartzJob { public QuartzJob(){ System.out.println("Quart