Friday, July 3, 2009

GWT Tutorial - Building a GWT RPC Service

Introduction
If you haven’t already please read our previous tutorials: Introduction to GWT, and Anatomy of a GWT Project.
As you already know, GWT is a Java framework that allows you to easily develop AJAX (Asynchronous JavaScript and XML) based web applications. The difference between GWT (AJAX based web) applications and traditional (non AJAX based) web applications is that GWT applications do not need to fetch new HTML pages from the server when they want to make User Interface (UI) changes. A GWT application behaves like a regular client/server application that happens to be running in a browser. The GWT application can fetch data from the server as it executes and update only the specific parts of the UI that need to be updated.
To learn more about GWT, I recommend reading these good books - GWT Solutions, GWT Applications, AJAX Security.
What is GWT RPC?
One of the most important pieces of the GWT framework is the GWT Remote Procedure Call (RPC) mechanism. This RPC mechanism makes it easy for a GWT application client to make a call to server-side code. GWT RPC makes it simple to get data between the client and the server.
What is a GWT service?
The server-side code that gets called from the client is referred to as a service (not to be confused with SOAP web-services). During a service call, the GWT RPC makes it easy for client and server to pass Java objects back and forth over HTTP.
GWT services can be used to accomplish tasks that are part of your application, but cannot be done on the client computer. Proper use of GWT services can allow you to develop an application where all the UI logic resides on the client, resulting in an application with greatly improved performance, reduced bandwidth, and reduced web server load. This will also result in a user experience that is similar to using a desktop application, and is far more compelling than the experience of visiting a simple web page.
GWT’s RPC mechanism hides all the plumbing necessary to create and consume services. All the proxy classes that handle RPC plumbing for making the server-side code invocation and for converting data back and forth between the client and server are all generated automagically. All you need to do is define your service’s interface and its server-side implementations.
Asynchronous – the A in AJAX
All network operations in GWT are asynchronous, or non-blocking, i.e. they return immediately after they are called, and require the user to use a callback method to handle the results when they are eventually returned from the server.
The reason for using asynchronous operation is that most browsers’ JavaScript engines are single-threaded. A blocking on a call to XMLHTTPRequest (a JavaScript Object defined by browsers to enable a web page to send HTTP requests) in a browser based application, would block the UI thread until a response was received from the server. This would make it appear as though the browser was frozen for the duration of the connection to the server. Some browsers provide a way around this, but there is no clean, universal solution. As a result, GWT does not implement any synchronous network connections.
Sending data back and forth between client and server
Because GWT RPC calls are between JavaScript (on the client) and Java code (on the browser), GWT provides object serialization as part of its RPC mechanism to help bridge the language divide. Please note that GWT serialization is not the same as Java serialization. You need to ensure that any object that needs to be sent between the client and server is of a GWT serializable type. A type is serializable and can be used in a service interface if the type:
• is primitive, such as char, byte, short, int, long, boolean, float, or double;
• is String, Date, or a primitive wrapper such as Character, Byte, Short, Integer, Long, Boolean, Float, or Double;
• is an array of serializable types (including other serializable arrays);
• is a serializable user-defined class; or
• has at least one serializable subclass
This is only a brief overview of GWT serialization. To learn more about the details of GWT object serialization, and how to create complex serializable objects, check out this tutorial.

No comments: