GenericObjectPool返回的对象当前不属于此池

时间:2019-06-30 05:47:44

标签: java pool apache-commons-pool

我创建了一个需要重用的对象池。每次尝试返回对象时,都会出现以下错误:“返回的对象当前不属于此池”

我已经覆盖了equals和hashcode方法,但这无济于事。

我使用以下代码设置池:

GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxIdle(10);
config.setMaxTotal(10);
config.setTestOnBorrow(true);
config.setTestOnReturn(true); 
ImageDownloaderPool.POOL= new ImageDownloaderPool<String, String>(new ImageDownloaderFactory<String, String>(), config);

这是我重写equals和hashCode的方式:

@Override
public boolean equals(Object o) {
   if (this == o) return true;
   if (!(o instanceof ImageDownloader)) return false;
   ImageDownloader<String, String> that = (ImageDownloader<String, String>) o;
   if (id != null ? !id.equals(that.id) : that.id != null) return false;
   return true;
}

@Override
public int hashCode() {
  return id != null ? id.hashCode() : 0;
}

我这样在Pool上调用returnObject:

 finally { 
   if (dl != null) { 
      try {
         ImageDownloaderPool.getPOOL().returnObject(dl);
      } 
      catch (Exception e2) {
        e2.printStackTrace();
      }

我收到此错误:

  

java.lang.IllegalStateException:返回的对象当前不属于此池   org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:524)   com.ifmrestoration.webscraper.CorrigoScraperPool.returnObject(CorrigoScraperPool.java:45)   com.ifmrestoration.webscraper.ImageDownloader.saveImageFromAWSurl(ImageDownloader.java:164)   com.ifmrestoration.webscraper.ImageDownloaderServlet.doPost(ImageDownloaderServlet.java:49)   javax.servlet.http.HttpServlet.service(HttpServlet.java:707)   javax.servlet.http.HttpServlet.service(HttpServlet.java:790)   org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848)   org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1772)   com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)   org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1759)   org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582)   org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)   org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)   org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)   org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)   org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)   com.google.apphosting.runtime.jetty9.ParseBlobUploadHandler.handle(ParseBlobUploadHandler.java:119)   org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1182)   com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.doHandle(AppEngineWebAppContext.java:187)   org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512)   org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)   org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)   org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)   com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:293)   org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)   org.eclipse.jetty.server.Server.handle(Server.java:539)   org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:333)   com.google.apphosting.runtime.jetty9.RpcConnection.handle(RpcConnection.java:213)   com.google.apphosting.runtime.jetty9.RpcConnector.serviceRequest(RpcConnector.java:81)   com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:134)   com.google.apphosting.runtime.JavaRuntime $ RequestRunnable.dispatchServletRequest(JavaRuntime.java:722)   com.google.apphosting.runtime.JavaRuntime $ RequestRunnable.dispatchRequest(JavaRuntime.java:685)   com.google.apphosting.runtime.JavaRuntime $ RequestRunnable.run(JavaRuntime.java:655)   com.google.apphosting.runtime.JavaRuntime $ NullSandboxRequestRunnable.run(JavaRuntime.java:847)   com.google.apphosting.runtime.ThreadGroupPool $ PoolEntry.run(ThreadGroupPool.java:270)   java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

由于您要使用两个类实例化池,所以我假设您正在扩展

org.apache.commons.pool2.impl.GenericKeyedObjectPool
在这种情况下,您应该使用:

pool.returnObject("yourkey", yourobject);

不需要AFAIK覆盖。