Sum*_*mit 8 java spring-scheduled spring-boot
我正在构建一个 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器,通过它可以打开预定的方法:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
我的方法正确吗?如何使用 springboot 和调度程序实现此功能?
Mak*_*iuk 10
这是 Spring Boot 中计划方法的启动/停止 API 的完整示例。您可以使用这样的 API:
http:localhost:8080/start - 用于以固定速率 5000 ms 启动预定方法
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());
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot 中计划方法的启动/停止 API。
@Component
public class ScheduledTasks {
private Logger logger = Logger.getLogger(ScheduledTasks.class);
@Value("${jobs.schedule.istime}")
private boolean imagesPurgeJobEnable;
@Override
@Transactional(readOnly=true)
@Scheduled(cron = "${jobs.schedule.time}")
public void execute() {
//Do something
//can use DAO or other autowired beans here
if(imagesPurgeJobEnable){
// Do your conditional job here...
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9600 次 |
最近记录: |