gpt4 book ai didi

java - 如何动态打开/关闭springboot应用程序中的预定方法

转载 作者:行者123 更新时间:2023-11-30 07:53:54 25 4
gpt4 key购买 nike

我正在构建一个 Springboot 应用程序,我想从前端打开一个计划的方法。 (因为我希望调度程序仅在从前端调用方法后运行)

此预定方法随后将调用具有给定参数的 Web 服务并继续运行,直到收到特定响应(“成功”)。

一旦收到特定的响应,我希望预定的方法停止运行,直到它被前端再次调用。

我不确定如何开始和停止计划方法的执行。

我目前有这个:

@Component
public class ScheduledTasks {

private static final Logger LOG = LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

@Scheduled(fixedRate = 5000)
public void waitForSuccess(String componentName) {
LOG.info("Running at: " + dateFormat.format(new Date()));
String response = MyWebService.checkStatus(componentName);
if ("success".equalsIgnoreCase(response)) {
LOG.info("success");
//Stop scheduling this method
} else {
LOG.info("keep waiting");
}
}
}

这是我的 Controller ,通过它可以打开预定的方法:

@Controller
public class MainController {

@GetMapping(/start/{componentName})
public @ResponseBody String startExecution(@PathVariable String componentName) {
//do some other stuff
//start scheduling the scheduled method with the parameter 'componentName'
System.out.println("Waiting for response");
}

}

我的做法是否正确?如何使用 springboot 和调度程序实现此功能?

最佳答案

这是 Spring Boot 中计划方法的启动/停止 API 的完整示例。您可以使用这样的 API:
http:localhost:8080/start - 以 5000 毫秒的固定速率启动预定方法
http:localhost:8080/stop - 用于停止计划的方法

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.time.Instant;
import java.util.concurrent.ScheduledFuture;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class TaskSchedulingApplication {

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

@Bean
TaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
}

@Controller
class ScheduleController {

public static final long FIXED_RATE = 5000;

@Autowired
TaskScheduler taskScheduler;

ScheduledFuture<?> scheduledFuture;

@RequestMapping("start")
ResponseEntity<Void> start() {
scheduledFuture = taskScheduler.scheduleAtFixedRate(printHour(), FIXED_RATE);

return new ResponseEntity<Void>(HttpStatus.OK);
}

@RequestMapping("stop")
ResponseEntity<Void> stop() {
scheduledFuture.cancel(false);
return new ResponseEntity<Void>(HttpStatus.OK);
}

private Runnable printHour() {
return () -> System.out.println("Hello " + Instant.now().toEpochMilli());
}

}

关于java - 如何动态打开/关闭springboot应用程序中的预定方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44464611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com