将 JedisPool 与 Tomcat 一起使用,资源不会返回到池中

Using JedisPool with Tomcat, resources not being returned back to pool

查看 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>

Listener 创建一个 RedisManager 实例以在整个应用程序中使用——这应该只在启动时发生一次(注意:我不知道如何在关闭时调用 destroy,如果知道的话会很高兴)。在整个程序中,Jersey 路由中使用了 JedisPool 的这个实例,如 APIServlet.java 所示。在路线中,我得到一个 JedisPool 资源,然后在路线的任何部分 return 之前,我 return 资源。

发生的事情是资源似乎没有被 returned(或者我对池的理解是错误的)。一段时间后,与我的 Redis 实例的连接增长到 maxTotal 5,000,然后我开始收到错误 "could not get a resource from the pool," 和 Tomcat 死亡。

我注意到的一些事情:

  1. 似乎有大量 ESTABLISHED https 连接存在(对此不是 100% 肯定,但似乎是这样)。

  2. 所有空闲的 Redis 客户端(好吧,几乎所有空闲的)都有一个 sismember 命令。

注意:我没有包含完整的 APIService.java 代码,因为我确实不允许这样做。我包含的片段确实给出了代码的整体要点。我在整个 APIService.java 代码(return 404、return 429 等)中 returning,并且在每个 return 之前我确保我是 return将资源添加到池中。

最后,这里是堆栈跟踪:

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.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)

第一:如果您已经初始化了池,请不要创建新池:

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 中,并且始终 return finally 块中的资源。注意:确保不要 return 资源(在本例中为 jedis)池化两次。

Jedis jedis;
try {
    jedis = RedisManager.getInstance().getJedis();
    ...
} finally {
    if (jedis != null) {
       RedisManager.getInstance().returnJedis(jedis);
       jedis = null;
    }
}

顺便说一句:您可以围绕您的 jedis get/return 代码创建一个小型 AutoCloseable 包装器并使用 java 尝试使用资源 - 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();
      ...
   }