在Spring Boot中,我们可以使用@Scheduled
注解和TaskScheduler
接口来实现任务调度。下面是两种实现方法的详细说明和示例代码。
方法一:使用@Scheduled注解
@EnableScheduling
注解,以启用定时任务支持。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Scheduled
注解。你可以通过fixedRate
、fixedDelay
或cron
表达式来配置任务的执行频率。import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduledTasks {
@Scheduled(fixedRate = 5000) // 每隔5秒执行一次
public void task1() {
System.out.println("Task 1 is running");
}
@Scheduled(cron = "0 * * * * *") // 每小时的0分执行一次
public void task2() {
System.out.println("Task 2 is running");
}
}
方法二:使用TaskScheduler接口
@EnableScheduling
注解,以启用定时任务支持。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
TaskScheduler
接口。import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Component;
import java.util.concurrent.ScheduledFuture;
@Component
public class MyScheduledTasks implements TaskScheduler {
private final TaskScheduler taskScheduler;
private ScheduledFuture<?> scheduledFuture;
public MyScheduledTasks(TaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay) {
scheduledFuture = taskScheduler.schedule(command, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
return new Date(System.currentTimeMillis() + delay);
}
});
return scheduledFuture;
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period) {
scheduledFuture = taskScheduler.scheduleAtFixedRate(command, initialDelay, period);
return scheduledFuture;
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay) {
scheduledFuture = taskScheduler.scheduleWithFixedDelay(command, initialDelay, delay);
return scheduledFuture;
}
public void cancel() {
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
}
}
MyScheduledTasks
类中的相应方法来执行任务。import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private MyScheduledTasks myScheduledTasks;
@GetMapping("/startTask")
public String startTask() {
myScheduledTasks.scheduleAtFixedRate(() -> {
System.out.println("Task is running");
}, 0, 5000); // 每隔5秒执行一次
return "Task started";
}
@GetMapping("/stopTask")
public String stopTask() {
myScheduledTasks.cancel();
return "Task stopped";
}
}
这样,你就可以在Spring Boot应用中使用任务调度功能了。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。