天天看點

CXF之用Dispatch處理異步調用執行個體

服務接口:

package cxf.server;

import javax.jws.WebService;

@WebService

public interface HelloWorld {

String sayHi(String text);

}

服務接口實作:

package cxf.server;

import javax.jws.WebService;

@WebService(endpointInterface = "cxf.server.HelloWorld")

public class HelloWorldImpl implements HelloWorld {

public String sayHi(String text) {

try {

Thread.sleep(30);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("sayHi called");

return "Hello " + text;

}

}

服務端配置:

<?xml version="1.0" encoding="UTF-8"?>

<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">

<import resource="classpath:META-INF/cxf/cxf.xml" />

<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:server id="helloWorld" serviceClass="cxf.server.HelloWorld" address="/HelloWorld">

<jaxws:serviceBean>

<bean class="cxf.server.HelloWorldImpl"/>

</jaxws:serviceBean>

<jaxws:features>

<bean class="org.apache.cxf.feature.LoggingFeature"/>

</jaxws:features>

</jaxws:server>

</beans>

用戶端調用1---輪詢方式異步調用:

package cxf.client;

import java.io.InputStream;

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.soap.MessageFactory;

import javax.xml.soap.SOAPMessage;

import javax.xml.transform.dom.DOMSource;

import javax.xml.ws.Dispatch;

import javax.xml.ws.Response;

import javax.xml.ws.Service;

public final class Client1 {

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

URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl");

QName serviceName = new QName("http://server.cxf/", "HelloWorldService");

Service service = Service.create(wsdlUrl,serviceName);

QName portName = new QName("http://server.cxf/", "HelloWorldPort");

Dispatch<DOMSource> disp = service.createDispatch(portName, DOMSource.class, Service.Mode.MESSAGE);

InputStream input = Client1.class.getResourceAsStream("soap1.xml");

MessageFactory factory = MessageFactory.newInstance ();

SOAPMessage soapMessage = factory.createMessage(null, input);

DOMSource dom = new DOMSource(soapMessage.getSOAPPart());

//輪詢方式

Response<DOMSource> rsp = disp.invokeAsync(dom);

while(!rsp.isDone()){

Thread.sleep(5);

System.out.println("sleep 5");

}

DOMSource domRsp = rsp.get();

System.out.println(domRsp.getNode().getLastChild().getNodeName());

System.out.println(domRsp.getNode().getLastChild().getTextContent());

}

}

用戶端調用2---回調方式異步調用:

package cxf.client;

import java.io.InputStream;

import java.net.URL;

import java.util.concurrent.Future;

import javax.xml.namespace.QName;

import javax.xml.soap.MessageFactory;

import javax.xml.soap.SOAPMessage;

import javax.xml.ws.Dispatch;

import javax.xml.ws.Service;

public final class Client2 {

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

URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl");

QName serviceName = new QName("http://server.cxf/", "HelloWorldService");

Service service = Service.create(wsdlUrl,serviceName);

QName portName = new QName("http://server.cxf/", "HelloWorldPort");

Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

InputStream input = Client2.class.getResourceAsStream("soap1.xml");

MessageFactory factory = MessageFactory.newInstance ();

SOAPMessage soapMessage = factory.createMessage(null, input);

MyAsyncHandler<SOAPMessage> handler = new MyAsyncHandler<SOAPMessage>();

//回調方式

Future<?> rsp = disp.invokeAsync(soapMessage, handler);

while(!rsp.isDone()){

Thread.sleep(5);

System.out.println("aaaaaaa");

}

SOAPMessage soapRsp = (SOAPMessage)rsp.get();

System.out.println(soapRsp.getSOAPBody().getNodeName());

System.out.println(soapRsp.getSOAPBody().getLastChild().getTextContent());

}

}

回調方式的AsyncHandler實作類:

package cxf.client;

import javax.xml.ws.AsyncHandler;

import javax.xml.ws.Response;

public class MyAsyncHandler<T> implements AsyncHandler<T> {

/**

* 這個回調類必須實作handleResponse()方法,它被調用由CXF運作時通知用戶端響應已經到達。

*/

@Override

public void handleResponse(Response<T> res) {

System.out.println("eeeeee");

}

}

請求的xml資料:soap1.xml

<?xml version="1.0" encoding="utf-8" ?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SOAP-ENV:Body>

<m:sayHi xmlns:m="http://server.cxf/">

<arg0>I like you!aha!!</arg0>

</m:sayHi>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

還有一個單向調用(OneWay)

當一個請求不需要一個響應的時候,你的遠端調用可以使用Dispatch對象上的invokeOneWay()方法。略。