52ky 发表于 2022-5-2 13:07:04

第一次接触WEBSERVICE

公司采用Powerbuilder+Webserice的技术,可以在数据窗口中以SQL语句的形式向Webservice发送对数据库的请求,然后Webservice完成对数据库的请求并将结果返回给PB客户端.这样的表格对于只知道如何用Powerbuilder开发C/S程序的我来说就像是一针强心剂。这样,抛开程序的执行效率和开发效率,PB代码中随处可见的SQL语句,以及用于事务控制的语句提交、回滚等都没了,事务管理也没有了。这样我们在维护PB代码的时候就轻松多了,同时我们可以把一些业务逻辑丢到Webservice中间层来处理,这样我们的客户端就可以瘦了.

但我只使用了一个封装函数,与内部处理无关。 pb如何与webservice通信?

1.网络服务

直译Web服务,网络服务,在互联网上发布的一个程序。这样一来,Tomcat也应该被看成是一个Webservice,因为它也是一个发布在互联网上的程序。说到Tomcat,我们知道他处理的是http协议,接受请求,返回相应的响应;但是这里所说的Webserice是指能够在网络上发布自己的组件供客户端调用,中间传输对客户端是透明的,但典型的就是Http协议,http协议只承载数据。作为方法调用,客户端必须知道服务器端在网络上发布了哪些方法,这个方法有哪些参数,参数的类型是什么(整数?字符?对象),方法返回的是什么类型?作为服务端,你必须知道客户端调用的是哪个方法,传递了什么参数?要描述这部分数据,还必须有一个协议,也就是我们所说的SOAP(Simple Object Transfer Protocol)。当然,描述这类信息的不只是SOAP协议,还有其他的协议如(Rest Style),书中提到过,我之前没见过。我觉得可以自己写。

工作001

   客户端使用soap包将我们需要请求的数据处理成soap包消息发送给服务器。服务器解析soap消息并进行相应的处理。响应消息也被处理成soap包并作为响应返回给客户端。 .

所以让我们构建一个网络服务

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 来构建客户端。

进入网络服务向导

p001

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

P002

   这样,Pb 就不会响应了。 但是,如果直接填写浏览器保存的xml wsdl文件,一切都会正常。 到底是怎么回事?

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

运行程序,弹出消息,服务器已经响应。
这是网络服务器的基本原型。 实现的细节,比如soap消息是如何封装的,服务器如何解析客户端发送的请求,目前还不清楚; 需要更多的书。

(Using PowerBuilder + WebService technology, the company can send the request for database to WebService in the form of SQL statement in the data window, and then WebService completes the request for database and returns the result to Pb client Such forms are like a shot in the arm for me who only know how to develop C / s programs with PowerBuilder. In this way, regardless of the execution efficiency and development efficiency of the program, the SQL statements that can be seen everywhere in PB code, as well as the statement submission and rollback for transaction control, are gone, and the transaction management is also gone. In this way, it is much easier for us to maintain PB code. At the same time, we can throw some business logic into the middle layer of web service, so that our client can be thin
But I only use one encapsulated function, which has nothing to do with internal processing. How does Pb communicate with web service?
1. Network services
Web service, web service, a program published on the Internet. In this way, Tomcat should also be regarded as a web service, because it is also a program published on the Internet. Speaking of tomcat, we know that it deals with the HTTP protocol, accepts requests and returns corresponding responses; However, the WebService mentioned here refers to the ability to publish its own components on the network for the client to call. The intermediate transmission is transparent to the client, but the typical is HTTP protocol, which only carries data. As a method call, the client must know which methods the server publishes on the network, what parameters the method has, what type of parameters (integer? Character? Object), and what type the method returns? As a server, you must know which method the client calls and what parameters are passed? To describe this part of data, there must also be a protocol, which is what we call soap (Simple Object Transfer Protocol). Of course, it is not only the soap protocol that describes this kind of information, but also other protocols, such as (rest style), which are mentioned in the book and I haven't seen before. I think I can write it myself.
Job 001
The client uses soap package to process the data we need to request into soap package message and send it to the server. The server parses the SOAP message and processes it accordingly. The response message is also processed into a soap packet and returned to the client as a response
So let's build a web service
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(); }
}
Server interface and implementation class
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());
}
}
This class publishes the implemented services to the network through
% java ch01. Ts.timeserverpublisher, run publisher
We can get there http://127.0.0.1:9876/ts?wsdl Go to the WSDL file that describes the entire service. Our client can automatically generate relevant methods to access our web service by parsing WSDL files using soap components or packages.
2. PowerBuilder client
After the service is published successfully, you can send a request to the server through the client. Since the familiar tool I use now is PowerBuilder, I use PowerBuilder to build the client.
Enter the network service Wizard
p001
Enter the address of the published webserie WSDL file
P002
In this way, Pb will not respond. However, if you directly fill in the XML WSDL file saved by the browser, everything will be normal. What the hell is going on?
Sib (service implementation bean) has appeared in the interface. It goes all the way to next and finally forms project,
P004
After deploy, a timeserver implport object is formed. This object is formed according to the WSDL file and contains the methods of the webserie object. We can communicate with the server according to this object. Regardless of how the SOAP message is encapsulated, we don't have to worry about it, just call the methods in the object.
p005
Next, we can program the WebService object with more client-side
soapConnection conn //Define SoapConnection
timeserverimplport lts_ port
int li_ rVal
string ls_ time
conn = create SoapConnection
li_ Rval = conn.createinstance (lts_port, "timeserver import") / * instantiation*/
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
Run the program, pop up a message, and the server has responded.
This is the basic prototype of web server. The implementation details, such as how SOAP messages are encapsulated and how the server parses the requests sent by the client, are still unclear; Need more books.
)



页: [1]
查看完整版本: 第一次接触WEBSERVICE