Asynchronous Web Service Using CXF

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;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;
import javax.xml.ws.ResponseWrapper;

@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 javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;

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
   
@UseAsyncMethod
   
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 {
                   
Thread.sleep(10000);
               
} catch (InterruptedException e) {
                   
e.printStackTrace();
               
}
               
Student resp = new Student();
                resp.setName
("How are you " + name);
                asyncResponse.set
(resp);
                System.out.println
("Responding on background thread\n");
                asyncHandler.handleResponse
(asyncResponse);
           
}
        }
.start();

       
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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

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

</beans>





Previous 1 2 3 Next








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

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