4

WSDL2JAVA(使用 XMLBeans 绑定选项)通过 Axis2 1.5.4 生成的存根是线程安全的吗?

实际上,我已经为通过多个线程调用的 Web 服务创建了一个存根。我已经配置了我自己的MultiThreadedHttpConnectionmanager并设置了,但是我在每次调用后都会HTTPConstants.REUSE_HTTP_CLIENT看到一些 NullPointerExceptions 。stub._getServiceClient().cleanupTransport

有时线程也会挂起。

同时我注意到在Web Service操作方法中生成的Stub中,在finally块中已经调用了cleanup()。我以后不应该打电话给stub._getServiceClient().cleanupTransport自己吗?

我的代码:

        httpConnMgr = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = httpConnMgr.getParams();
        if (params == null) {
            params = new HttpConnectionManagerParams();

        }
        params.setDefaultMaxConnectionsPerHost(numberOfThreads);
        httpConnMgr.setParams(params);
        HttpClient httpClient = new HttpClient(httpConnMgr);

        service = new Service1Stub(this.endPointAddress);
        service._getServiceClient().getOptions()
                .setTimeOutInMilliSeconds(this.timeOut);
        service._getServiceClient().getOptions()
                .setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Boolean.TRUE);
        service._getServiceClient().getOptions()
        .setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, Boolean.FALSE);
        service._getServiceClient()
                .getOptions()
                .setProperty(HTTPConstants.SO_TIMEOUT, (int) (this.timeOut));
        service._getServiceClient()
                .getOptions()
                .setProperty(HTTPConstants.CONNECTION_TIMEOUT,
                        (int) (this.timeOut));
        service._getServiceClient().getServiceContext().getConfigurationContext()
                .setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

同时在生成的存根中,我注意到 cleanUp 已经被调用:

   finally {
            _messageContext.getTransportOut().getSender().cleanup(_messageContext);
        }

任何建议都会很有帮助。谢谢。

4

1 回答 1

7

当我前段时间研究 Axis2 时,我也有关于它的线程安全相关问题。

查找有关 Axis2 线程安全的信息很困难,但我最终遇到了以下 Jira 问题:https ://issues.apache.org/jira/browse/AXIS2-4357

提到:

Axis2 客户端不是线程安全的,从项目一开始就是这种情况 [...] 为不同的线程使用不同的存根 [...]

问题本身以Won't Fix状态和以下注释关闭:

Axis2 存根不是线程安全的。正如 Deepal 指出的那样,这是设计使然。

那是当时为我做的。

基本上你需要每个线程使用一个存根,或者你可以使用存根池(如果我没记错的话)存根可以重复使用(但仍然需要每个线程使用一个存根以避免任何问题)。其他人似乎已经成功使用了存根池(请参见此处的相关 SO 问题)。

关于线程安全,我通常遵循的一个建议是:如果没有明确说明某些东西是线程安全的,那么就假设它不是.

于 2011-05-14T19:26:22.150 回答