我正在尝试使用 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 发生了什么