You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus/RestEasy中Jersey的CloseableService等效组件是什么?

在Quarkus/RestEasy中替代Jersey CloseableService的方案

在Quarkus/RestEasy里,有两种方案可以实现Jersey中CloseableService的核心功能——在请求结束(无论成功、抛出异常或连接中断)时可靠关闭资源:

方案一:使用Quarkus自带的CloseableService

Quarkus提供了同名的CloseableService类(位于io.quarkus.rest.runtime.spi包),用法和Jersey版本几乎完全一致,直接通过@Inject注入即可:

@Path("/datasets/{dataset}")
@Consumes("application/json")
@Produces("application/json")
@GET
public Stream<TSRecord> getDatasetDataJson(
        @PathParam("dataset") final String dsName,
        @QueryParam("parameter") final Parameter parameter,
        @Inject io.quarkus.rest.runtime.spi.CloseableService closer
) throws NoSuchDatasetException {
    final Stream<TSRecord> stream = database.stream(dsName, parameter);
    closer.add(stream::close);
    return stream;
}

方案二:使用标准JAX-RS的CompletionCallback

如果希望代码更贴合JAX-RS标准、不依赖Quarkus特定类,可以通过@Context注入ContainerRequestContext,调用其register方法注册回调,在请求完成时执行关闭操作:

@Path("/datasets/{dataset}")
@Consumes("application/json")
@Produces("application/json")
@GET
public Stream<TSRecord> getDatasetDataJson(
        @PathParam("dataset") final String dsName,
        @QueryParam("parameter") final Parameter parameter,
        @Context ContainerRequestContext requestContext
) throws NoSuchDatasetException {
    final Stream<TSRecord> stream = database.stream(dsName, parameter);
    requestContext.register(() -> stream.close());
    return stream;
}

两种方案都能达到和Jersey CloseableService完全一致的效果,可根据项目对标准API的依赖要求选择。

内容的提问来源于stack exchange,提问作者Matthias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:16:03