spring Timer Timing Task to Time Unexecuted Problem Solving

  • 2021-12-09 08:45:43
  • OfStack

Directory spring Timer Timing Task to Time Not Executed Application Scenario Cause Analysis Solution Solution After Modifying System Time Spring Timing Task Not Executed Problem Description Cause Error Solution Problem

spring Timer Timing Task to Time Not Executed

Application scenario

There are n timer tasks in a timer class, which are executed once every 30 seconds and once every 1 minute. The timer task with problems is that the timer task executed at 0:00 is not executed at 0:00.

Cause analysis

The default configuration of spring timer task scheduled-tasks is single-threaded serial execution. When a timer task is blocked or the execution time is too long, the thread will be occupied, and other timer tasks will queue up for execution, resulting in the subsequent timer tasks not being executed on time.

Solution

Start multithreaded timed task execution


/**
 *  Multithreaded execution of timed tasks 
 */
@Configurable
public class ScheduleConfig implements SchedulingConfigurer {
    private static final int FIVE = 5;
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(FIVE));
    }
}

Solve the problem that Spring timing task is not executed after modifying system time

Problem description

Spring Timing Task Not Executed

The reason for this is that we have an spring timing task, which should be performed every half hour.

Cause

Due to various reasons, this server was shut down last night. This morning, "restart the server" and "start the timed task service".

After the machine restarted, it was found that the system time of the server machine was not 1 with the actual Beijing time, with a difference of 10 hours.

Therefore, I use date-s 10:35:35 setting and Beijing time to keep 1.

Errors

I thought so, the time has already reached 1, and the timed task should be able to execute normally!

After waiting for several hours, the scheduled task was still not performed.

So look at the system log, system log 1 is normal, but there is no log log for timed task execution.

Solve a problem

Strangely enough, the service started normally, but the timed task was not executed.

However, the service that was still running yesterday was over because of one shutdown?

Impossible, so shut down the application service and restart again.

The timing task was successfully executed, and it was concluded that the Spring timing task would fail after the server system time changed.

After the server system time changes, the Spring timing task will fail. The solution is to restart the application service.


Related articles: