Skip to main content

GWT - Calling remote procedures

Today's browsers include a special JavaScript object called XMLHttpRequest that allows communication between the browser and server without forcing a page refresh . This special JavaScript object is the basis for making browser-based Remote Procedure Calls(RPCs).

GWT provides two tools that sit on top of the XMLHttpRequest object.  The first is the RequestBuilder class, which is essentially a wrapper around this object, although it's a bit more Java-like in its usage.  The second tool, GWT-RPC, is more elaborate and lets you send and receive real Java objects between the client and server.

RequestBuilder class lets you create a request to be submitted to the server, gives you the ability to fire off the request, and provides access to the results sent back from the server.


String url = "/service/search";
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = rb.sendRequest("term=GWT+in+Action",
new RequestCallback() {
public void onResponseReceived (Request req, Response res) {

// process here
}
public void onError (Request req, Throwable exception) {
// handle error here
}
});
}
catch (RequestException e) {
// handle exception here
}

You can also use JSONObject and populates it with several properties, e.g.

JSONObject obj = new JSONObject();
obj.put("author", new JSONString("Hanson and Tacy"));
obj.put("pages", new JSONNumber(600));
String serializedObj = obj.toString();


The GWT-RPC mechanism lets you send Java objects between the client and server with only a little additional work on both the client and server sides.  First, define a service interface that will be implemented by the server. e.g.
The Server side codes:

public class ServerStatusData implements IsSerializable
{
public String serverName;
}

public interface ServerStatusService extends RemoteService
{
ServerStatusData getStatusData ();
}


Next comes the implementation of the server, as follows,

public class ServerServiceImpl
extends RemoteServiceServlet
implements ServerStatusService
{
public ServerStatusData getStatusData () {

ServerStatusData result = new ServerStatusData();
result.serverName = ....
return result;
}

Third Step:
Registering your service with GWT by adding it to the project configuration file.
<module>
<inherits name="com.google.gwt.user.User'/>
<entry-point class='...ServerStatus'/.
<servlet path="/server-status" class= "....ServerServiceImpl"/>

When your service defined, implemented, and configured on the server, the next step is to address the client-side issues and finally call the service from the client.
Preparation:
GWT does most of the work for you, however you need to create one last interface. The GWT compiler uses this interface when it generates the service proxy object. A proxy object is an object instance that forwards the requests to another target. In this case, you'll call a local method, and the proxy object is responsible for serializing the parameters, calling the remote  service, and handling the deserializatin of the return value. You don't write the code fro the proxy class; the GWT compiler handles this for you. In the client-side code, you create the proxy object by writing the following:
GWT.create(ServerStatusService.class);

Here you call the static method create() of the com.google.gwt.core.client.GWT class, passing it the class object of the remote service interface.
The proxy object returned implements two interfaces: one that you need to create(ServerStatusServiceAsync), and one supplied by GWT(ServiceDefTarget)

import com.google.gwt.user.client.rpc.AsyncCallback;
public interface ServerStatusServiceAsync {
void getStatusData (AsyncCallback callback);
}

Calling the remote server service:
step 1: creating the proxy object

ServerStatusServiceAsync serviceProxy =
(ServerStatusServiceAsync) GWT.create(ServerStatusService.class);



Step 2: casting the proxy object to ServiceDefTarget:

ServiceDefTarget target = (ServiceDefTarget) serviceProxy;
target.setServiceEntryPoint(GWT.getModuleBaseURL()
+ "server-status");

Or using annotation:
Step 1:

/**
 * The client side stub for the RPC service.
 */
@RemoteServiceRelativePath("server-status")

public interface ServerStatusService extends RemoteService
{
ServerStatusData getStatusData ();
}

RemoteServiceRelativePath will associates a RemoteService with a relative path. This annotation will cause the client-side proxy to automatically invoke the ServiceDefTarget.setServiceEntryPoint(String) method with GWT.getModuleBaseURL() + value() as its argument. Subsequent calls to ServiceDefTarget.setServiceEntryPoint(String) will override this default path.



Step 2:

/**
 * Create a remote service proxy to talk to the server-side Greeting service.
 */
private final ServerStatusServiceAsync serviceProxy = GWT
.create(ServerStatusService.class);




Step 3: Create a callback object

AsyncCallback callback = new AsyncCallback() {
public void onFailure (Throwable caught) {
GWT.log("RPC error", caught);
}
public void onSuccess (Object result) {
GWT.log("RPC success", null);
}
};

Step 4: make the remote service call
serviceProxy.getStatusData(callback);

the RemoteServiceServlet is where all the magic happens. This servlet receives the data from the server, which we already mentioned must be text due to the way the underlying XMLHttpRequest object works, the deserializes the text data into Java objects. On the return trip, the RemoteServiceServlet serializes the return value into text, which can then be sent back to the browser.




Comments

Popular posts from this blog

Quicksort implementation by using Java

 source: http://www.algolist.net/Algorithms/Sorting/Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: 1st: Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value(e.g. some people would like to pick the first element and do the exchange in the end) 2nd: Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Apply quicksort algorithm recursively to the left and the right parts - the previous pivot element excluded! Partition algorithm in detail: There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal

Live - solving the jasper report out of memory and high cpu usage problems

I still can not find the solution. So I summary all the things and tell my boss about it. If any one knows the solution, please let me know. Symptom: 1.        The JVM became Out of memory when creating big consumption report 2.        Those JRTemplateElement-instances is still there occupied even if I logged out the system Reason:         1. There is a large number of JRTemplateElement-instances cached in the memory 2.     The clearobjects() method in ReportThread class has not been triggered when logging out Action I tried:      About the Virtualizer: 1.     Replacing the JRSwapFileVirtualizer with JRFileVirtualizer 2.     Not use any FileVirtualizer for cache the report in the hard disk Result: The japserreport still creating the a large number of JRTemplateElement-instances in the memory        About the work around below,      I tried: item 3(in below work around list) – result: it helps to reduce  the size of the JRTemplateElement Object        

Stretch a row if data overflows in jasper reports

It is very common that some columns of the report need to stretch to show all the content in that column. But  if you just specify the property " stretch with overflow' to that column(we called text field in jasper report world) , it will just stretch that column and won't change other columns, so the row could be ridiculous. Haven't find the solution from internet yet. So I just review the properties in iReport one by one and find two useful properties(the bold  highlighted in example below) which resolve the problems.   example: <band height="20" splitType="Stretch" > <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true"> <reportElement stretchType="RelativeToTallestObject" mode="Opaque" x="192" y="0" width="183" height="20"/> <box leftPadding="2"> <pen lineWidth="0.25"/>