gpt4 book ai didi

java - 使用 Jersey CDI 自动关闭数据库连接?

转载 作者:行者123 更新时间:2023-12-01 21:59:06 24 4
gpt4 key购买 nike

我想向 Jersey 资源注入(inject)一个实现 AutoClosable 的对象,并让框架在请求结束时自动关闭它。
基本上,这是一个数据库连接提供程序,打开的连接应在请求结束时关闭。
使用 @inject 注释时,对象不会关闭。
这可能吗?如何实现?

最佳答案

您可以使用CloseableService

The service may be used within the scope of a request to add instances of Closeable that are to be closed when the request goes out of scope, more specifically after the request has been processed and the response has been returned.

您可以将此服务注入(inject)到 Factory 中,您可以在其中创建您的提供程序。只需将提供程序添加到 Factory 内的 CloseableService 中,它将在请求结束时关闭。

public class ConnectionProviderFactory implements Factory<ConnectionProvider> {

@Inject
private CloseableService closeableService;

@Override
public ConnectionProvider provider() {
ConnectionProvider provider = new ConnectionProvider();
closeableService.add(provider);
return provider;
}

@Override
public void dispose(ConnectionProvider provider) {
}
}

然后只需在你的 Binder 中注册工厂即可

bindFactory(ConnectionProviderFactory.class)
.to(ConnectionProvider.class)
.in(RequestScoped.class);

请注意,Factory 类有一个 dispose 方法。我不能百分百确定它是如何工作的细节,因为我有过有时它被调用,有时不被调用的经验。我发现 CloseableService 更可靠。也许有一天我会四处挖掘,尝试为自己解开 dispose 方法的谜团。

<小时/>更新

不太确定您在做什么不同的事情,但下面是使用 Jersey Test Framework 的完整测试。像任何其他 JUnit 测试一样运行它。唯一的区别是我使用 Closeable 来代替 AutoCloseable

import java.io.Closeable;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.CloseableService;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class InjectCloseableTest extends JerseyTest {

public static final String CONNECTION_MSG = "Connection";

public static class ConnectionProvider implements Closeable {
private static final Logger LOGGER
= Logger.getLogger(ConnectionProvider.class.getName());

public String getConnection() {
return CONNECTION_MSG;
}

@Override
public void close() {
LOGGER.info("---- Closing Connection ----");
}
}

@Path("connection")
public static class ConnectionResource {

@Inject
ConnectionProvider connectionProvider;

@GET
public String get() {
return connectionProvider.getConnection();
}
}

public static class ConnectionProviderFactory implements Factory<ConnectionProvider> {

@Inject
CloseableService closeableService;

@Override
public ConnectionProvider provide() {
ConnectionProvider provider = new ConnectionProvider();
closeableService.add(provider);
return provider;
}

@Override
public void dispose(ConnectionProvider t) {}
}

@Override
public ResourceConfig configure() {
return new ResourceConfig(ConnectionResource.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true))
.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ConnectionProviderFactory.class)
.to(ConnectionProvider.class)
.in(RequestScoped.class);
}
});
}

@Test
public void doit() {
Response response = target("connection").request().get();
assertEquals(200, response.getStatus());
assertEquals(CONNECTION_MSG, response.readEntity(String.class));
response.close();
}
}

这是运行它所需的唯一依赖项。

<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey2.version}</version>
<scope>test</scope>
</dependency>

关于java - 使用 Jersey CDI 自动关闭数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914394/

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