In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to realize springboot timing task and asynchronous task". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to realize springboot timing task and asynchronous task" can help you solve the problem.
Simple case of asynchronous task
When we develop a project, we often use asynchronous processing tasks. For example, when we send an email on a website, the backend will send an email, which will cause the foreground response to remain motionless, and the response will not be successful until the email is sent. Therefore, we usually use a multi-threaded approach to deal with these tasks.
Create a new service package
Create an AsyncService class
@ Service public class AsyncService {public void hello () {try {Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("business in progress ~");}}
Create a controller package
Create an AsyncController class under the controller package
@ RestController public class AsyncController {@ Autowired AsyncService asyncService; @ GetMapping ("/ hello") public String hello () {/ / after calling the method, the display of Success asyncService.hello () on the page is delayed by 3 seconds; return "success";}}
When you access Localhost:8080/hello, after a delay of 3 seconds, the Success is output on the page and the business is in progress in the background.
New problem: if you want the page to output the information "Success" directly, and let the hello method operate directly with multithreading in the background, you need to add the @ Async annotation, so that spring boot will open its own thread pool to call.
Improvement: annotating AsyncService
@ Async// tells Spring that this is an asynchronous method public void hello () {try {Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("business in progress ~");}
But for this annotation to work, you also need to turn on the asynchronous annotation function in the entry file.
@ EnableAsync / / enable the asynchronous annotation function @ SpringBootApplication public class SpringbootTaskApplication {public static void main (String [] args) {SpringApplication.run (SpringbootTaskApplication.class, args);}}
When testing again at this time, it is found that the page outputs Success directly, but the output business is still in progress after 3 seconds in the background.
A simple case of scheduled tasks
Some scheduled tasks are often set up at work, such as analyzing the log at a certain time every day.
So Spring provides a way to schedule tasks asynchronously and provides two interfaces.
TaskExecutor interface
TaskScheduler interface
Two notes:
@ EnableScheduling
@ Scheduled
Create a ScheduleService, write a hello method in it, and let it execute regularly
@ ServicepublicclassScheduledService {/ / seconds, day, month and week @ Scheduled (cron= "0 *?") / / you need to learn the syntax of some cron expressions to understand how to set the time, which means that publicvoidhello () {System.out.println ("hello/") is executed every time the time reaches 0 seconds;}}
To use the timing function, you also need to add @ EnableScheduling to the entry file to indicate that the timing task function is enabled
@ SpringBootApplication @ EnableScheduling// enable timing task annotation function @ EnableAsync// enable asynchronous annotation function publicclassSpringbootTaskApplication {publicstaticvoidmain (String [] args) {SpringApplication.run (SpringbootTaskApplication.class,args);}}
When the test runs, it is found that hello//// will be printed in the background whenever the time is 0 seconds.
This is the end of the introduction on "how to implement springboot timed tasks and asynchronous tasks". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.