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

Introduction of several ways of timing tasks in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "several ways to introduce Java timing tasks". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "several ways to introduce Java scheduled tasks"!

Several implementation methods of timing tasks in Java

Based on java.util.Timer timer, timing tasks similar to alarm clock are implemented using open source third-party timing task frameworks such as Quartz, elastic-job, xxl-job, etc., which are suitable for distributed project applications to use an annotation provided by Spring: @ Schedule, which is easy to develop and easy to use, and is also a way introduced in this article.

Spring itself provides support for scheduled tasks, and this article introduces the use of @ Scheduled timers in Spring Boot.

Create a scheduled task

First, add the @ EnableScheduling annotation to the project startup class to enable support for scheduled tasks

@ SpringBootApplication@EnableSchedulingpublic class ScheduledApplication {

Public static void main (String [] args) {SpringApplication.run (ScheduledApplication.class, args);}

}

The purpose of the @ EnableScheduling annotation is to discover the task of the annotation @ Scheduled and execute it in the background.

Secondly, write timing task classes and methods, which are loaded through Spring IOC, using @ Component annotations, and timing methods using @ Scheduled annotations.

@ Componentpublic class ScheduledTask {

@ Scheduled (fixedRate = 3000) public void scheduledTask () {System.out.println ("Task execution time:" + LocalDateTime.now ());}

}

FixedRate is a long type that indicates the number of milliseconds between tasks. The scheduled tasks in the above code are executed every 3 seconds.

Run the timed project, the project start and run logs are as follows, it can be seen that the log execution record is printed every 3 seconds.

2019-10-16 22 com.wupx.ScheduledApplication 50 com.wupx.ScheduledApplication 04.791 INFO 10610-- [main] com.wupx.ScheduledApplication: Started ScheduledApplication in 1.513 seconds (JVM running for 1.976) Task execution time: 2019-10-16T22:50:04.791 Task execution time: 2019-10-16T22:50:07.782 Task execution time: 2019-10-16T22:50:10.779@Scheduled details

In the above getting started example, the @ Scheduled (fixedRate = 3000) annotation is used to define the tasks to be executed every 3 seconds. The use of @ Scheduled can be summarized as follows:

@ Scheduled (fixedRate = 3000): execute 3 seconds after the last execution time point (fixedRate attribute: the delay of executing the scheduled task again after the scheduled task starts (you need to wait for the last scheduled task to complete) (in milliseconds) @ Scheduled (fixedDelay = 3000): 3 seconds after the last execution is completed (fixedDelay attribute: the delay of executing the scheduled task again after the completion of the scheduled task (to wait for the completion of the last scheduled task), in milliseconds) @ Scheduled (initialDelay = 1000, fixedRate = 3000): execute after the first delay of 1 second Then execute the fixedRate rule every 3 seconds (initialDelay attribute: the delay time for the first execution of a scheduled task, which needs to be used with fixedDelay or fixedRate) @ Scheduled (cron= "0 021 *? *"): define the rule through the cron expression

Among them, the common cron expressions are:

002 1 *? *: execute 01510? * MON-FRI at 2: 00 a.m. on the 1st of each month: 01510? 6L 2019-2020 at 10:15 every day from Monday to Friday: 10:15 on the last Friday of every month from 2019 to 202020: 10:00, 2 p.m. 012? * WED: every Wednesday at 12:00 * *?: 01510? * every day at 12:00: 01510 * *?: 01510 * *? every day at 10:15 *? *: execute 0 15 10 * * every morning at 10:15 2019: execute at 10:15 every morning in 2019

Summary

This article mainly introduces the configuration and use of timed tasks based on Spring Boot, which mainly involves two annotations and four attributes:

Main program entry @ EnableScheduling start timing task timing method @ Scheduled set timing cron property: execute fixedRate property according to cron rule: execute fixedDelay property at a fixed rate: delay execution again after the last execution: delay execution for the first time, delay execution again after the first execution

At this point, I believe you have a deeper understanding of the "introduction of several ways of Java scheduled tasks". You might as well come to practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

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

12
Report