CXF 服务调用DEMO
方法1:
wsdl2java:
通过wsdl2java命令在客户端生成服务对象,通过调用生成的服务对象就能调用远程的服务接口。Wsdl2java使用位用cxf下载包的bin目录下:
具体用法如下:
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
在客户端生成如下java文件:
客户端调用:
import client.Hello;
import client.HelloService;
import client.User;
public class client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hello sh = new HelloService().getHelloPort();
User user = new User();
user.setName("aa");
user.setAge("23");
System.out.println(sh.sayHello(user));
}
}
方法2:
异步polling技术
客户端定时检查是否调用成功
public class AsyncPolling {
//组装SOAP,调用服务器sayHello方法
private static String sayHello =
"<ns1:sayHello xmlns:ns1='http://service/'>" +
" <arg0>"+
"<name>aa</name>"+
"<age>23</age>"+
"</arg0>" +
"</ns1:sayHello>";
public static void main(String[] args)throws Exception{
callService(sayHello);
}
public static void callService(String payload) throws Exception{
/**
** “http://service/” 对应 targetNamespace*
** “HelloSerice”对应 serive*
**/
QName serviceName = new QName("http://service/", "HelloService");
Service service = Service.create(new URL("http://localhost:8080/testcxf/ws/hello?wsdl"), serviceName);
/**
** “http://service/” 对应 targetNamespace*
** “HelloPort”对应 port*
**/
QName portName = new QName("http://service/", "HelloPort");
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技术
服务器端回调客户端,通知调用成功或失败
public static void callService1(String payload) throws Exception{
QName serviceName = new QName("http://service/", "HelloService");
Service service = Service.create(new URL("http://localhost:8080/testcxf/ws/hello?wsdl"), serviceName);
QName portName = new QName("http://service/", "HelloPort");
final Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Mode.MESSAGE);
final Source msg = new SAXSource(new InputSource(new StringReader(payload)));
final AsyncHandler<Source> handler = new resourceAsynHandler();
new Thread() {
public void run() {
Future<?> rsp = dispatch.invokeAsync(msg, handler);
Source response;
Transformer transformer;
try {
response = (Source) rsp.get();
transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(response, new StreamResult(System.out));
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
}catch(Exception e2){
e2.printStackTrace();
}
}
}.start();
System.out.println("I'm doing other..");
}
resourceAsynHandler类
public class resourceAsynHandler implements AsyncHandler<Source>{
public void handleResponse(Response<Source> res) {
System.out.println("resourceAsynHandler 被调用了");
}
}