cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache CXF Documentation > Dynamic Clients
Date Thu, 10 Dec 2009 03:04:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CXF20DOC&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="">Dynamic
     <h4>Page <b>edited</b> by             <a href="">Daniel
     <div class="notificationGreySide">
         <p>The usual way to construct a web service client is to include the Java interface
for the service (the SEI) and any classes that are used for inputs and output in the client
application. This is not always desirable or practical.</p>

<p>CXF supports several alternatives to allow an application to communicate with a service
without the SEI and data classes. JAX-WS specified the <a href="/confluence/display/CXF20DOC/JAX-WS+Dispatch+API"
title="JAX-WS Dispatch API">JAX&#45;WS Dispatch API</a>, as well as the Provider
interface for reading and writing XML. This page, however, describes the dynamic client facility
of CXF. With dynamic clients, CXF generates SEI and bean classes at runtime, and allows you
to invoke operations via APIs that take Objects, or by using reflection to call into full

<p>Note that, in general, CXF only supports WSI-BP services. If you attempt to create
a dynamic client for a WSDL that uses features outside of WSI-BP, CXF may throw an exception.

<h1><a name="DynamicClients-DynamicClientFactoryandJaxWsDynamicClientFactory"></a>DynamicClientFactory
and JaxWsDynamicClientFactory</h1>

<p>CXF provides two factory classes for dynamic classes. If your service is defined
in terms of JAX-WS concepts, you should use the JaxWsDynamicClientFactory. If you do not want
or need JAX-WS semantics, use the DynamicClientFactory. The remainder of this page uses the
JaxWs version.</p>

<p>Let's pretend for a moment that you have a WSDL which defines a single operation
"echo" which takes an input of a string and outputs a String. You could use the JaxWsDynamicClientFactory
for it like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(<span class="code-quote">"echo.wsdl"</span>);

<span class="code-object">Object</span>[] res = client.invoke(<span class="code-quote">"echo"</span>,
<span class="code-quote">"test echo"</span>);
<span class="code-object">System</span>.out.println(<span class="code-quote">"Echo
response: "</span> + res[0]);
<p>Many WSDLs will have more complex types though. In this case the JaxWsDynamicClientFactory
takes care of generating Java classes for these types. For example, we may have a People service
which keeps track of people in an organization. In the sample below we create a Person object
that was generated for us dynamically and send it to the server using the addPerson operation:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(<span class="code-quote">"people.wsdl"</span>,

<span class="code-object">Object</span> person = <span class="code-object">Thread</span>.currentThread().getContextClassLoader().loadClass(<span

Method m = person.getClass().getMethod(<span class="code-quote">"setName"</span>,
<span class="code-object">String</span>.class);
m.invoke(person, <span class="code-quote">"Joe Schmoe"</span>);

client.invoke(<span class="code-quote">"addPerson"</span>, person);
<p>You may be asking yourself the following question: "Where did the class name 'com.acme.Person'
come from?"</p>

<p>One way to get the class names is to run wsdl2java and examine the results. The dynamic
client factory uses the same code generator as that tool. Another way is to walk the CXF service
model. This has the advantage that it delivers Class&lt;?&gt; objects directly, so
you don't need to obtain the correct class loader reference and run loadClass.</p>

<p>The wsdl_first_dynamic_client sample uses this approach. Read the file ''
to see the process, which uses some of the java.bean classes to simplify the code slightly.</p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Note</b><br
/><p>The JaxWsDynamicClientFactory sets the Thread context ClassLoader to a new ClassLoader
that contains the classes for the generated types.   If you need the original ClassLoader,
make sure you save it prior to calling createClient.  </p></td></tr></table></div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>

       <a href="">View
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message