您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。