设为首页 加入收藏

TOP

通向架构师的道路(第十天)之 Axis2 Web Service(一)(五)
2018-02-08 09:56:58 】 浏览:736
Tags:通向 架构 师的 道路 Axis2 Web Service
", "name")); System.out.println(result.getText()); synchronized (this) { this.notify(); } } public boolean isComplete() { return complete; } public void onFault(MessageContext msgContext) { System.out.println(msgContext.getEnvelope().getBody().getFault() .toString()); synchronized (this) { this.notify(); } } public void onError(Exception e) { e.printStackTrace(); synchronized (this) { this.notify(); } } public void onComplete() { this.complete = true; synchronized (this) { this.notify(); } } }

有了callback接口,我们来写我们的webservice调用类

HelloWorldWithReturnNonBlock.java

package org.sky.axis2.helloworld;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.client.async.AxisCallback;

import org.apache.axis2.context.MessageContext;

public class HelloWorldWithReturnNonBlock {

         private static EndpointReference targetEPR = new EndpointReference(

                            "http://localhost:8080/Axis2Service/services/HelloWorld");

         public void sayHello() {

                   OMFactory fac = OMAbstractFactory.getOMFactory();

                   OMNamespace omNs = fac.createOMNamespace(

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

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

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

                   name.setText("ymk");

                   method.addChild(name);

                   method.build();

                   Options options = new Options();

                   options.setTo(targetEPR);

                   ServiceClient sender = null;

                   try {

                            HelloWorldNonBlockCB callback = new HelloWorldNonBlockCB();

                            sender = new ServiceClient();

                            sender.setOptions(options);

                            sender.sendReceiveNonBlocking(method, callback);

                            synchronized (callback) {

                                     try {

                                              callback.wait();

                                     } catch (InterruptedException e) {

                                              e.printStackTrace();

                                     }

                            }

                   } catch (AxisFault e) {

                            e.printStackTrace();

                   } finally {

                            if (sender != null)

                                     try {

                                               sender.cleanup();

                                     } catch (Exception e) {

                                     }

                   }

         }

         public static void main(String[] args) {

                   HelloWorldWithReturnNonBlock testClient = new HelloWorldWithReturnNonBlock();

                   testClient.sayHello();

         }

}

注意加粗标红处的代码。

4.3 非阻塞式双工HelloWorldWithReturnDualNonBlock.java

非阻塞式的Web Service客户端代码很简单,在此不做任何擅述,我们就说终极的非阻塞式双工模式的Web Service客户端使用Axis2的API如何实现。

记住以下几个口决:

  • options.setUseSeparateListener(true);

非阻塞式双工模式,会在客户端也打开一个监听器,而且一直不断的监听着服务器的返回值,该进程一旦被吊用,会一直被挂在客户端这边。

  • 设置webservice客户端模式为双工全开

options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

  • 先寻址,后engageModule

在双工模式下Web Service的客户端需要寻址,即engageModule,这个engageModule需要这样的一个参数:

engageModule(“addressing”)

或者也可写成:

engageModule(Constants.MODULE_ADDRESSING);
这个engageModule就是需要访问你的工程的WEB-INF\modules\目录下的一个叫addressing

首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇通向架构师的道路(第九天)之 we.. 下一篇通向架构师的道路(第八天)之 we..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目