Rauf has posted 29 posts at DZone. You can read more from them at their website. View Full User Profile

GWT UniversalClient: "Look Mom, No RPC!"

10.11.2008
| 11028 views |
  • submit to reddit

How to develop GWT applications without RPC code using the UniversalClient API.

GWT has enabled a more OO style of programming again within the web application world (yeah, there are some that will say that is bad for the web). For fans of Swing-type component frameworks and other similar GUI frameworks, GWT is a great welcome.

But with this style of programming, you go back to separating the client from the server more clearly. This is both good and bad. The good is that you can do a lot more things with the client now (AJAX stuff) but the bad is that you have a demarcation line between where the client code ends and server-side code starts, which requires developers to write and stub out RPC classes and interfaces. With server-side web progamming, everything is sort of mixed together so developers don't worry about where the resource is, because most of the time the code is running on the server to begin work.

Anyway, with GWT you have to write RPC code. Not the hardest thing in the world to do, but it can be a bit of a pain. Well, here comes the SOAFaces framework to the rescue with its UnverisalClient. The UniversalClient is a bit like the MuleClient you find in the Mule ESB project. It can talk to just about any server-side method or service and pass and return arguments when invoking said services. The UniveralClient allows GWT developers to write their client code and call out to remote services with much less hassle. The UniversalClient interface can invoke just about any standard Java POJO method or Mule/ESB service and return the results without writing a line of GWT RPC code. If the ESB web service already exists, then just call it. If you want to create your own, simply attach an annotation to any service-side POJO and now it is available to be called from your GWT client (again, no RPC or marshelling to deal with). And it is your choice whether to pass and return JOSN or POJO objects back and forth.

Here is a code example of what a simple GWT client would look like using the UniversalClient API to invoke a very simple service on the server side. This is, of course, a simple example, but you can also use the UniversalClient to invoke Mule endpoints as well.

public class UCDemo implements EntryPoint {

private static final String UNIVERSAL_CLIENT_SERVLET = "/SOAFacesRPCServlet";
private UniversalClient _oClient;

private Button button;

/**
* Simple entry point and use call to UniversalClient.
*/
public void onModuleLoad() {
button = new Button("Click To Execute Service Call");

DockPanel dockPanel = new DockPanel();
dockPanel.add(button, DockPanel.WEST);
RootPanel.get().add(dockPanel);

button.addClickListener(new ClickListener() {
public void onClick(Widget btn) {

//Format endpoint is: soafaces:///
String stEndpoint1 = "soafaces://com.dupont.crd.gwt.service.HelloService/helloThereEndpoint";
if (btn.equals(button)) {

//Create the UniversalClient Async Callback
UniversalClientPOJOCallback oMulePOJOCallback = new UniversalClientPOJOCallback() {

public void onUniversalClientSuccess(Object result) {
//Serivces returns a String
String returnVal = (String) result;
Window.alert("Wow this was a success! Return value: " + returnVal);
}

public void onFailure(Throwable ex) {
Window.alert("Error during UniversalClient call: " + ex.getMessage());
}
};

//Using send method to call remote service
getUniversalClient().send(stEndpoint1, oMuleJSONCallback);
}
}
});
}

private UniversalClient getUniversalClient() {
if(_oClient == null) {
_oClient = UniversalClientFactory.getInstance().getUniversalClient(UNIVERSAL_CLIENT_SERVLET);
}
return _oClient;
}
}

Here is what the server-side code looks like. This is the service endpoint referenced by the client above. As you can see, very simple. Just a POJO with an annotation marker. This allows the UnviversalClient to invoke this service and return the results.

public class HelloWorld {

@ServiceEndpoint(name="helloThereEndpoint")
public String helloThere() {
return "You got it";
}
}

Some Resources

 

 

Published at DZone with permission of its author, Rauf Issa.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Robbie Cheng replied on Mon, 2008/10/13 - 8:00am

ZK allows you to access server-side resources directly. No more RPC call, neither annotation marker. In the following code snippet, you UI widgets can access DAO with ease.  Explore the Live Demo.

<window title="Hibernate" border="normal">
<zscript>{
void submit() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

User aUser = new User();

//assign what end user entered into the persistence object.
aUser.setName(name.value);
aUser.setEmail(email.value);

session.save(aUser);

tx.commit();
HibernateUtil.closeSession();
}
}</zscript>
<grid>
<rows>
<row>Name : <textbox id="name"/></row>
<row>Email: <textbox id="email"/></row>
<row><button label="submit" onClick="submit()"/></row>
</rows>
</grid>
</window>

 

Rauf Issa replied on Mon, 2008/10/13 - 8:11am

Not sure what your point is? And what does ZK have anything to do with this topic? If you want to write apps with page orietned HTML tags, by all means give ZK a try ;)

Keep in mind the UniversalClient annotation marker is also one level security ACL. You do not want the client accessing any POJO method/service on the server. This is "directly" being accessed as well from GWT.

Robbie Cheng replied on Mon, 2008/10/13 - 8:33am

ZK is a server-centric Ajax framework. Not for page-based application, please. With ZK, developers no longer ave to write client codes, and dealing with the communication between client and server. Web application can accees any kind of resources direcly. Personally, I prefer ZK's programming model which is more straingforward to me.

Although GWT keeps developers away from JavaScript, but the programming model is still prmitative. Web application are seperated into two parts, client codes, and server codes. That's why you need RPC call or UniversalClient to access resources at server.

Software Nuts replied on Mon, 2008/10/13 - 2:12pm

come on guy is trying sell ZK where people like GWT.

GWT has it own plus where you want to put animation or do things on client side instead of doing everything on server. Bruce Johnson has put demo why GWT and people who don't know should look into it?

ZK, Appcelerator or Flex all has it's own plus so keep the discussion on topic.

Fred Barney replied on Mon, 2008/10/13 - 11:53pm

What is the matter with these ZK losers?  It's unbelievably obnoxious.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.