0

我正在尝试使用 apache pool2 来汇集一些资源。我让它在单元测试中运行良好,但是当我在 spring 3 中尝试使用它时,我得到了一个错误。

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

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

所以,我的问题是:为什么这在单元测试中有效,但在弹簧控制器/服务中失败

控制器:

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

此外,如果我用新的 TestService() 替换 @Autowired TestService 似乎也无济于事。

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

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

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

4

2 回答 2

4

当我尝试轮询 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;
}

}

于 2014-04-08T21:00:33.963 回答
0

我在 jaxws 端口代理中看到了同样的怪异之处,这(obj == obj)是真的但是(obj.equals(obj))假的。因此,就像您在评论中提到的那样,在将端口对象放入池中之前,我必须包装 Jaxws 服务端口以覆盖等号和哈希码。

Groovy Proxy 类使这变得非常简单。这是我正在使用的代理:

class JaxwsPortProxy<T> extends Proxy {
    private final UUID poolId = UUID.randomUUID()

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

        JaxwsPortProxy that = (JaxwsPortProxy) o

        poolId == null ? that.poolId == null : poolId.equals(that.poolId)
    }

    @Override
    int hashCode() {
        poolId?.hashCode() ?: 0
    }
}

然后我的 BasePooledObjectFactory.create() 方法的结尾简单地包装了服务端口,如下所示:

return new JaxwsPortProxy<RuleServiceWS>().wrap(ruleService)

感谢您帮助我找到此解决方案!

于 2014-05-07T14:17:36.097 回答