设为首页 加入收藏

TOP

通向架构师的道路(第十一天)之 Axis2 Web Service ( 二 )(三)
2018-02-08 09:56:56 】 浏览:754
Tags:通向 架构 师的 道路 十一 Axis2 Web Service
ed (this) { this.notify(); } } }

下面是客户端运行后的输出:

 

注意在CallBack类中的这一句:

OMElement result = element.getFirstChildWithName(new QName(
                                    "http://javatype.helloworld.axis2.sky.org","return"));

和下面这个SOAP UI的返回值来作个比较“注意这个Return”

“<ns:return>hello: Simon Shen</ns:return>”

三、深入理解Axis2的API的写法

我们一边看着SOAP UI给我们生成的调用端和结果来看这些个API。

Axis2中的一切都是一种OMElement,它其实就是一个“XML”结构的Java对象。

3.1 先来看调用端的生成

下面是Soap UI帮我们生成的客户端

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jav="http://javatype.helloworld.axis2.sky.org">

   <soapenv:Header/>

   <soapenv:Body>

      <jav:sayHello>

         <!--Optional:-->

         <jav:name>Simon Shen</jav:name>

      </jav:sayHello>

   </soapenv:Body>

</soapenv:Envelope>

通过上述的内容我们可以知道,调用一个以wsdl方式暴露的Web Service的客户端需要下面这些元素:

1)      调用地址 end point

private static EndpointReference targetEPR = new EndpointReference(
                    "http://localhost:8080/Axis2Service/services/HelloJava");

2)      Web Service方法,注意service.xml描述中的这一行

<actionMapping>urn:sayHello</actionMapping>

这个被称为我们的Web Method,它指向了我们在类中的:

public String sayHello(String name) throws XMLStreamException {

该方法有一个返回,一个进参,返回已经我们的service.xml描述中用:

<messageReceivers>
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                     class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>

它的进参我们假设是一个String类型的。

这样一个Web Method的原始调用(其实我们把它称为SOAP调用)应该是如SOAP UI工具中给我们生成的语句那样:

<jav:sayHello>
         <!--Optional:-->
         <jav:name>Simon Shen</jav:name>
    </jav:sayHello>

因此,为了生成上述的SOAP语句,我们采Axis2的API书写如下,下面的代码的效果将会在程序运行时产生等同于上面的SOAP调用语句,一起来看看:

OMFactory fac = OMAbstractFactory.getOMFactory();

           OMNamespace omNs = fac.createOMNamespace(

                             "http://javatype.helloworld.axis2.sky.org", "");

           OMElement method = fac.createOMElement("sayHello", omNs);

           OMElement name = fac.createOMElement("name", omNs);

           name.setText("ymk");

           method.addChild(name);

                   method.build();

当”method.build()”命令发出后,上述这些API就生成SOAP调用语句,去调用”sayHello”这个方法,然后往里面传入了一个String类型的值。

可以看到,这个API非常像dom4j的语句,只是这边不是一个document类型而是一个类XML的OMElement结构体,都是addChild, setText之类的,对吧?

再继续往下看我们的API

Options options = new Options();

           options.setTo(targetEPR);

           options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

           options.setUseSeparateListener(true);

                   options.setAction("urn:sayHello");

1)      将该web method的调用指向指定的end point

2)      开启双工模式

3)      因为是双工模式,需要在client端开启一个监听器

4)      设置调用的web method为”urn:sayHello”

sender = new ServiceClient();
                            sender.engageModule(Constants.MODULE_ADDRESSING);

                            sender.setOptions(options);
                            sender.sendReceiveNonBlocking(method, callback);

1)      生成调用句柄

2)    &n

首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux 性能检查命令总结 下一篇JDK 10 本周将进入候选发布阶段,..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目