Scheduled Jobs in Spring 3
Matt Young
//
in
Technology
Spring has made scheduling a Java method as easy as creating a UNIX cron job. Here's a quick introduction.
Those of us who got our start writing web applications in the early days were used to having cron available to us to run PERL
scripts when we needed a periodic task executed. Over time, the
evolution of web applications into the application server space has made
it a little less convenient to run your business logic at an
appropriate interval.
Within the Java world, Quartz
is a commonly used technology introduced to solve the problem.
But the people behind Spring
have, as with most things, boiled the process down to something very simple.
Spring 2 had a useful task executor and task
scheduler, but Spring 3 has evolved the process into easy
annotation-based awesomeness. Java methods that you've already written
to generate reports, do maintenance, or check status can all be run with
common cron interval definitions.
Here's a quick set of steps you can use to set up this kind of scheduled task within your Spring-based Java application:
Step 1: Define a task executor and task scheduler in an application context file:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
">
<task:scheduler id="taskScheduler"/>
<task:executor id="taskExecutor" pool-size="1"/>
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
</beans>
The
"task" namespace is used here to declare the scheduler and executor.
The third line of the XML asks Spring to scan classes for annotations
related to scheduled tasks. If you have multiple tasks to run at the
same time, you can get them to run in parallel by increasing the pool
size.
Step 2: Annotate the method you want to run on a schedule
@Service
public class ReportingServiceImpl implements ReportingService {
private static final Logger logger = Logger.getLogger(ReportingServiceImpl.class);
@Autowired
private MailService ms;
@Override
// Every night at 1 AM
@Scheduled(cron = "* * 1 * * ?")
public void generateAndEmailReport() {
logger.info("Starting report at " + new Date(System.currentTimeMillis()));
Report r = generateReport();
ms.sendReport(r);
logger.info("Report sent at " + new Date(System.currentTimeMillis()));
}
}
Simple!
There
are some other useful variants of the annotations. Instead of
supplying a 'cron' pattern, you could just define an interval.
@Scheduled(fixedRate=60000)
This annotation would have a method run every 60 seconds. (FixedRate defines a time in milliseconds)
You can also use the
@Async
annotation to mark a scheduled method as asynchronous.
Two things to be aware of:
At
the time of writing, there's no way to control if or how often a method
is retried if an exception occurs during scheduled method execution.
So, if an exception happens during execution of a scheduled method,
Spring will re-try the method automatically. If the exception continues
to happen, this can result in large logs and possibly a lot of resource
consumption. I've submitted an enhancement request to Spring to make
this behavior configurable. http://jira.springframework.org/browse/SPR-6860
Spring
3.0.0 Release had a bug where web apps with a task scheduler would end
up executing scheduled methods twice. This has been resolved in Spring
3.0.1.
You can read more about scheduling tasks with Spring here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html
转自:http://www.vodori.com/blog/spring3scheduler.html
其他参考:http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/scheduling.html
分享到:
相关推荐
Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析,做了一些测试说明了各个参数的作用
spring boot @scheduled注解 cron 表达式实现计划任务调度。
spring @Scheduled定时任务代码
本代码是个demo,展示spring定时任务的三种实现方式:xml配置实现,注解实现及动态定时任务实现。
在不停服务的情况下,动态修改Spring定时任务的执行周期,即动态修改定时任务的cron参数。
使用spring @Scheduled注解执行定时任务
本文将详细探讨Spring Boot中@Scheduled注解的使用,包括其原理、实现流程、步骤和代码示例。通过本文,读者将能够了解如何在Spring Boot应用中轻松创建和管理定时任务。 # @Scheduled注解简介 在Spring框架中,@...
NULL 博文链接:https://rmn190.iteye.com/blog/273564
- [使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程](http://blog.didispace.com/spring-initializr-in-intellij/) ### 工程配置 - chapter2-1-1:[配置文件详解:自定义属性、随机数、多环境...
本工程用于研究如何在Spring框架中采用Scheduled注释的方式实现Spring定时器的功能 本工程编码方式:UTF-8 相关博客:http://blog.csdn.net/gaohuanjie/article/details/43559167
在 Salesforce 中轻松管理计划作业 该应用程序使用自定义对象、2 个视觉力页面和一些顶点类。 将组件添加到组织后,将出现一个名为“计划作业”的选项卡。 当您单击选项卡时,您将看到通过应用程序提交的预定作业的...
IDEA使用springboot自带scheduled实现任务调度 代码解析:https://www.cnblogs.com/personblog/p/14237202.html
主要介绍了spring 定时任务@Scheduled的相关资料,文中通过示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
spring boot工程的定时任务
本篇文章主要介绍了详解在Spring3中使用注解(@Scheduled)创建计划任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
laravel在heroku上计划的工作 Heroku上的Laravel Scheduled Jobs的代码示例
基于SpringBoot和Spring自带的schedul-springboot-task-scheduled
本篇文章中主要介绍了Spring Boot中使用@Scheduled创建定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
本例中一共使用了两种 spring 的定时任务,一种是使用 xml 配置的定时任务,一种是使用 annotation 配置的定时任务,其中后者的 cronExpress 表达式是配置在配置文件中的,两者的区别在于,xml是比较传统的方式,...
我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现。本案例实现了一个非常简单的任务调度。