Several possible reasons why java spring boot timing tasks are not executed

First, add the @EnableScheduling annotation to the main Application to indicate that
this app has scheduled tasks. The classes that need to be scanned for scheduled tasks.

package com.other;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class OtherApplication {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(OtherApplication.class, args);
	}
}

Then add
@Component
@EnableScheduling
@EnableAsync to the main timing task class

The first two comments, the third @EnableAsync is added as appropriate.
My code is as follows

package com.other.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableAsync
@EnableScheduling
public class UploadGrade {
    
    
    @Scheduled(fixedRate = 2000)
    public void task1(){
    
    
        System.out.println("task1运行"+ System.currentTimeMillis()); 
    }
}

It seems simple, but there are many pits hidden, and you fall into it accidentally, such as:

(1) This method cannot have parameters
(2) This method cannot have return values
(3) This class cannot contain other methods with any annotations
(4) This class must be in the same package as the main Application. As shown below.
Insert picture description here

Guess you like

Origin blog.csdn.net/phker/article/details/111918743