0

一部のリソースをプールするために apache pool2 を使用しようとしています。単体テストでは問題なく動作していますが、Spring 3 で使用しようとするとエラーが発生します。

他のすべては、コントローラーとサービスで動作しています。プーリング コードを追加する前にエンドポイントにアクセスできましたが、自動配線されたサービスは null ではありません。これは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)

また、@Autowired TestService を new 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() メソッド内で (つまり "==" を使用して) オブジェクトの同一性を比較したい場合、ラップされたオブジェクトをプロキシ オブジェクトと比較しているため、これは機能しません。これは明らかに false です。その問題を解決するために、プロキシの周りにラッパー オブジェクトを導入しました。ここでは、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 サービス ポートをラップして equals と hashcode をオーバーライドする必要がありました。

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 に答える