首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

webService异步处置

2012-12-26 
webService异步处理第一步: 创建一个接口package testimport javax.jws.WebService@WebServicepublic in

webService异步处理
第一步: 创建一个接口

package test;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
public String sayHello(String name);
}

第二步:创建接口的实现类
package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements IHelloWorld {

public String sayHello(String name) {
try{
Thread.sleep(1000);
}catch(Exception ex) {

}
System.out.println("sayHello is called by " + name);
return "Hello " + name;
}

}

第三步:创建服务器端
package server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import test.HelloWorldImpl;

public class Server {

public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost:8888/HelloWorld");
factory.create();
}

}

第四步 创建客户端  Polling方式
package client;

import java.io.StringReader;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;

import org.xml.sax.InputSource;

public class AsyncPollingClient {

private static String payload =
"<ns1:sayHello xmlns:ns1='http://test/'>" +
"<arg0>Test</arg0>" +
"</ns1:sayHello>";

public static void main(String[] args) throws Exception {
QName serviceName = new QName("http://test/", "HelloWorldImplService");
Service service = Service.create(new URL("http://localhost:8888/HelloWorld?wsdl"), serviceName);
QName portName = new QName("http://test/", "HelloWorldImplPort");
Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Mode.PAYLOAD);

Source msg = new SAXSource(new InputSource(new StringReader(payload)));
Response<Source> responseSource = dispatch.invokeAsync(msg);

System.out.println("Sleep...");
Thread.sleep(1000);
System.out.println("Wake up...");

Source response = responseSource.get();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(response, new StreamResult(System.out));
}

}

第五步:创建客户端 CallBack方式
package client;

import java.io.StringReader;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;

import org.xml.sax.InputSource;

public class AsyncCallBackClient {

private static String payload =
"<ns1:sayHello xmlns:ns1='http://test/'>" +
"<arg0>Test</arg0>" +
"</ns1:sayHello>";

public static class SimpleAsyncHandler implements AsyncHandler<Source> {
public void handleResponse(Response<Source> responseSource) {
try{
Source response = responseSource.get();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(response, new StreamResult(System.out));
} catch(Exception ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception {
QName serviceName = new QName("http://test/", "HelloWorldImplService");
Service service = Service.create(new URL("http://localhost:8888/HelloWorld?wsdl"), serviceName);
QName portName = new QName("http://test/", "HelloWorldImplPort");
final Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Mode.PAYLOAD);

final Source msg = new SAXSource(new InputSource(new StringReader(payload)));
final AsyncHandler<Source> handler = new SimpleAsyncHandler();
new Thread(){
public void run(){
dispatch.invokeAsync(msg, handler);
}
}.start();

System.out.println("I'm doing other..");
}

}

热点排行