CXF Asynchronous Client

2. CXF Asynchronous Client Using Callback

The client application get responses from the server when response is ready, there is no polling happens here.

package com.student.client;

import java.util.concurrent.Future;

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

import com.student.ChangeStudentDetails;


public final class StudentClient {

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

       
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.setServiceClass
(ChangeStudentDetails.class);
        factory.setAddress
("http://localhost:8081/CXFTutorial/ChangeStudent?wsdl");
        factory.getInInterceptors
().add(new LoggingInInterceptor());
        factory.getOutInterceptors
().add(new LoggingOutInterceptor());
        ChangeStudentDetails client =
(ChangeStudentDetails) factory.create();

       
       
// callback method
       
TestAsyncHandler testAsyncHandler = new TestAsyncHandler();
        System.out.println
("Invoking changeStudentAsync using callback object...");
        Future<?> response = client.changeStudentAsync
(
               
System.getProperty("user.name"), testAsyncHandler);
       
while (!response.isDone()) {
           
Thread.sleep(100);
       
}
       
String resp = testAsyncHandler.getResponse();
        System.out.println
("Server responded through callback with: " + resp);

        System.exit
(0);
   
}
}

AsyncHandler Callback Implementation

package com.student.client;

import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;

import com.student.Student;

public class TestAsyncHandler implements AsyncHandler<Student> {

   
private Student    reply;

   
public void handleResponse(Response<Student> response) {
       
try {
           
System.err.println("handleResponse called");
            reply = response.get
();
       
} catch (Exception ex) {
           
ex.printStackTrace();
       
}
    }

   
public String getResponse() {
       
return reply.getName();
   
}

}
Output
Invoking changeStudentAsync using callback object...
Server responded through callback with: How are you Rockey

 





Previous 1 2








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