Spring can dynamically modify the time parameters and manually start the shutdown operation

Spring implements timed tasks in three ways: the timer class of Java, spring task and quartz.

This article only introduces spring's own task and third-party quartz. Spirng task can be compared to a lightweight quartz. It is very simple to use. In addition to spring related packages, it does not need additional packages, and supports annotation and configuration files. Then directly demonstrate the implementation of spring task annotation.

First, create a task class as follows:

Next, add the namespace and description to the spring configuration file:

Finally, send the spring configuration file:

It's done. You can use it, but it's troublesome and rigid. It's used to write a demo. Then make a little transformation.

Implement the configuretasks method of the schedulengconfigurer interface. If you directly start the project, you can execute this method five times per second. Once the cron parameter is modified, the scheduledtaskeregister will re create a trigger to implement a new cron expression task, which solves the problem of dynamic scheduling of scheduled tasks, The content about the task class in the previous spring configuration file can also be deleted. In addition, you can modify the task pool of spring task to increase the number of tasks that can exist at the same time, as follows:

In web projects, we sometimes need to manually shut down scheduled tasks or immediately execute the functions of scheduled tasks. At this time, the spring task is not strong enough. This is also the reason why it is lightweight. It is simple to use, but the functions provided are not powerful enough. At this time, it is quartz's turn. Quartz is another open source project of OpenSymphony open source organization in the field of job scheduling. It can be combined with J2EE and J2SE applications or used alone. Quartz can be used to create simple or complex programs that run ten, a hundred, or even tens of thousands of jobs. Jobs can be made into standard Java components or EJBs.

If you are interested, you can refer to the timed task development of jeecg framework, which integrates it and is very convenient to use.

Then we integrate quartz ourselves to realize dynamic timing tasks. Clear your mind first, 1 Import jar package; 2. Configure spring files; 3. Write task code.

First, import the required jar package. I use spring 4.1 Version 9 and quartz 2.2 7. By the way, spring 3.1 Quartz 2 is supported only when x is above X's jar package, the reason will not be explained in detail. You can baidu by yourself.

summary

The above is what Xiaobian introduced to you. Spring can dynamically modify the time parameters and manually start the shutdown operation. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>