Actually Making a Call

The process of making an RPC from the client always involves the exact same steps.
  1. Instantiate the service interface using GWT.create().
  2. Specify a service entry point URL for the service proxy using ServiceDefTarget.
  3. Create an asynchronous callback object to be notified when the RPC has completed.
  4. Make the call.

Example

Suppose you want to call a method on a service interface defined as follows:
public interface MyEmailService extends RemoteService {
  void emptyMyInbox(String username, String password);
}
Its corresponding asynchronous interface will look like this:
public interface MyEmailServiceAsync {
  void emptyMyInbox(String username, String password,
      AsyncCallback callback);
}
The client-side call will look like this:
public void menuCommandEmptyInbox() {
  // (1) Create the client proxy. Note that although you are creating the
  // service interface proper, you cast the result to the asynchronous
  // version of
  // the interface. The cast is always safe because the generated proxy
  // implements the asynchronous interface automatically.
  //
  MyEmailServiceAsync emailService = (MyEmailServiceAsync) GWT.create(MyEmailService.class);

  // (2) Specify the URL at which our service implementation is running.
  // Note that the target URL must reside on the same domain and port from
  // which the host page was served.
  //
  ServiceDefTarget endpoint = (ServiceDefTarget) emailService;
  String moduleRelativeURL = GWT.getModuleBaseURL() + "email";
  endpoint.setServiceEntryPoint(moduleRelativeURL);

  // (3) Create an asynchronous callback to handle the result.
  //
  AsyncCallback callback = new AsyncCallback() {
    public void onSuccess(Object result) {
      // do some UI stuff to show success
    }

    public void onFailure(Throwable caught) {
      // do some UI stuff to show failure
    }
  };

  // (4) Make the call. Control flow will continue immediately and later
  // 'callback' will be invoked when the RPC completes.
  //
  emailService.emptyMyInbox(fUsername, fPassword, callback);
}
It is safe to cache the instantiated service proxy to avoid creating it for subsequent calls.

Related topics

RemoteService, ServiceDefTarget, GWT.create(Class)