import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableScheduling
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
@Component
public class MyScheduledTasks {
@Scheduled(cron = "0 0 * * * ?")
public void myScheduledTask() {
// Your task logic here
}
}
@Component
public class MyScheduledTasks {
private static final Logger LOGGER = LoggerFactory.getLogger(MyScheduledTasks.class);
@Scheduled(cron = "0 0 * * * ?")
public void myScheduledTask() {
try {
// Your task logic here
} catch (Exception e) {
LOGGER.error("Error occurred during scheduled task execution: {}", e.getMessage());
}
}
}
Spring的定时任务不生效可能有多种原因。以下是一些可能的原因和相应的解决方法:
上一篇:初步了解 RabbitMQ