Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the @ Scheduled function in Spring

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use @Scheduled function in Spring". In daily operation, I believe many people have doubts about how to use @Scheduled function in Spring. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use @Scheduled function in Spring"! Next, please follow the small series to learn together!

Spring @Scheduled Annotation

The @ scheduled comment is used for task scheduling. Trigger information needs to be provided with this comment.

You can use the attribute fixedDelay/fixedRate/cron to provide trigger information.

fixedRate causes Spring to run tasks periodically, even if the last invocation is still running

FixedDelay specifically controls the next execution time at the end of the last execution.

Cron is a feature derived from the Unix cron utility and has a variety of options depending on your needs.

Example usage:

@Scheduled Usages@Scheduled(fixedDelay =30000)public void demoServiceMethod () {... } @Scheduled(fixedRate=30000)public void demoServiceMethod () {... } @Scheduled(cron="0 0 * * * *")public void demoServiceMethod () {... }1.2 How to enable @Scheduled comments

To use @Scheduled in a spring application, you must first define the xml namespace and schema location definitions in the applicationConfig.xml file. Task: annotation driven was also added to support annotation-based task scheduling.

applicationConfig.xmlxmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/task/http://www.springframework.org/schema/task/spring-task-3.0.xsd

The above addition is necessary because we will be using an annotation-based configuration.

1.3 Use @Scheduled comment

The next step is to create a class and a method in the class, as follows:

DemoService.javapublic class DemoService{ @Scheduled(cron="*/5 * * * * ? ") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}

in the above example

Using the @Scheduled annotation in turn causes the Spring container to understand that the method below will run as a job.

Remember that methods annotated with @Scheduled should not have parameters passed to them.

They shouldn't return anything.

If you want to use external objects in the @Scheduled method, you should inject them into the DemoService class using automatic connections instead of passing them as parameters to the @Scheduled method.

2. Fixed delay and frequency using @Scheduled

In this method, the fixedDelay attribute is used with the @Scheduled annotation.

Examples:

DemoServiceBasicUsageFixedDelay.javapackage com.howtodoinjava.service; import java.util.Date;import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay{ @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) //Or use this public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}

The application configuration is as follows:

applicationContext.xml

< ?xml version="1.0" encoding="UTF-8"?>

3. Use @Scheduled with cron expression

In this method, the cron attribute is used with the @Scheduled annotation.

Examples:

DemoServiceBasicUsageCron.javapackage com.howtodoinjava.service;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;public class DemoServiceBasicUsageCron{ @Scheduled(cron="*/5 * * * * ? ") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}

The application configuration is as follows:

applicationContext.xml

< ?xml version="1.0" encoding="UTF-8"?>

4. Configure Cron with properties file

In this method, the cron attribute is used with the @Scheduled annotation. The value of this attribute must be a cron expression, as shown in the previous method, but this cron expression will be defined in the properties file and the key for the associated attribute will be used for the @Scheduled annotation.

This separates cron expressions from the source code, making changes easy.

DemoServicePropertiesExample.javapackage com.howtodoinjava.service;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;public class DemoServicePropertiesExample { @Scheduled(cron = "${cron.expression}") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}

The application configuration is as follows:

applicationContext.xml 5. Configure Cron with context

This method configures cron expression in property file, and configures job scheduling using cron expression property key in configuration file. The main change is that you don't need to use @Scheduled annotations on any method. Method configuration is also done in the application configuration file.

Examples:

DemoServiceXmlConfig.javapackage com.howtodoinjava.service;import java.util.Date;public class DemoServiceXmlConfig{ public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); }}

The application configuration is as follows:

applicationContext.xml At this point, the study of "how to use the @Scheduled function in Spring" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report