Start and stop scheduling task in Spring java
I develop a simple Spring Boot app that consumes REST and writes to DB.
I tried a @Scheduled
annotation to initiate a task to run it periodically. But the scheduling process starts automaticly, which is not exactly what I want. I need an ability to start and stop a scheduled task from a web page. When a user opens a page he must see a status of a process : Running/Stoped.
What is the easy way to implement it? Create a new thread? How to get a status of a process? Save it in db?
Maybe smb has an example of starting and stoping schedduled task from web page?
Try to use ScheduledExecutorService. For example, first of all create a ScheduledExecutorService
:
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(System.getRuntime().availableProcessors());
then create a scheduled task:
ScheduledFuture<?> task = scheduledExecutorService.scheduleAtFixedRate(
() -> System.out.println("some task"), 0, 30, TimeUnit.SECONDS);
and when you want to cancel the task, do the following:
task.cancel(true);
链接地址: http://www.djcxy.com/p/50906.html
上一篇: 如何更改Google文档网站在Android中显示PDF的方式?
下一篇: 在java中启动和停止调度任务