How to add, cancel and reset spring built-in task scheduling
preface
Everyone should know that using spring's task scheduling brings great convenience to our development. However, after our task scheduling configuration is completed, it is difficult to change it, unless the server is stopped, the configuration is modified, and then restarted. Obviously, this is not conducive to online operation. In order to realize dynamic task scheduling modification, I have also consulted some materials on the Internet. Most of them are implemented based on quartz. There are few task scheduling built in spring, and the effect is not ideal. The new configuration can take effect only after the next task execution, which can not take effect immediately. Based on the principle of exploration and research, I have checked the source code of spring. Here is a method for adding, canceling and resetting spring's built-in task scheduling. I don't have much to say. Let's take a look at the detailed introduction.
The implementation method is as follows
First, we need to enable spring's task scheduling
This part of the configuration is very common on the Internet. Next, we need to use @ enableshcheduling and org springframework. scheduling. annotation. The schedulingconfigurator carries our own scheduling configuration. In the schedulingconfigurator interface, we need to implement a void configuretasks (scheduledtaskeregister); The traditional method is to add the scheduling information to be executed in this method. The basic tasks on the Internet are also implemented using this method. Add tasks using addtriggertask and dynamically modify the scheduling time in combination with cron expression. We don't do this here.
Check the source code of scheduledtask registrar. We find that after the object is initialized, the scheduletasks () method will be executed, and the task scheduling information will be added in this method. Finally, all the task information will be stored in the collection named scheduledfutures.
Therefore, my idea is to dynamically modify the set to realize the addition, cancellation and reset of task scheduling. The implementation code is as follows:
The source code of BeanUtils used is as follows:
Because of the loading delay, when using this method to customize the configuration task scheduling, you first need to call the initiated () method to determine whether the initialization is completed, otherwise an error may occur.
Next, let's test:
In this class, we first use a thread to wait for the initialization of our own task scheduling, add a task that prints a sentence every five seconds, and then use another thread to modify the task after 30 seconds. The essence of modification is to cancel the original task, and then add a new task.
Initialize the above class in the configuration file
Run the program and observe the console output
In this way, we realize the dynamic reset task. The above methods are explored by individuals. If there are better solutions, please correct them.
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.