查看redis client list
的输出,我看到目前有600个活跃客户端,并且它继续增长。这是一个输出片段:
id=285316 addr=x.x.x.x:55699 fd=14131 name= age=53055 idle=53029 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=sismember
id=285317 addr=x.x.x.x:55700 fd=14132 name= age=53055 idle=53050 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=sismember
这是我的代码:
Listener.java :
import com.sun.jersey.api.model.AbstractResourceModelContext;
import com.sun.jersey.api.model.AbstractResourceModelListener;
import javax.ws.rs.ext.Provider;
@Provider
public class Listener implements AbstractResourceModelListener {
@Override
public void onLoaded(AbstractResourceModelContext modelContext) {
RedisManager.getInstance().connect();
}
}
RedisManager.java :
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisManager {
private static final RedisManager instance = new RedisManager();
private static JedisPool pool;
private RedisManager() {
}
public final static RedisManager getInstance() {
return instance;
}
public void connect() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(5000);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setMaxIdle(50);
poolConfig.setMinIdle(1);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);
pool = new JedisPool(poolConfig, "redis_hostname");
}
public void release() {
pool.destroy();
}
public Jedis getJedis() {
return pool.getResource();
}
public void returnJedis(Jedis jedis) {
pool.returnResourceObject(jedis);
}
}
APIServlet.java :
@Path("/")
public class APIService {
@GET
@Path("/lookup")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@QueryParam("email") String email,
@QueryParam("pretty") String pretty
) throws JSONException {
Jedis jedis = RedisManager.getInstance().getJedis();
if (jedis.sismember("inprocess", email)) {
RedisManager.getInstance().returnJedis(jedis);
return Response.status(202).entity("{\"status\":202, " +
"\"processing\":{\"type\":\"Lookup performed\", " +
"\"message\":\"We're performing analysis on this " +
"record. Result should be ready in a few minutes" +
".\"}}").build();
}
Person person = new Person();
person.lookup(person);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(person);
JSONObject jsonObj = new JSONObject(jsonString);
jsonObj.remove("objectID");
jsonObj.remove("data_quality");
jsonObj.put("status", 200);
RedisManager.getInstance().returnJedis(jedis);
if (!jsonObj.isNull("name") && !jsonObj.get("name").equals("")) {
if (hasPretty) {
return Response.status(200).entity(jsonObj.toString(4))
.build();
}
return Response.status(200).entity(jsonObj.toString()).build();
}
return Response.status(404).entity("{\"status\":404, " +
"\"error\":{\"type\":\"Data Not Found.\", " +
"\"message\":\"We were not able to find data " +
"on this email.\"}}").build();
}
}
Maven依赖:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.2.0</version>
</dependency>
监听器创建一个RedisManager实例,在整个应用程序中使用 - 这应该只在启动时发生一次(注意:我不知道如何在关机时调用destroy,这很好知道)。在整个程序中,这个JedisPool实例用于Jersey路由,如APIServlet.java中所示。在路由中,我获得了一个JedisPool资源,然后在返回路径的任何部分之前,我返回了资源。
发生的事情是资源似乎没有被返回(或者我对池的理解是错误的)。经过一段时间后,与我的Redis实例的连接增长到最大总数5,000,然后我开始收到错误&#34;无法从池中获取资源,&#34;和汤姆猫死了。
我注意到的一些事情:
似乎有大量的ESTABLISHED https连接(不是100%肯定,但似乎是这种情况)。
所有闲置的Redis客户端(好吧,几乎全部都是)都有一个cmd的sismember。
注意:我没有包含完整的APIService.java代码,因为我真的不允许这样做。我包含的代码片段确实给出了代码的整体要点。我将返回整个APIService.java代码(返回404,返回429等),并在每次返回之前确保我将资源返回到池中。
最后,这是堆栈跟踪:
10-Feb-2016 08:04:23.161 SEVERE [http-nio-443-exec-14] com.sun.jersey.spi.container.ContainerResponse.mapMappableContainerException The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:50)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:86)
at co.talentiq.api.APIService.getMsg(APIService.java:63)
at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:2)
首先:如果您已初始化池,请不要创建新的:
public class RedisManager {
...
public void connect() {
if(pool != null) {
System.out.println("Already exists");
return;
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
第二......你的getMsg方法日志中是否有例外?
public Response getMsg(@QueryParam("email") String email,
@QueryParam("pretty") String pretty
您应该将所有使用池化资源的工作包装到try-catch-finally中并始终在finally块中返回资源。注意:确保不要将资源(在这种情况下为jedis)返回池两次。
Jedis jedis;
try {
jedis = RedisManager.getInstance().getJedis();
...
} finally {
if (jedis != null) {
RedisManager.getInstance().returnJedis(jedis);
jedis = null;
}
}
顺便说一下:你可以围绕你的jedis get / return代码创建一个小的AutoCloseable包装,并使用java try with resources - https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
使用try-with-resource的代码段
public void release() {
pool.destroy();
}
public static class JedisWrapper implements AutoCloseable {
private final JedisPoolConfig pool;
private final Jedis jedis;
public JedisWrapper(JedisPoolConfig pool, Jedis jedis) {
this.pool = pool;
this.jedis = jedis;
}
public Jedis get() {
return jedis;
}
@Override
public void close() {
pool.returnResourceObject(jedis);
}
}
public JedisWrapper getJedis() {
return new JedisWrapper(pool, pool.getResource());
}
// you can delete this method
public void returnJedis(Jedis jedis) {
pool.returnResourceObject(jedis);
}
后来在使用地点
public Response getMsg(@QueryParam("email") String email,
@QueryParam("pretty") String pretty
) throws JSONException {
try(JedisWrapper jw = ...) {
Jedis jedis = jw.get();
...
}