如何调用2个CXF动态客户端?

时间:2011-12-29 09:02:33

标签: cxf

尝试调用两个CXF-2.5.0动态客户端失败:

  

org.apache.cxf.interceptor.Fault:编组错误:此上下文不知道net.webservicex.GetWeather

package my.test;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class TwoDynClients {
    public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client clientA = dcf.createClient("http://www.webservicex.net/stockquote.asmx?WSDL");
        Object[] resultA = clientA.invoke(new QName("http://www.webserviceX.NET/", "GetQuote"), "GOOG");
        System.out.println("resultA: " + resultA[0]);
        Client clientB = dcf.createClient("http://www.webservicex.net/GlobalWeather.asmx?wsdl");
        Object[] resultB = clientB.invoke(new QName("http://www.webserviceX.NET", "GetWeather"), new Object[] {"Stuttgart", "Germany"} );
        System.out.println("resultB: " + resultB[0]);
    }
}

以上代码段的输出是:

29.12.2011 9:37:12 org.apache.cxf.jaxb.JAXBUtils logGeneratedClassNames
INFO: Created classes: net.webservicex.GetQuote, net.webservicex.GetQuoteResponse, net.webservicex.ObjectFactory
resultA: <StockQuotes><Stock><Symbol>GOOG</Symbol><Last>639.70</Last><Date>12/28/2011</Date><Time>4:00pm</Time><Change>0.00</Change><Open>N/A</Open><High>N/A</High><Low>N/A</Low><Volume>0</Volume><MktCap>207.2B</MktCap><PreviousClose>639.70</PreviousClose><PercentageChange>0.00%</PercentageChange><AnnRange>473.02 - 645.00</AnnRange><Earns>29.337</Earns><P-E>21.81</P-E><Name>Google Inc.</Name></Stock></StockQuotes>
29.12.2011 9:37:22 org.apache.cxf.jaxb.JAXBUtils logGeneratedClassNames
INFO: Created classes: net.webservicex.GetCitiesByCountry, net.webservicex.GetCitiesByCountryResponse, net.webservicex.GetWeather, net.webservicex.GetWeatherResponse, net.webservicex.ObjectFactory
29.12.2011 9:37:24 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {http://www.webserviceX.NET}GlobalWeather#{http://www.webserviceX.NET}GetWeather has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: net.webservicex.GetWeather is not known to this context
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:261)
  • 当客户订单发生变化时,第二次失败。
  • 使用一个JaxWsDynamicClientFactory创建两个客户端是否可以?

1 个答案:

答案 0 :(得分:1)

Daniel Kulp的回答是How to call 2 CXF dynamic clients?

ClassLoader cl = Thread.currentThread().getContextClassloader();
JaxWsDynamicClientFactory dcf =
JaxWsDynamicClientFactory.newInstance();
Client clientA = dcf.createClient(".....");
Thread.currentThread().setContextClassloader(cl);
dcf = JaxWsDynamicClientFactory.newInstance();
Client clientB = dcf.createClient(".....");