Asynchronous Web Service Using CXF

Asynchronous Web Service Using CXF explains about Creating / Developing JAX-WS Asynchronous Web service with the help of Apache CXF, Spring, Eclipse and deployed in Tomcat

In this example we are showing implementing an Asynchronous SOAP services using CXF Java first approach.

The asynchronous model allows the client thread to continue after making a two-way invocation without being blocked while awaiting a response from the server. Once the response is available, it is delivered to the client application asynchronously

For this there are two alternative approaches:

1) Callback: client application implements the interface to accept notification of the response availability
2) Polling: client application periodically polls a instance to check if the response is available

Reference ->

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.3
  4. Tomcat 7

Following jar must be in classpath

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

CXF Asynchronous Web Service Tutorial

I am creating a sample web service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFTutorial" according to following screenshot

Create CXF Project CXF Asynchronous Tutorial

Create a Student Object

package com.student;

public class Student {
private String name;
public String getName() {
return name;
public void setName(String name) { = name;

Create a Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import java.util.concurrent.Future;

import javax.jws.WebService;

@WebService(name = "ChangeStudentDetails")
public interface ChangeStudentDetails {

public Response<Student> changeStudentAsync(String name);

@ResponseWrapper(localName = "changeStudentResponse", className = "com.student.Student")
public Future<?> changeStudentAsync(String requestType,AsyncHandler<Student> asyncHandler);

@ResponseWrapper(localName = "changeStudentResponse", className = "com.student.Student")
public String changeStudent(String name);

Implement the Service Interface

Here we implement the service interface created on the previous step

org.apache.cxf.annotations.UseAsyncMethod is available from 2.6.0. By Using @UseAsyncMethod annotation, if the transport supports asynchronous feature, CXF will call the async version that takes an AsynHandler object and the service can call that handler when the response is ready. If the transport does not support the CXF will use the synchronous method as normal.

package com.student;

import java.util.concurrent.Future;

import javax.jws.WebService;

import org.apache.cxf.annotations.UseAsyncMethod;
import org.apache.cxf.jaxws.ServerAsyncResponse;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

// synchronous method, as before
public String changeStudent(String name) {
System.out.println("Executing operation changeStudent synchronously\n");
return "How are you " + name;

// async callback Method (new method)
public Future<?> changeStudentAsync(final String name, final AsyncHandler<Student> asyncHandler) {
System.out.println("Executing operation changeStudentAsync asynchronously\n");
final ServerAsyncResponse<Student> asyncResponse = new ServerAsyncResponse<Student>();
new Thread() {
public void run() {
try {
} catch (InterruptedException e) {
Student resp = new Student();
("How are you " + name);
("Responding on background thread\n");

return asyncResponse;

// async polling Method (new method)
public Response<Student> changeStudentAsync(String requestType) {
return null;

Create a cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation class on "jaxws:endpoint" tag

<beans xmlns=""
    xmlns:xsi="" xmlns:jaxws=""

    <jaxws:endpoint id="changeStudent"
        implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent" />


Change web.xml

Change the web.xml file to find CXF servlet and cxf.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
    xmlns="" xmlns:web=""
    id="WebApp_ID" version="3.0">




Publishing CXF Web Service

Run CXF On Tomcat


You can Find all the deployed JAX-WS/JAX-RS services you need to append 'services' at the end of the URL so URL will become following


Deployed CXF Web Service

CXF Asynchronous WebService Running


you can also need CXF Asynchronous client example in order to run this service

4 Responses to "Asynchronous Web Service Using CXF"
  1. sen 2014-03-16 10:02:54.0
  1. globero 2014-03-17 10:02:54.0
  1. admin 2014-03-18 10:02:54.0
  1. Khaled AFI 2014-03-19 10:02:54.0

Your email address will not be published. Required fields are marked *