Services in GWT
Google Web Toolkit is a Web Application Framework dealing with Servers and Clients. When a Server is required to do some processing in a web-app Services must be used. A service is used to invoke server-side code from the client (and vice-versa occasionally).
In GWT the Client side code pages run more like an application within the Client (Browser), so requesting the HTML pages from the Server is not necessary. But in common with all Server-Client Architectures, even GWT needs to contact the Server as they execute, this is carried out by Remote Procedure Calls (RPC). What is RPC?
RPC is a powerful technique for constructing distributed, client-server based applications. It is based on extending the notion of conventional or local procedure, so that the called procedure need not exist in the same address space as the calling procedure. GWT automatically generates most of the classes required for RPC.
Services: Creation and Implementation.
Before getting into RPC, we will have a look at relationships between various classes and interfaces that we are creating for a service. With GWT Framework classes we create certain classes and interfaces for THE RPC Service.
Remote Procedure Call - In Detail Client Creation: To start with, we will create a client side code, by creating an Interface that extends the RemoteService Interface
public interface MyService extends RemoteService { public String myMethod(String s); }
Any implementation of this Service must extend the RemoteServiceServlet and should Implement this Service Interface:
public class MyServiceImpl extends RemoteServiceServlet implements MyService { public String myMethod(String s) { // Do something interesting with 's' here on the server. return s; } }
Call Progress:
Now we need an Asynchronous Interface based on our Service Interface:
interface MyServiceAsync { public void myMethod(String s, AsyncCallback callback); }
If we are using Asynchronous method calls, then we have to have callback object that can serve as a messenger to indicate when the call is completed.
Asynchronous methods are always void, only the callback object is used for communication to the caller. A service Interface and its Asynchronous counterpart have one simple relationship. The Asynchronous method must be in the same package as the Service Interface. If a service interface is called com.example.cal.client.AjaxMattersService, then the asynchronous interface must be called com.example.cal.client.AjaxMattersServiceAsync.
For each method in the Service Interface,
public ReturnType methodName(ParamType1 param1, ParamType2 param2);
the Asynchronous Interface must look something like this
public void methodName(ParamType1 param1, ParamType2 param2, AsyncCallback callback);
In order to respond to client requests, the service ultimately needs to perform some processing. Such server-side processing occurs in the service implementation. A service implementation must extend RemoteServiceServlet and must implement the associated service interface. One important thing that to be noted here is that, the service implementation does not implement the asynchronous version of the service interface. to simplify matters we can say that every service implementation is ultimately a servlet, but rather than extending HttpServlet, it extends RemoteServiceServlet instead.