gpt4 book ai didi

quarkus 和 ScheduledExecutorService

转载 作者:行者123 更新时间:2023-12-04 13:28:16 26 4
gpt4 key购买 nike

Quarkus 有一个 https://quarkus.io/guides/scheduler来安排任务。但是,我想使用 ScheduledExecutorService .这在夸克中是允许的吗?例如,在wildfly 中有ManagedScheduledExecutorService必须使用,因为服务器正在管理线程,不允许用户管理线程。这对夸克也有效吗?

最佳答案

这是一个 SimpleSheduler 类(class)
包裹:package io.quarkus.scheduler.runtime; https://github.com/quarkusio/quarkus/blob/main/extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/SimpleScheduler.java
为了开发调度程序扩展,他们使用了 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
输出:
["正在运行任务 1...","正在运行任务 3..."]
10 秒后到达终点
输出:
["正在运行任务1...","正在运行任务3...","正在运行任务2...","正在运行任务1...","正在运行任务3..."]

关于quarkus 和 ScheduledExecutorService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66748191/

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