I'd recommend this pattern instead:

public interface GreetingServiceAsync
{
  public static class Wrapper {
     public static final GreetingServiceAsync RPC = GWT.create
    (GreetingService.class);
  }

   void greetServer(String input, AsyncCallback<String> callback);
}

That way you don't pay for the cost of actually binding any of the services
until you use them (which is about 3 seconds per service (at least on my
really fast machine) in hosted mode - I believe this kind of stuff is free
when compiled).

Saw this pattern on a Google Talk by a Googler from last year.  Essentially
the Java classloader won't instantiate the RPC singleton until the wrapper
class is referenced for the first time.

Also, by doing something like:

if (!GWT.isScript()) {
   new Timer() {
       public void run() {
           GreetingServiceAsync service = GreetingServiceAsync.Wrapper.RPC;
       }
   }.schedule(Random.nextInt(500) + 1);
}

somewhere around the place where you are initializing the code that will use
the service, you'll bind the service in the background while the user isn't
doing anything & you shouldn't actually notice the cost of doing that call.

Also, I'm not sure if the GWT.create function is actually expensive when
running the Javascript (my suspicion is no) but if it is, then you can
remove the GWT.isScript() check.

On Sat, Apr 11, 2009 at 4:12 AM, Dean S. Jones <deansjo...@gmail.com> wrote:

>
> new in 1.6 is the @RemoteServiceRelativePath("...") Annotation. This
> makes it somewhat easier to do "Async Singletons"
>
> typically, to get a remote interface in your app, as in the example,
> you would do:
>
> GreetingServiceAsync greetingService = GWT.create
> (GreetingService.class);
>
> then use greetingService locally. If you need the RPC interface in
> more than one place, you would have to pass around the reference,
> store it globally, or worse, end up rebinding it....
>
> far easier is to bind it IN the AsyncInterface as a static. It is
> bound once, it is globally available, it's the same type, but also can
> be passed as a reference. The idiom is simple:
>
> public interface GreetingServiceAsync
> {
>    public static GreetingServiceAsync RPC = GWT.create
> (GreetingService.class);
>
>    void greetServer(String input, AsyncCallback<String> callback);
> }
>
> then you can use it simply, safely, ... anywhere in your client code:
>
> GreetingServiceAsync.RPC.greetServer("stuff", new AsyncCallback<String>
> ()
> {
> }
>
> This can also be done in 1.4/1.5 with just a little more code....
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to