`

Spring+Schedule(定时任务)小示例

阅读更多
本篇文章简单介绍一个Spring的schedule小例子。此定时任务放在一个mvc工程里面,配置以及代码都可以参考http://fengyilin.iteye.com/admin/blogs/2338830这篇文章,下面主要把关于schedule的内容记录下来。

变更点:
1.在servlet-context.xml文件中引入schedule的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven>

	</annotation-driven>

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources/ directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
<!-- 	<beans:bean -->
<!-- 		class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->
<!-- 		<beans:property name="prefix" value="/WEB-INF/views/" /> -->
<!-- 		<beans:property name="suffix" value=".jsp" /> -->
<!-- 	</beans:bean> -->
	<beans:bean id="templateResolver"
		class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".html" />
		<!-- Template cache is true by default. Set to false if you want -->
		<!-- templates to be automatically updated when modified. -->
		<beans:property name="cacheable" value="false" />
	</beans:bean>
	<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
	<!-- enables Spring's own MessageSource message resolution mechanisms. -->
	<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
		<beans:property name="templateResolver" ref="templateResolver" />
	</beans:bean>
	
	<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <beans:property name="templateEngine" ref="templateEngine" />
    </beans:bean>
	
	<!-- Imports user-defined @Controller beans that process client requests -->
	<beans:import resource="controllers.xml" />
	
    <!-- Imports user-defined @Scheduled beans that process schedule tasks -->
    <beans:import resource="schedules.xml" />

</beans:beans>


                                             
在此文件的最后一行引入了schedules.xml
2.追加schedules.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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--扫描schedule类package-->
    <context:component-scan base-package="test.spring.schedule"/>
     <!--使用spring scheduled的核心配置,启动scheduled注解功能-->
    <task:annotation-driven />
</beans>                                         

                                             
3.追加具体执行任务的类
  /**
* 
*/
package test.spring.schedule;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author dwxx-116
 *
 */
@Component
public class MySchedule {
    private static final Log logger = LogFactory.getLog(MySchedule.class);
    @Scheduled(fixedRate=2000)
    public void processQueues() {
        logger.info("start to processQueues,time:" + System.currentTimeMillis());
    }
}



本示例只是简单的做了测试,定时任务每2秒钟重复执行一次,当然spring Scheduled通过cron表达式支持更加灵活的调度配置。

4.追加依赖jar包aopalliance-1.0.jar

最终的工程目录



完整的工程代码请参考附件
  • 大小: 132.7 KB
分享到:
评论

相关推荐

    (动态多)定时任务源码自动运行调度后台执行

    1、spring 定时任务demo 2、定时任务(xxl-job)(XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。) 3、...

    spring 定时任务 示例代码

    本示例将深入探讨如何使用Spring的TaskScheduler或者Quartz Scheduler来创建和管理定时任务。 首先,我们要了解Spring的`@Scheduled`注解。这是一个用于方法级别的注解,可以让你轻松地定义一个定时任务。例如: `...

    spring动态配置定时任务

    在Java Spring框架中,动态配置定时任务是一项非常实用的功能,它允许我们根据需求灵活地更改或添加定时任务,而无需每次改动都重启应用。本文将深入探讨如何在Spring中实现这种动态配置,以及如何结合数据库来管理...

    Spring3.0定时任务简单实例web工程

    在本实例中,我们关注的是Spring 3.0版本中的定时任务功能,这是一个基于Web工程的简单示例,适合在MyEclipse环境中运行。 首先,我们要了解Spring 3.0中的定时任务是如何工作的。Spring提供了`TaskExecutor`接口和...

    Spring定时任务(Web项目)

    本案例将详细介绍如何利用Spring技术来实现定时任务,并提供相关的代码示例。 一、Spring定时任务简介 Spring框架的定时任务功能主要依赖于`Spring Task`模块,也称为Spring的后台任务处理。它提供了基于`@...

    java写定时任务,定时任务、定时器详细示例

    Java中的定时任务与定时器是实现自动化操作的关键技术之一,特别是在...然而,在对实时性和精度有更高要求的应用场景下,可能需要考虑使用更高级的定时框架或服务,如Quartz Scheduler或Spring Boot的定时任务支持。

    spring_schedule_task_demo.rar

    本示例"spring_schedule_task_demo.rar"提供了一个关于如何使用Spring进行定时任务设置的演示,包括基于配置和基于注解两种方式。 首先,我们来讨论基于配置的方式。在Spring中,我们可以使用`*&gt;` XML命名空间来...

    Spring普通定时任务和动态设置定时任务

    ### Spring 普通定时任务与动态设置定时任务详解 #### 一、Spring 定时任务简介 在软件开发过程中,经常会遇到需要周期性执行的任务,例如数据备份、定时发送邮件等。对于这类需求,Spring 提供了一种简单且灵活的...

    spring定时任务

    在Spring框架中,定时任务是实现自动化操作的重要功能,它允许开发者在特定时间点或按照预设周期执行任务。Spring提供了两种主要的方式来配置定时任务:基于XML和基于Java-config的配置方式。 首先,我们来详细了解...

    Springboot2-Quartz 后台可动态配置的定时任务

    本项目“Springboot2-Quartz 后台可动态配置的定时任务”是基于SpringBoot 2.x版本与Quartz Scheduler整合的一个示例,它展示了如何在后台管理系统中动态地创建、更新和删除定时任务,以及监控这些任务的状态,为...

    Spring定时调度简单实现源码

    这个"Spring定时调度简单实现源码"很显然是一个关于如何在Spring中配置和使用定时任务的示例代码包。下面我们将深入探讨Spring的定时调度机制以及其核心组件。 Spring的定时任务调度主要依赖于两个关键组件:`Task...

    spring几种定时执行任务方法 TimeTask Quartz

    在Java和Spring框架中,实现定时任务是常见且重要的需求之一。这不仅涵盖了日常生活中诸如自动烧水提醒、上班闹钟等场景,更在企业级应用中扮演着关键角色,如定期数据同步、批量处理任务、系统健康检查等。本文将...

    spring boot schedule swagger

    "spring boot schedule swagger"的主题涵盖了两个关键领域:Spring Boot的定时任务调度(Schedule)和Swagger API文档工具。本文将深入探讨这两个核心概念,并结合SQL语句和代码示例,为你提供全面的理解。 1. **...

    SpringBoot 整合Quartz(集群)实现定时任务调度

    SpringBoot整合Quartz实现定时任务调度是企业级应用中常见的需求,主要用于自动化执行某些周期性的任务,例如数据备份、报表生成、系统维护等。Quartz是一个功能强大的开源作业调度框架,能够灵活地定义任务和调度...

    Tomcat的定时任务(计时器)

    本文将深入探讨如何在Tomcat中实现定时任务,主要涉及的知识点包括Java的定时器(Timer)和Spring框架的TaskScheduler。 首先,让我们了解一下Java中的定时任务。在Java标准库中,有一个名为`java.util.Timer`的类...

    Spring监听器及定时任务实现方法详解

    通过示例代码,我们将展示如何使用Spring监听器和定时任务来实现批处理任务的执行。 Spring监听器 在Spring框架中,监听器是指实现ServletContextListener接口的类,用于监听ServletContext的生命周期事件。监听器...

    小码农的代码(四)----------JAVA中Timer定时器与Spring定时任务

    在Java编程语言中,`Timer`类和Spring框架的定时任务是两种常见的实现定时执行任务的方式。本篇文章将深入探讨这两个主题,旨在帮助初学者理解它们的工作原理以及如何在实际项目中应用。 首先,让我们来看一下Java...

    java-springboot-quartz-定时任务.zip

    在SpringBoot中定义定时任务,我们创建一个实现了`org.springframework.scheduling.Trigger`和`org.springframework.scheduling.TaskExecutor`接口的类,或者直接使用Quartz的`CronTrigger`或`SimpleTrigger`。...

Global site tag (gtag.js) - Google Analytics