Knowledge Seeking

GWT – Asynchronous RPC January 14, 2011

Filed under: GWT,Java — nicozan @ 1:28 pm

Most semi-complex WebApps have a back-end server. GWT give us the possibility to interact with the server-side using RPC (Remote Procedures Call). I’ll explain how it works through an example:

On the client-side, we have 2 Interfaces that will be the key to our communication with the server-side.

package com.wordpress.nicozan.client;
public interface BankClient extends RemoteService {
  public String askForLoan(String s);

package com.wordpress.nicozan.client;
public interface BankClientAsync {
   public void askForLoan(String s, AsyncCallback<String> callback);

The nature of asynchronous methods 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.

On the server side we just have 1 class, that implements the methods of the BackClient interface.

package com.wordpress.nicozan.server;
import com.wordpress.nicozan.client.BankClient;
public class Bank extends RemoteServiceServlet implements
    BankClient {
  	public String askForLoan(String s) {
			if (Integer.parseInt(s) <= 1000){
				return new String("Loan Granted");	
		 		return new String("Loan Denied");


It is very important to take into consideration the suffix Async and argument referencing the AsyncCallback. The relationship between service interface and its asynchronous counterpart must follow certain naming standards. If the naming standards aren’t applied the GWT compiler won’t generate the proper code to implement RPC.
The service interface and the asynchronous interface must be in the same package. Each method in the asynchronous service interface must have a corresponding method in the asynchronous service interface with an extra AsyncCallback parameter as the last argument.

Up to now we have defined all the necessary elements, but how should we use them?. Usually in common WebApps we have to define and map our servlet in the web.xml file:


Please remember that one servlet can be mapped in more than one URL, no the other way around. GWT needs an other definition of the servlet in the Module Configuration File,”Application.gwt.xml” :

<servlet path="/bankServlet" class="com.wordpress.nicozan.server.Bank"/> 

Nothing further to say, thanks for reading it. I hope it helps.. 😀


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s