Webservice初接触

   公司用到了Powerbuilder+Webserice的技术,能将数据窗口中对数据库的请求,以SQL语句的形式,发到Webservice中,然后由Webservice完成对数据库的请求,并将结果返回给PB客户端。这样的形式,对于我这个原来只知道用Powerbuilder开发C/S程序的人来说,就像打了一针强心针。通过这样的方式,抛开程序的执行效率和开发效率不说,就是PB代码中的随处可见的SQL语句,以及对事务控制的语句commit,rollback等等,都不见了,对事务管理都交给了Webservice,这样,我们在维护PB代码的时候,轻松了很多,同时也可以将部分的业务逻辑丢到Webservice这个中间层中来处理,让我们的客户端瘦起来。

  可是我用到的只一个封装好的函数,对内部的处理一无所只,pb是如何与webservice通信的呢?

1.Webservice

  从字面上来翻译Web服务,网络服务,一个发布在网络上的程序,照这么说,Tomcat应该也算是个Webservice,因为他也是一个发布在网络上的程序,说道Tomcat,我们就知道,他处理的是http协议,接受请求,返回相应;不过,这里说的Webserice是指能将自己的组件发布在网络上供客户端调用,中间的传输对客户端是透明的,不过典型的还是Http协议,http协议只是承载数据的,做为方法的调用,客户端必须知道,服务端有哪些方法发布在网络上,这个方法有些什么参数,参数的类型是什么(整形?字符?对象),这个方法返回的类型是什么?而做为服务端必须要知道客户端调用的是哪个方法,传递了哪些参数?要描述这部分数据,必须也要一个协议,这就是我们说的SOAP(简单对象传输协议)。当然要描述这样的信息并不只是SOAP协议,还有其它的协议比如(Rest Style),书上题到的,也没见过,我觉得完全也可以自己写。

  工作001

   客户端通过soap的包讲我们需要请求的数据加工成soap包消息,发送给服务端,服务端解析soap消息,进行相应的处理,讲响应的消息同样加工成soap包,作为响应返回给客户端。

  那么我们来建立一个webservice吧

package ch01.ts;  // time server

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 *  The annotation @WebService signals that this is the
 *  SEI (Service Endpoint Interface). @WebMethod signals 
 *  that each method is a service operation.
 *
 *  The @SOAPBinding annotation impacts the under-the-hood
 *  construction of the service contract, the WSDL
 *  (Web Services Definition Language) document. Style.RPC
 *  simplifies the contract and makes deployment easier.
*/
@WebService
@SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer {
    @WebMethod String getTimeAsString();
    @WebMethod long getTimeAsElapsed();
}



package ch01.ts; 

import java.util.Date;
import javax.jws.WebService;

/**
 *  The @WebService property endpointInterface links the
 *  SIB (this class) to the SEI (ch01.ts.TimeServer).
 *  Note that the method implementations are not annotated
 *  as @WebMethods.
*/
@WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer {
    public String getTimeAsString() { return new Date().toString(); }
    public long getTimeAsElapsed() { return new Date().getTime(); }
}

服务端的接口,以及实现类

public class TimeServerPublisher {
    public static void main(String[ ] args) {
      // 1st argument is the publication URL
      // 2nd argument is an SIB instance
      Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
    }
}

这个类将实现的服务发布在网络上,我们通过
% java ch01.ts.TimeServerPublisher,运行发布程序

我们就可以通过http://127.0.0.1:9876/ts?wsdl,就可以到描述整个服务的wsdl文件了,我们的客户端,利用soap的组件或者是包,解析这个wsdl文件,就能自动生成访问我们webservice服务的相关方法。

2.PowerBuilder客户端

 当服务发布成功以后,就可以通过客户端发送对服务端的请求了,由于我现在用熟悉的工具是是Powerbuilder,因此我用Powerbuilder来建立一个客户端。

进入webservice向导

p001

输入发布的webserice的wsdl文件的地址

P002

 这样Pb会没反应,但是,直接填写用浏览器另存为的xml的wsdl文件,确能一切正常,这是怎么回事?

P003

SIB(Service Implementation Bean)已经出现在界面中了,一路Next,最后形成了Project,

 P004
 
deploy以后形成一个timeserverimplPort对象,这个对象根据wsdl文件形成,包含了webserice对象的方法,我们就能够根据这个对象与服务端进行通讯了,而又管soap消息是如何封装的,我们不用去管,只管调用对象中的方法就行。
p005
 
接下来就能够更具客户端的webservice对象进行编程了
soapConnection conn //Define SoapConnection
timeserverimplport lts_port
int li_rVal
string ls_time
conn = create SoapConnection 
li_rVal = conn.CreateInstance(lts_port, "timeserverimplport") /*实例化*/
try
    ls_time=  lts_port.gettimeasstring( )
   // Invoke service
   messagebox("", ls_time)
catch ( SoapException e )
   messagebox ("ErrDAT*    d or", "Cannot invoke Web service")   
    // error handling   
end try
destroy conn

p006

运行程序,消息弹出,已经得到了服务端得相应。
这就是一个webserce的基本的雏形,有关实现的细节,比如soap的消息是如何封装的,服务端是如何对客户端发送过来的请求如何解析的,这些都还不清楚;还需要多多看书,多多了解,先这样吧。
 
 
 
原文地址:https://www.cnblogs.com/szxiaofei14/p/3216246.html