用于Java 6的JaxWS webservice客户端无法在Java 8中运行

时间:2015-09-22 15:07:31

标签: java web-services java-8 jax-ws

我有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中,上下文类加载器和类'类加载器都为null。

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

3 个答案:

答案 0 :(得分:6)

在从Java 6升级到Java 8之后,我们一直在看到这个问题(相同的堆栈跟踪和所有问题)。我将首先发布我们的解决方案,然后进行补充说明。

对我们来说,出现此问题的主要条件是:

  1. Java 8通过jvm.dll在Windows程序中作为嵌入式JVM运行。我试图重现在独立的Java 8 JVM中运行的问题,但我无法实现它。但是,独立运行对于诊断问题非常有帮助。
  2. 使用初始化与JAX-WS Web服务的连接的代理方法,该服务使用服务器端托管的WSDL在客户端动态创建存根。
  3. 解决方法:

    当您调用Service.getPort(Class<T>)时,它需要在自己的线程中完成,与先前运行调用的线程分开。这允许你有机会将该线程上的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产品中。如果没有remote debugging的JVM,则需要更长时间才能找到问题的根源。一旦我看到1)当我运行相同的代码,调用独立JVM(Windows产品之外)内的Service.getPort(Class<T>)调用,并且2)ClientContainer类能够获得当前线程的ClassLoader和3)ClassLoader返回的不是bootstrap ClassLoader(即,不是null),它让我意识到我必须找到一种方法来确保线程{{1运行中不会得到引导程序ClassLoader。然后我们的目标是看看我是否能找到一种方法来改变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来源中有两次尝试解析类加载器。问题是,如果这两个尝试都返回引导类加载器,则由于ClientContainer将为null,因此将在第45行产生NullPointerException:

    cl

    此解决方法可确保cl.getResource("META-INF/"+resource); 代码解析的类加载器将成为您在线程上设置的类加载器。

    我已经为JAX-WS团队提交了一张机票,可以在此处调查此问题:original java.net link现在在GitHub #1178上。

答案 1 :(得分:3)

升级到Java 8后我遇到了同样的问题。@ 2AGuy的答案有很多帮助。但是,问题发生在Mac和Linux上。 可能有一个更简单的版本来解决这个问题。 在调用getPort()之前,您的代码可以在行下执行:

Thread.currentThread().setContextClassLoader(XXX.class.getClassLoader());

这将把Bootstrap类加载器中的线程上下文类加载器替换为你设置它的人,而不需要为创建一个新线程来设置classLoader。

这对我来说仍然是个谜。我怀疑创建SOAP服务客户端代理(文档上下文中的服务端点代理)的线程是主线程或由它创建的,其classLoader是引导类加载器,在JVM启动时的最早阶段加载。基于Class#getClassLoader()的Java doc,出现NullPointer。

  

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

答案 2 :(得分:1)

尝试在Java8中再次为webservice客户端生成存根。

通常我们生成一个存根罐并将其放在类路径中。鉴于Java是向后兼容的,我们跳过了在升级到更高版本时重新生成jar的步骤。

但是对于Java 6&amp; 7它的工作只在Java 8中失败,我建议再次生成Stub,以排除Java 8的向后兼容性中的任何故障。