How to debug spring boot scheduling tasks
When I was doing enterprise development recently, there was a requirement for me to adjust and optimize the scheduled tasks for sending emails.
@Scheduled tasks
The scheduled task of sending emails that has been completed looks like this:
1 |
|
The difficulty of optimization and adjustment lies in the trouble of debugging:
- The code of the current development environment does not set a unified interface to debug back-end timing tasks through the front-end
- If you debug by modifying cron, you need to restart the project, and more importantly, you will send a lot of harassing emails
- Another idea is to call the method in the test code, but it is also very tricky
In fact, there is a relatively simple idea, rewriting the afterPropertiesSet
method of the InitialzingBean
class.
When spring initializes a bean, if the bean implements the InitializingBean
interface, it will automatically call the afterPropertiesSet
method, so it is conceivable that in this way, you can debug by restarting idea:
1 |
|
Is not it simple? Of course, this may not be the optimal solution, but if it is only used briefly in the project, it is not a big problem.