tumblr counter

Create CXF Client

Create CXF Client Example explains step by step details of how to create a CXF web service client / wsdl soap client using Apache CXF WebService Framework

For Creating Apache CXF Client, We are using org.apache.cxf.jaxws.JaxWsProxyFactoryBean. The factory option is particular to CXF and it is non-standard. This will give you a great control over the endpoint.

The Other options like Wsimport tool available with JDK are standard but that will give you less control. For example you can add a logging interceptor in order to show the logs when accessing your service using org.apache.cxf.jaxws.JaxWsProxyFactoryBean.

For publishing a web service, you can follow this tutorial CXF Web Service Tutorial. This client is based on mentioned Tutorial

Note

For running this client you need to refer the classes created on CXF Web Service Tutorial

You can also read about implementing an Asynchronous client CXF Asynchronous Client

Required Libraries

You need to download following libraries in order to Generate CXF Client

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.3
  4. Tomcat 7 (Deploying Web Service)

Following jar must be in classpath

  1. commons-logging-1.1.1.jar
  2. cxf-2.7.3.jar
  3. httpasyncclient-4.0-beta3.jar
  4. httpclient-4.2.1.jar
  5. httpcore-4.2.2.jar
  6. httpcore-nio-4.2.2.jar
  7. neethi-3.0.2.jar
  8. spring-aop-3.0.7.RELEASE.jar
  9. spring-asm-3.0.7.RELEASE.jar
  10. spring-beans-3.0.7.RELEASE.jar
  11. spring-context-3.0.7.RELEASE.jar
  12. spring-core-3.0.7.RELEASE.jar
  13. spring-expression-3.0.7.RELEASE.jar
  14. spring-web-3.0.7.RELEASE.jar
  15. wsdl4j-1.6.2.jar
  16. xmlschema-core-2.0.3.jar

Create CXF Client

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.student.ChangeStudentDetails;
import com.student.Student;

// CXF JAX-WS Client / Consuming Web Services With CXF

public final class StudentClient {

   
public static void main(String args[]) throws Exception {

     
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

      factory.setServiceClass
(ChangeStudentDetails.class);
      factory.setAddress
("http://localhost:8080/CXFTutorial/ChangeStudent?wsdl");
      factory.getInInterceptors
().add(new LoggingInInterceptor());
      factory.getOutInterceptors
().add(new LoggingOutInterceptor());
      ChangeStudentDetails client =
(ChangeStudentDetails) factory.create();
      Student student =
new Student();
      student.setName
("Rockey");
      Student changeName = client.changeName
(student);
      System.out.println
("Server said: " + changeName.getName());
      System.exit
(0);
   
}
}
Output

Server said: Hello Rockey

 




Generate CXF Client How To Create A WSDL CXF Client Generate Webservice Client With CXF CXF Client CXF JAX-WS Client CXF SOAP Client Consuming Web Services With CXF org.apache.cxf.jaxws.JaxWsProxyFactoryBean Example How Do I Develop CXF Client Developing CXF Consumer

You might also like following tutorials :

Java Tutorial Apache CXF Tutorial Hibernate Tutorial Spring Tutorial Struts Tutorial
4 Comments on "Create CXF Client"
20.10.2012 18:24:27 lacike
For all you that are wondering where to get the service class - just make a reference to server project classes folder in client project build path ;-)
22.10.2012 17:06:19 admin
Thanks for your suggestion, I have updated the post
22.02.2014 03:23:13 sorty
When invoking the client, it takes 50-60 seconds after i am getting java.net.SocketTimeoutException: Read timed out, following are the stacktrace,,,, could you help me

Caused by: org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:220)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:276)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:222)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:171)
... 26 more
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1000)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1900)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1828)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:590)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
... 31 more
22.02.2014 05:52:49 admin
@sorty,

You can use HTTPConduit and HTTPClientPolicy and need to set ConnectionTimeout and ReceiveTimeout when invoking the service, please see the below code

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import com.student.ChangeStudentDetails;
import com.student.Student;

// How to handle java.net.SocketTimeoutException: Read timed out exception

public final class StudentClient {

    public static void main(String args[]) throws Exception {

      JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
      long timeout = 10000L;

      factory.setServiceClass(ChangeStudentDetails.class);
      factory.setAddress(http://localhost:8080/CXFTutorial/ChangeStudent);
      factory.getInInterceptors().add(new LoggingInInterceptor());
      factory.getOutInterceptors().add(new LoggingOutInterceptor());
      ChangeStudentDetails ChangeStudentService = (ChangeStudentDetails) factory.create();
      
      Client client = ClientProxy.getClient(ChangeStudentService);
      if (client != null) {
          HTTPConduit conduit = (HTTPConduit) client.getConduit();
          HTTPClientPolicy policy = new HTTPClientPolicy();
          policy.setConnectionTimeout(timeout);
          policy.setReceiveTimeout(timeout);
          conduit.setClient(policy);
      }
      
      Student student = new Student();
      student.setName(Rockey);
      Student changeName = ChangeStudentService.changeName(student);
      System.out.println(Server said:  + changeName.getName());
      System.exit(0);
    }
}

Note; Here we are using CXF's JaxWsProxyFactoryBean, if you need to set timeout in JAX-WS way, you can set
request timeout on request context property. See the code below

java.util.Map requestContext =((javax.xml.ws.BindingProvider) port).getRequestContext();
requestContext.put(com.sun.xml.internal.ws.request.timeout, new Long(600000));