views:

645

answers:

2

Hey guys,

I am trying to develop a Spring Roo/GWT app with the newest integration of GWT in Roo.

Getting the scaffolding to work is very straightforward, but I don't really understand how the RPC works there.

Can someone of you provide a simple example how to do a simple service to connect client/server within Spring Roo and GWT.

Would be very helpful for a start, as I couldn't find any resource on that.

thx & regards, Flo

A: 

The functionality you are referring to is currently under heavy development (or so the guys at Google want us to believe ;)) so the API and internal workings are not final and will most likely still change before the final release of GWT 2.1 (this was stated a few times during the GWT sessions during Google IO 2010). However, you can browse the Bikeshed sample in the trunk to see a working (hopefully ;)) example. There's also the 2.1 branch that appears to contain the up-to-date (?) sample (and the cookbook that was promised on Google IO).
Personally, I'd wait with switching your code to the new RPC model till the guys working on GWT say it's safe to do so ;) (but it's definitely a good idea to get accustomed with the general idea now - it's not like they will change everything :D).

Igor Klimer
+3  A: 

Flo,

Not sure if you are up on google wave at all, but that does seem to be one place to keep apace of the current effort. Specifically this wave is available to the public: RequestFactory Wave

It covers the details (well emerging details) about the RequestFactory API.

The basic idea is that your domain model objects are needed on the server side and the client side. Using hibernate can cause issues with the class files and people have wound up having two sets of model objects, and using custom GWT-RPC to make server requests and marshall/un-marshall between the client- and server-side model objects. Not an ideal solution. Even if you can use the same model objects, the overhead of the RPC is a drag.

Enter RequestFactory and we see that google engineers are probably getting paid what they are worth. Take a look at the sample code generated from the .roo - specifically ApplicationRequestFactory.java.

package com.springsource.extrack.gwt.request;

import com.google.gwt.requestfactory.shared.RequestFactory;

public interface ApplicationRequestFactory extends RequestFactory {
    ReportRequest reportRequest();
    ExpenseRequest expenseRequest();
    EmployeeRequest employeeRequest();
}

This is an interface that provides request methods for each of the domain objects. There is no implementation of this class defined in the project. It is instantiated in the EntryPoint with a call to GWT.create(...):

final ApplicationRequestFactory requestFactory = 
    GWT.create(ApplicationRequestFactory.class);
requestFactory.init(eventBus);

Within the com.springsource.extrack.gwt.request package you will see an ApplicationEntityTypesProcessor.java which is cleverly using generics to package the references to the domain classes for use later in the presentation. The rest of that package though are events and handlers for each model object.

Specifically there are four auto-generated classes for each object:

  • EmployeeRecord.java - This is a DTO for the domain object.
  • EmployeeRecordChanged.java - This is a RecordChanged event to provide a hook method onEmployeeChanged.
  • EmployeeChangedHandler.java - This is an interface that will be implemented when specific behaviour for the onEmployeeChanged is needed.
  • EmployeeRequest.java - This is an interface used by the ApplicationRequestFactory to package up the various access methods for a given object.

Keep in mind there is a lot of code generated behind the scenes to support all this. And from M1 to M2 a lot has been cleaned out of what is visible in a GWT project. I'd expect there to be more changes, but not as drastic as M1 to M2 was.

So finally these events can be used as in the UI package to tie together the domain and the UI. ReportListActivity.java:

public void start(Display display) {
    this.registration = eventBus.addHandler(ReportRecordChanged.TYPE, new ReportChangedHandler() {
        public void onReportChanged(ReportRecordChanged event) {
            update(event.getWriteOperation(), event.getRecord());
        }
    });
    super.start(display);
}

Again I refer you to the wave for more info. Plus the expenses.roo demonstrates how to use Places and has a rather slick Activity framework as well. Happy GWTing.

Regards.

Jeff