Java 6的JaxWS webservice客户端在Java 8中无法工作

8

我有一个JaxWS Web服务客户端,多年来一直在Java 6中成功运行。现在当Java升级到版本8时,我们在获取端口时遇到了NullPointerException异常。

java.lang.NullPointerException
    at com.sun.xml.internal.ws.client.ClientContainer$1.getResource(Unknown Source)
    at com.sun.xml.internal.ws.assembler.MetroConfigLoader.locateResource(Unknown Source)
    at com.sun.xml.internal.ws.assembler.MetroConfigLoader.locateResource(Unknown Source)
    at com.sun.xml.internal.ws.assembler.MetroConfigLoader.init(Unknown Source)
    at com.sun.xml.internal.ws.assembler.MetroConfigLoader.<init>(Unknown Source)
    at com.sun.xml.internal.ws.assembler.TubelineAssemblyController.getTubeCreators(Unknown Source)
    at com.sun.xml.internal.ws.assembler.MetroTubelineAssembler.createClient(Unknown Source)
    at com.sun.xml.internal.ws.client.Stub.createPipeline(Unknown Source)
    at com.sun.xml.internal.ws.client.Stub.<init>(Unknown Source)
    at com.sun.xml.internal.ws.client.Stub.<init>(Unknown Source)
    at com.sun.xml.internal.ws.client.Stub.<init>(Unknown Source)
    at com.sun.xml.internal.ws.client.sei.SEIStub.<init>(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getStubHandler(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBaseProxy(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
    at javax.xml.ws.Service.getPort(Unknown Source)
    at myclient.stub.MyService.<init>(MyService.java:38) 

我已经尝试使用Java 7版本1.7.0_80运行它,那里也可以工作,但是Java 8的第一个版本会导致此异常。

我已经苦苦思索了一段时间,所以如果有人能给我任何线索从哪里开始修复这个问题,那就太好了。

这是WSDL,我对其进行了一些编辑,因为这不是我的服务,但希望这足够了吗?

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<wsdl:definitions xmlns:ns1="http://www.dummyservice/sample/interface" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Sample"
        targetNamespace="http://www.dummyservice/sample/interface">
  <wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    targetNamespace="http://www.dummyservice/sample/interface" xmlns="http://www.dummyservice/sample/interface"
    elementFormDefault="qualified" jaxb:version="2.0">
      <xs:element name="PersonQuery">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="system" type="xs:string" />
            <xs:element name="user" type="xs:string" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="PersonReply">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Header" type="HeaderType" />
            <xs:element name="person" type="PersonType" minOccurs="0" maxOccurs="1" />
            <xs:element name="address" type="AddressType" minOccurs="0" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:complexType name="HeaderType">
        <xs:sequence>
          <xs:element name="tila" type="StatusType" />
        </xs:sequence>
      </xs:complexType>
      <xs:simpleType name="StatusType">
        <xs:annotation>
          <xs:appinfo>
            <jaxb:typesafeEnumClass>
              <jaxb:typesafeEnumMember name="SUCCESS" value="0001" />
              <jaxb:typesafeEnumMember name="FAIL" value="0000" />
            </jaxb:typesafeEnumClass>
          </xs:appinfo>
        </xs:annotation>
        <xs:restriction base="xs:string">
          <xs:enumeration value="0000" />
          <xs:enumeration value="0001" />
        </xs:restriction>
      </xs:simpleType>
      <xs:complexType name="PersonType">
        <xs:sequence>
          <xs:element name="firstname" type="xs:string" minOccurs="0" />
          <xs:element name="lastname" type="xs:string" minOccurs="0" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="AddressType">
        <xs:sequence>
          <xs:element name="addresstype" type="AddresstypeType" minOccurs="0" />
          <xs:element name="streetaddress" type="xs:string" minOccurs="0" />
          <xs:element name="city" type="xs:string" minOccurs="0" />
          <xs:element name="postalcode" type="xs:string" minOccurs="0" />
        </xs:sequence>
      </xs:complexType>
      <xs:simpleType name="AddresstypeType">
        <xs:annotation>
          <xs:appinfo>
            <jaxb:typesafeEnumClass>
              <jaxb:typesafeEnumMember name="HOME" value="001" />
              <jaxb:typesafeEnumMember name="OFFICE" value="002" />
            </jaxb:typesafeEnumClass>
          </xs:appinfo>
        </xs:annotation>
        <xs:restriction base="xs:string">
          <xs:enumeration value="001" />
          <xs:enumeration value="002" />
        </xs:restriction>
      </xs:simpleType>
    </xs:schema>
  </wsdl:types>
  <wsdl:message name="PersonQueryOperationRequest">
    <wsdl:part element="ns1:PersonQuery" name="parameters" />
  </wsdl:message>
  <wsdl:message name="PersonQueryOperationResponse">
    <wsdl:part element="ns1:PersonReply" name="parameters" />
  </wsdl:message>
  <wsdl:portType name="SamplePort">
    <wsdl:operation name="PersonQueryOperation">
      <wsdl:input message="ns1:PersonQueryOperationRequest" />
      <wsdl:output message="ns1:PersonQueryOperationResponse" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="SampleSOAP" type="ns1:SamplePort">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="PersonQueryOperation">
      <soap:operation soapAction="http://www.dummyservice/sample/interface/SampleOperation" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SampleService">
    <wsdl:port binding="ns1:SampleSOAP" name="Sample">
      <soap:address location="https://127.0.0.1/data/ws" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

编辑:类加载似乎是问题所在,该ClientContainer中的上下文类加载器和类的类加载器均为空。
private final ResourceLoader loader = new ResourceLoader() {
public URL More ...getResource(String resource) throws MalformedURLException {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null) {
        cl = this.getClass().getClassLoader();
    }
    return cl.getResource("META-INF/"+resource);
}
};

当我们在调用ws之前将系统类加载器显式设置为上下文类加载器时,它开始工作了。但这是一个好的解决方法吗?我想知道为什么Java8停止工作,这可能是ws实现中的问题吗?
谢谢, Janne

你能否编辑你的问题,包括你的WSDL,这样我们就可以尝试重新创建这个问题吗? - VGR
我已经包含了 WSDL,但我不得不对其进行编辑以与原始文件区分开来,希望这样可以。 - Janne Rantala
看起来 该方法 抛出 NullPointerException 的唯一方式是当类被系统 ClassLoader 加载时(因为 Java 允许返回 null 来表示系统 ClassLoader)。你正在运行什么操作系统?你尝试使用最新版本的 Java 了吗? - VGR
这个问题出现在Windows Server 2008 R2环境中。我尝试过1.8.0_60版本,如果我没记错的话,那是最新可用的版本。我们的Java是通过从C语言编写的应用程序中的JNI使用的。现在,如果我在Eclipse中运行相同的jar文件进行单元测试,它可以完美地工作。 - Janne Rantala
3个回答

6
我们在从Java 6升级到Java 8后也遇到了这个问题(完全相同的堆栈跟踪)。我将首先发布我们的解决方案,然后是额外的说明。
对于我们来说,此问题发生的主要条件是:
1. 使用jvm.dll通过Windows程序嵌入式运行Java 8。我尝试在独立的Java 8 JVM中复制该问题,但无法让它发生。然而,在独立运行时诊断问题非常有帮助。 2. 使用代理方法初始化与JAX-WS Web服务的连接,该方法动态使用托管在服务器端的WSDL在客户端上创建存根。
解决方法:
当您调用Service.getPort(Class )时,需要在其自己的线程中执行,与之前运行调用的线程分开。这为您提供了一个机会,在该线程上设置ClassLoader以不是引导类加载器的ClassLoader,这是ClientContainer.java中问题的关键点(下面有更多解释)。以下是一些示例代码,适用于我们的工作。您需要修改它以适应您的需求。
public class YourClass {
  private YourWebService yourWebService;

  // You may want to synchronize on this method depending on your use case
  public YourWebService getYourWebService() {
    if ( this.yourWebService == null ) {
      // We create a thread so that we can set the ClassLoader
      Thread t = new Thread() {
        public void run() {
          synchronized(this) {
            // Get the string of your webservice WSDL from somewhere
            String url = "http://YOURHOST:YOURPORT/your-web-service/YourWebService?wsdl";
            URL srvUrl = null;
            try {
              srvUrl = new URL( url );
            } catch ( MalformedURLException ex ) {
              throw new RuntimeException( String.format( "Malformed URL: %s", url ), ex );
            }

            QName qName = new QName( "your-webservice-namespace", "YourWebServiceName" );
            Service service = Service.create( srvUrl, qName );
            this.yourWebService = service.getPort( YourWebService.class );
            notify();
          }
        }
      };

      // Thread.currentThread().getContextClassloader()
      // returns null in com.sun.xml.internal.ws.client.ClientContainer.
      // (See http://hg.openjdk.java.net/jdk8/jdk8/jaxws/file/d03dd22762db/src/share/jaxws_classes/com/sun/xml/internal/ws/client/ClientContainer.java, lines 39-47)
      // To work around that, I force setting of the ContextClassLoader 
      // on this thread (in which the Service.getPort() method will run) so 
      // that when ClientContainer calls Thread.currentThread().getContextClassLoader(), it doesn't get a null 
      // (i.e., the bootstrap classloader).
      //
      t.setContextClassLoader( YourClass.class.getClassLoader() );
      t.start();
      // Wait until above thread completes in order to return yourWebService 
      synchronized( t ) {
        try {
          t.wait();
        } catch ( InterruptedException e ) {
          e.printStackTrace();
        }
      }
    }

    return this.yourWebService;
  }
} 

额外的背景和细节:

对我们来说,诊断这个问题的困难在于该问题仅发生在启动嵌入式JVM的Windows产品中。如果没有远程调试该JVM,要找到问题的根本原因将会花费更长的时间。当我看到以下三点时,我才意识到必须找到一种方法来确保运行ClientContainer的线程不会获得引导类加载器:1)当我在独立的JVM中运行调用Service.getPort(Class<T>)的相同代码时(在Windows产品之外),2)ClientContainer类能够获取当前线程的ClassLoader,3)返回的ClassLoader不是引导ClassLoader(即不为null)。因此,目标就是要找到一种方法来改变由ClientContainer代码解析的ClassLoader。

ClientContainer 源代码:http://hg.openjdk.java.net/jdk8/jdk8/jaxws/file/d03dd22762db/src/share/jaxws_classes/com/sun/xml/internal/ws/client/ClientContainer.java

注意在 ClientContainer 源代码中,有两个尝试解析类加载器的操作。问题在于如果这两个尝试都返回引导类加载器,则会在第 45 行抛出 NullPointerException,因为 cl 将为 null:

cl.getResource("META-INF/"+resource);

这个解决方法可以确保 ClientContainer 代码使用的类加载器是您在线程上设置的类加载器。
我已经为 JAX-WS 团队提交了一个工单,以便调查此处的问题: java.net原始链接 ,现在在 GitHub #1178 上。

3
我升级到Java 8后遇到了同样的问题,@2AGuy的答案很有帮助。然而,这个问题在我运行Mac和Linux时都出现了。 可能有一种更简单的方法来解决这个问题。在调用getPort()之前,您的代码可以执行以下行: Thread.currentThread().setContextClassLoader(XXX.class.getClassLoader()); 这将将线程上下文类加载器从引导类加载器替换为您设置为其所有者,而无需创建一个新线程来设置classLoader。
根本原因仍然是个谜。我怀疑创建SOAP服务客户端代理(文档上下文中的服务端点代理)的线程是主线程或由它创建的线程,它的classLoader是引导类加载器,在JVM启动时加载的最早阶段。根据Class#getClassLoader()的Java doc,会出现NullPointer。

某些实现可能使用null来表示引导类加载器。如果此类是由引导类加载器加载的,则在这种实现中,此方法将返回null。


1
尝试在Java8中再次生成webservice客户端的存根。通常我们会生成一个存根的jar并将其放入类路径中。鉴于Java是向后兼容的,当升级到更高版本时,我们跳过重新生成jar的步骤。但由于在Java 6和7中它可以工作,只有在Java 8中出现故障,我建议再次生成存根以排除Java 8向后兼容性中的任何故障。

我用Java 8生成了它们,但这没有任何影响。 - Janne Rantala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接