Quarkus具有调度任务的https://quarkus.io/guides/scheduler。但是,我想使用ScheduledExecutorService。这在夸克中是允许的吗?例如,在wildfly中,必须使用ManagedScheduledExecutorService,因为服务器正在管理线程,而不允许用户管理线程。这对夸克也有效吗?
发布于 2021-10-09 05:14:02
这是一个SimpleSheduler类
包:package io.quarkus.scheduler.runtime;
为了开发调度器扩展,他们使用了ScheduledExecutorService。
下面是一个使用ScheduledExecutorService,的调度任务
import javax.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ApplicationScoped
public class ScheduledExecutorRunnable {
List<String> list =
new ArrayList<String>();
public List<String> get() {
sheduleTask();
return list;
}
void sheduleTask() {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
Runnable task2 = () -> list.add("Running task2...");
task1();
ses.schedule(task2, 10, TimeUnit.SECONDS);
task3();
ses.shutdown();
}
void task1() {
list.add("Running task1...");
}
void task3() {
list.add("Running task3...");
}
}演示
import com.knf.dev.Resource.ScheduledExecutorService.ScheduledExecutorRunnable;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/verify")
public class EndPoint {
@Inject
ScheduledExecutorRunnable scheduledExecutorRunnable;
@GET
@Path("/sheduler")
@Produces(MediaType.APPLICATION_JSON)
public List<String> sheduler() {
return scheduledExecutorRunnable.get();
}
}命中端点:http://localhost:8080/verify/sheduler
输出:
“运行task1...",”运行task3...“
10秒后到达终结点
输出:
"Running task1...“、"Running task3...”、"Running task2...“、"Running task1...”、"Running task3...“
https://stackoverflow.com/questions/66748191
复制相似问题