原文:http://www.gwtproject.org/doc/latest/DevGuideServerCommunication.html#DevGuideGettingUsedToAsyncCalls
RPC Plumbing Diagram
This section outlines the moving parts required to invoke a service. Each service has a small family of helper interfaces and classes. Some of these classes, such as the service proxy, are automatically generated behind the scenes and you generally will never realize they exist. The pattern for helper classes is identical for every service that you implement, so it is a good idea to spend a few moments to familiarize yourself with the terminology and purpose of each layer in server call processing. If you are familiar with traditional remote procedure call (RPC) mechanisms, you will recognize most of this terminology already.
rpc的service们,每一个都有一组辅助的接口和类。一些类,比如proxy,是自动生成的。每个service的辅助类都是固定的,所以先了解相关术语和每个类是干什么的。
Creating Services
In order to define your RPC interface, you need to:
- Define an interface for your service that extends RemoteService and lists all your RPC methods.
- Define a class to implement the server-side code that extendsRemoteServiceServlet and implements the interface you created above.
- Define an asynchronous interface to your service to be called from the client-side code.
Synchronous Interface
To begin developing a new service interface, create a client-side Java interface that extends the RemoteService tag interface.
package com.example.foo.client; import com.google.gwt.user.client.rpc.RemoteService; public interface MyService extends RemoteService { public String myMethod(String s); }
This synchronous interface is the definitive version of your service's specification. Any implementation of this service on the server-side must extendRemoteServiceServlet and implement this service interface.
package com.example.foo.server; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.example.client.MyService; public class MyServiceImpl extends RemoteServiceServlet implements MyService { public String myMethod(String s) { // Do something interesting with 's' here on the server. return s; } }
Tip: It is not possible to call this version of the RPC directly from the client. You must create an asynchronous interface to all your services as shown below.-从客户端没法直接调动同步接口,必须创建异步接口,然后调用异步接口。
Asynchronous Interfaces
Before you can actually attempt to make a remote call from the client, you must create another client interface, an asynchronous one, based on your original service interface. Continuing with the example above, create a new interface in the client subpackage:
package com.example.foo.client; interface MyServiceAsync { public void myMethod(String s, AsyncCallback<String> callback); }
The nature of asynchronous method calls requires the caller to pass in a callback object that can be notified when an asynchronous call completes, since by definition the caller cannot be blocked until the call completes. For the same reason, asynchronous methods do not have return types; they generally return void. Should you wish to have more control over the state of a pending request, return Request instead. After an asynchronous call is made, all communication back to the caller is via the passed-in callback object.-异步接口的本质决定了caller必须传入一个callback对象(异步的本质:caller在调用异步方法后不会等待返回结果),也决定了异步的方法不能有返回值,只能是void。方法执行结束后会通过callback对象找到返回后应该执行的代码。
Naming Standards
Note the use of the suffix Async and argument referencing the AsyncCallback class in the examples above. The relationship between a service interface and its asynchronous counterpart must follow certain naming standards. The GWT compiler depends on these naming standards in order to generate the proper code to implement RPC.-注意Async后缀和对AsyncCallback的引用。service接口和serviceAsync异步接口必须遵循命名规则。gwt编译器根据这些命名规则生成rpc代码。
- A service interface must have a corresponding asynchronous interface with the same package and name with the Async suffix appended. For example, if a service interface is named com.example.cal.client.SpellingService, then the asynchronous interface must be called com.example.cal.client.SpellingServiceAsync.-异步接口名=同步接口名+Async
- Each method in the synchronous service interface must have a coresponding method in the asynchronous service interface with an extra AsyncCallbackparameter as the last argument.-异步接口中的方法必须和service接口中一一对应,然后参数多一个AsyncCallback。