Spring 文档中提供了几种定时器的使用,现将使用到的列举如下:
1.The Task Namespace
Beginning with Spring 3.0, there is an XML namespace for configuring TaskExecutor
and TaskScheduler
instances. It also provides a convenient way to configure tasks to be scheduled with a trigger.
The following element will create a ThreadPoolTaskScheduler
instance with the specified thread pool size.
<task:scheduler id="myScheduler" pool-size="10"/>
The value provided for the id attribute will be used as the prefix for thread names within the pool. The scheduler element is relatively straightforward. If you do not provide apool-size attribute, the default thread pool will only have a single thread. There are no other configuration options for the scheduler.
<task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="beanA" method="methodA" fixed-delay="5000"/> </task:scheduled-tasks>
As you can see, the scheduler is referenced by the outer element, and each individual task includes the configuration of its trigger metadata. In the preceding example, that metadata defines a periodic trigger with a fixed delay indicating the number of milliseconds to wait after each task execution has completed. Another option is fixed-rate, indicating how often the method should be executed regardless of how long any previous execution takes. Additionally, for both fixed-delay and fixed-rate tasks an initial-delay parameter may be specified indicating the number of milliseconds to wait before the first execution of the method. For more control, a "cron" attribute may be provided instead. Here is an example demonstrating these other options.
<task:scheduled-tasks scheduler="myScheduler"> <task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000"/> <task:scheduled ref="beanB" method="methodB" fixed-rate="5000"/> <task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI"/> </task:scheduled-tasks> <task:scheduler id="myScheduler" pool-size="10"/>
2.Using the Quartz Scheduler
Wiring up jobs using triggers and the SchedulerFactoryBean
We’ve created job details and jobs. We’ve also reviewed the convenience bean that allows you to invoke a method on a specific object. Of course, we still need to schedule the jobs themselves. This is done using triggers and a SchedulerFactoryBean
. Several triggers are available within Quartz and Spring offers two QuartzFactoryBean
implementations with convenient defaults: CronTriggerFactoryBean
and SimpleTriggerFactoryBean
.
Triggers need to be scheduled. Spring offers a SchedulerFactoryBean
that exposes triggers to be set as properties. SchedulerFactoryBean
schedules the actual jobs with those triggers.
Find below a couple of examples:
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="jobDetail" /> <!-- 10 seconds --> <property name="startDelay" value="10000" /> <!-- repeat every 50 seconds --> <property name="repeatInterval" value="50000" /> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="exampleJob" /> <!-- run every morning at 6 AM --> <property name="cronExpression" value="0 0 6 * * ?" /> </bean>
Now we’ve set up two triggers, one running every 50 seconds with a starting delay of 10 seconds and one every morning at 6 AM. To finalize everything, we need to set up the SchedulerFactoryBean
:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> <ref bean="simpleTrigger" /> </list> </property> </bean>
相关推荐
在Java开发领域,Spring Boot和Spring Batch的整合是构建高效批处理系统的一种常见方式。Spring Boot以其简洁的配置和快速的启动能力深受开发者喜爱,而Spring Batch作为Spring框架的一部分,专注于批量处理任务,...
Spring框架是Java应用程序开发中的一个核心组件,它提供了一个丰富的IOC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)功能,使得开发者能够更方便地管理对象和实现模块化...
Spring Integration + Spring WS 整合 在 Java 领域中,Spring Integration 和 Spring WS 是两个常用的框架,它们分别负责集成系统和 Web 服务。今天,我们将探讨如何将这两个框架整合在一起,实现一个完整的 Web ...
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
在构建分布式系统时,Spring Cloud Gateway 作为微服务架构中的边缘服务或 API 网关,扮演着至关重要的角色。它负责路由请求到相应的微服务,并可以提供过滤器功能,如限流、熔断等。而Spring Security 则是 Java ...
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
"Spring 实战第六版" Spring Framework 是一个广泛使用的 Java 应用程序框架,它提供了一个通用的编程模型和配置机制,帮助开发者快速构建企业级应用程序。下面是对 Spring Framework 的详细知识点总结: 1. 什么...
标题中的“Spring 5, Spring Boot 2.0, Spring Cloud”揭示了三个核心的Java开发框架和技术。这些是Spring框架的最新版本,Spring Boot的第二个主要版本,以及用于构建微服务架构的Spring Cloud。 首先,Spring 5是...
Spring 框架是 Java 开发中的一个核心组件,它为构建企业级应用程序提供了全面的编程和配置模型。Spring 4.3.14 是该框架的最后一个4.x系列正式版,发布于2018年2月24日。这个版本在Spring 5.0发布之前提供了一个...
Spring Cloud和Spring Boot是两个非常重要的Java开发框架,它们在微服务架构中扮演着核心角色。Spring Boot简化了创建独立的、生产级别的基于Spring的应用程序的过程,而Spring Cloud则为开发者提供了快速构建分布式...
spring4.3.1全套jar下载。 4.3.1/spring-aop-4.3.1.RELEASE.jar 4.3.1/spring-aspects-4.3.1.RELEASE.jar 4.3.1/spring-beans-4.3.1.RELEASE.jar 4.3.1/spring-context-4.3.1.RELEASE.jar 4.3.1/spring-core-4.3.1....
spring3.0.0相关jar包 org.springframework.aop-3.0.0.RELEASE org.springframework.asm-3.0.0.RELEASE org.springframework.aspects-3.0.0.RELEASE org.springframework.beans-3.0.0.RELEASE org.springframework....
《Spring AI Core 0.8.1:开启人工智能之旅》 在现代软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java开发中的首选框架之一。而Spring AI Core则是Spring生态系统中专门为人工智能(AI)和机器学习...
在IT行业中,Spring框架是Java应用开发中的一个关键组件,它提供了一整套服务来简化企业级应用的构建。RabbitMQ则是一个流行的开源消息队列系统,它基于AMQP(Advanced Message Queuing Protocol)协议,用于高效地...
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
个人觉得spring3.2的源代码比较好编译,没啥脾气,像我之前下载的spring的最新版本源代码,还有spring4.0的源代码,不论是使用jdk1.6还是1.7甚至是1.8编译都出问题,结果还是觉得spring3.2编译过程轻松一点,配合...
Spring Boot+Spring Cloud 试题解析 Spring Boot 是一个基于 Java 的框架,用于构建独立的、生产级别的基于 Spring 的应用程序。Spring Cloud 是一个基于 Spring Boot 的微服务架构框架,提供了在分布式系统中服务...