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

A preliminary understanding of quartz timing tasks

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Today, let's talk about the quartz timing task that I taught myself two days ago. My understanding of the quartz timing task is that I can set a time, and then, when this time comes, to execute the business logic. This is my simple understanding. Next, let's look at how the code is implemented. First, there are two ways to implement it, one is through code, the other is through configuration files. I suggest using configuration file configuration to achieve scheduled tasks. After all, if there are any changes in the future, you only need to change the configuration file. There is no need to change the code, recompile, or redeploy.

Let's start with the first timed task implemented in pure code.

The first step is to download the necessary jar package. The jar package I use is:

The second step is to write a class to implement the Job interface, in which there is only one method execute, in which you can write your own business logic, which will be executed when the time is up.

The third step, write a timing task management class QuartzManager.java, in this class there are two ways to add scheduled tasks, because there are two kinds of triggers, the first kind of simpleTrigger simple trigger, the second is cronTrigger, you can define a very complex time, so there are two ways to add scheduled tasks.

The first method uses simpleTrigger simple triggers

The comments of the above code are very clear, focusing on jobDataMap, this map is used to pass parameters, the value set here can be obtained in the task, so that it is needed for a rainy day.

The second method is to use cronTrigger triggers

The difference between this method and the first method is that the trigger is different, which leads to a different way to set the time, but in order to deal with the more complex scenarios in reality, the second trigger is recommended.

Step 4, write a main method for testing

This paper focuses on the time setting rules of the second kind of flip-flop. * means to perform a task every 5 seconds, followed by a list of commonly used times

Next, let's talk about scheduled tasks that are implemented using configuration files.

The first step is to download the necessary jar package. The jar package I use is:

The second step is to write a class to implement the Job interface, in which there is only one method execute, in which you can write your own business logic, which will be executed when the time is up.

The third step is to need the configuration file of log4j, the configuration file of quartz and quartz_jobs.xml

Next is quartz_jobs.xml. There are some requirements for the location of this file. Look at the directory structure.

The specific content of this file is to configure job and trigger.

In this configuration file, I define two tasks, one using simpleTrigger and the other using cronTrigger

The fourth step is to write a test class to test the scheduled task, in which you only need to get the schedule from the scheduling factory, and then turn on the scheduling.

The above two ways of implementing timed tasks are realized, but I strongly recommend using configuration to implement timed tasks, and the benefits will not be mentioned any more.

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

Network Security

Wechat

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

12
Report