Detailed explanation of Java scheduled tasks

Timed tasks are often used in projects. Based on the blogger's own experience in using timed tasks, this paper introduces timed tasks in the following points:

1. Introduction to quartz timing task and spring configuration of quartz timing task

2. Schedulerfactory performs addition, deletion, modification and query on scheduled tasks

3. Summary

Introduction to quartz scheduled tasks:

Quartz is one of the regular tasks often used in the project. It is an open source job scheduling framework written entirely in Java. It can be combined with J2EE and J2SE applications or used alone. Its main components include job, scheduler and cronexpression. It will be introduced here. The following describes how spring configures quartz.

To configure quartz, you need to understand that configuring quartz is to configure job, scheduler and cronexpression. After these three parts are configured, it is a complete timing task. The configuration is as follows:

After the configuration is completed, add a Java class to perform a task for you. Each quartz job must have a specific class that implements the org.quartz.job interface. The code is as follows:

Of course, there is no need to specially configure the interface in the job. You can use spring injection. Here is just a description of the method of configuring the interface.

Here, the simple task of configuring quartz timing has been completed. Here is an additional case where I use quartz in the project: the cronexpression expression expression is read from the database.

At that time, I solved this problem by inheriting org.springframework.scheduling.quartz.crontriggerbean and setting cronexpression. The specific code is as follows:

The configuration of trigger should be changed:

XX. Initcrontriggerfactorybean Code:

Sysparamservice is an interface to query the corresponding cronexpression expression from the database according to the key. In addition to spring injection, this interface can also be set using the set method, as follows:

In this way, the set method of sysparamservice will be added to the xx.initcrontriggerfactorybean. At this time, the code of xx.initcrontriggerfactorybean is:

Quartz timing task is almost here. The basic configuration and use are described above. If you want to have an in-depth understanding of quartz, you can find more information on the Internet and use it in practice. Next, we will explain the examples used in the project that can add, delete, modify and query scheduled tasks at any time.

The addition, deletion, modification and query of scheduled tasks can actually be regarded as the addition, deletion, modification and query of data. However, the addition, deletion, modification and query of scheduled tasks are job and trigger operations. The specific codes are as follows:

Scheduled task manager code:

Job code:

Add a scheduled task Code:

QuartzManager.addJob(JobName,TriggerName,TestJob.class,Date);

In this way, the scheduled task will be executed when the time date is reached. However, the added tasks are saved in memory, and the scheduled tasks will be lost when the project is restarted. Therefore, it is best to add a class to scan the database and reload the unexecuted scheduled tasks into memory when the project is started.

The application of scheduled tasks in the project needs to be adjusted according to the specific business. However, as long as the principle and implementation of scheduled tasks are clear, they can be flexibly used for specific business in the project. I hope this article can enable you to quickly understand and use scheduled tasks and apply them to practice.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>