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 被调用了");

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值