java - Apache Pool 无法在 Spring Controller /服务中返回对象

标签 java spring-mvc pool apache-commons-pool

我正在尝试使用 apache pool2 来池化一些资源。我让它在单元测试中工作正常,但是当我尝试在 spring 3 中使用它时,出现错误。

其他一切都与 Controller 和服务一起工作。在添加池代码之前我可以访问端点,并且 Autowiring 服务不为空。这一切都使用 context:component-scan

连接在一起

如果我在 Controller 方法中设置断点,我会看到从借用返回的对象是 org.apache.cxf.jaxws.JaxWsClientProxy@15de00c 。然后检查 Activity 池中的对象给出 org.apache.cxf.jaxws.JaxWsClientProxy@15de00c

所以,我的问题是:为什么这在单元测试中有效,但在 spring Controller /服务中失败

Controller :

 @Controller
 public class TestController() {

 @Autowired
 private TestService testService

 @RequestMapping(value="/test", method=RequestMethod.GET)
 public ModelAndView getTest() throws Exception {

 GenericObjectPool<MyObj> pool = testService.getPool();
pool.returnObject(pool.borrowObject());

 return new ModelAndView("jsp/test", "command", new TestObj()); //not really relevant yet

   }
 }     

和服务:

 @Service
 public class TestService implements DisposableBean {

 GenericObjectPool<MyObj> pool;

public TestService () {
    pool = new GenericObjectPool<MyObj>(new MyObjPooledObjectFactory());
}

public GenericObjectPool<MyObj> getPool() {
    return pool;
}

public void setPool(GenericObjectPool<MyObj> pool) {
    this.pool = pool;
}


@Override
public void destroy() throws Exception {
    LOG.info("DESTROYING Service");
    this.pool.close();
}
 }

工厂:

 MyObjPooledObjectFactory extends BasePooledObjectFactory<MyObj> {
  @Override
 public MyObjc create() throws Exception {
       MyObj myObj = expensive call.
     return myObj;
  }
 @Override
  public PooledObject<MyObj> wrap(MyObj obj) {
    return new DefaultPooledObject<MyObj>(obj);
}
 }


@Override
public void destroyObject(PooledObject<MyObj > p) throws Exception {
super.destroyObject(p);
 p.releaseConnection();
 }

终于我有了

 @Test
public void testMe() {


    TestService service = new TestService();
        GenericObjectPool<MyObj> pool = service.getPool();
    try {
        pool.returnObject(pool.borrowObject());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }


}

我得到的错误是:

 java.lang.IllegalStateException: Returned object not currently part of this pool
 at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:537)
 at com.example.controller.TestController.getTest(TestController.java:56)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:779)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:821)
 at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
 at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
 at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
 at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
 at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:89)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
 at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
 at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
 at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
 at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
 at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
 at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
 at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
 at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
 at weblogic.work.ExecuteThread.run(ExecuteThread.java:176)

此外,如果我用 new TestService() 替换 @Autowired TestService 似乎也没有帮助。

嗯,我不知道为什么会失败,但我已经将其缩小到一个完全不同的问题:

            if (!myObj.equals(myObj)) {
                throw new IllegalStateException("WTF, Why is this not equal to itself!");
            }

会抛出异常。所以现在我需要弄清楚这个 jaxWsClientProxy 发生了什么

最佳答案

当我尝试轮询 jaxws 客户端端口时,我遇到了同样的问题。根本原因是相同的 equals 方法不适用于 Proxy 对象。然而,如果我们想在 equals() 方法中比较对象标识(即使用“==”),这是行不通的,因为我们正在将包装对象与代理对象进行比较,这显然是错误的。为了解决这个问题,我在代理周围引入了包装对象,其中定义了可以在 equals() 和 hashCode() 方法中使用的字段“id”。

代码

public class DefaultCommonsPooledObject<T> extends DefaultPooledObject<T> {

private final UUID id = UUID.randomUUID();

/**
 * Create a new instance that wraps the provided object so that the pool can track the state of the pooled object.
 * 
 * @param object The object to wrap
 */
public DefaultCommonsPooledObject(T object) {
    super(object);
}

@Override
public boolean equals(Object o) {
    if (this == o)
        return true;
    if (!(o instanceof DefaultCommonsPooledObject))
        return false;

    DefaultCommonsPooledObject that = (DefaultCommonsPooledObject) 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;
}

}

关于java - Apache Pool 无法在 Spring Controller /服务中返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824889/

相关文章:

java - java配置中的Spring MVC在访问jsp时得到404

java - SOAP 代理客户端的 Spring Boot 通用异常处理程序

java - 有效地池化 ByteBuffer 的实例

java - 如何解决我的 JavaFX TableView 中的这个视觉故障?

java - Eclipse IDE项目目录结构帮助【Java】

java - Rest 中的更新方法(如 Controller )

java - 字符串池——字符串总是存在于常量池中吗?

java - 是否可以在一行代码中执行两个不同的 "+="和 "-="操作?

java - 如何使用 KeyListener 移动 JFrame 中的矩形?

python - 带有嵌套网络请求的 Gevent 池