Generate Async Interfaces

Features

  • Can add service to an existing app
  • Generates code inside folder
/target/generated-sources

One-time Prep for a project (POM)

  1. Make sure the pom.xml is prepared by adding the following to the appropriate section of the gwt-maven-plugin configuration:
         <execution>
            <phase>process-classes</phase>
            <configuration>
              <output>${gwt.output.directory}</output>
              <logLevel>INFO</logLevel>
              <servicePattern>**/service/*.java</servicePattern>
            </configuration>
            <goals>
              <goal>compile</goal>
              <goal>generateAsync</goal>
            </goals>
          </execution>

Steps for adding a Remote Service

  1. Create the interface under the <project>.client.service directory.
    1. From the <project>.client.service directory: File -> New -> Interface
    2. Extend RemoteService
  2. Here's a short single method example. To the newly created interface, add the following:
    1. Define the method calls.
    2. Add the @RemoteServiceRelativePath("<yourContext>") call for an appropriate context path.
package com.jettmarks.routes.listTags.client.service;
 
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.jettmarks.routes.listTags.client.bean.User;
 
/**
 * @author jett
 *
 */
@RemoteServiceRelativePath("getUser")
public interface GetUser extends RemoteService
{
  public User getUser(String token);
}
  1. Create an Eclipse Launch configuration (Run Configuration) using the Maven Build and choosing the target:
    generateAsync.png
  2. When this is executed, the output will be placed under the <project>/target/generated-sources directory. Move them side by side with the original interface within the project/service directory.
  3. The Impl class will still need to be created using Eclipse. It extends RemoteServiceServlet and implements the manually written interface, not the Async one generated by the gwt-maven-plugin.
  4. To call the Remote Service, the client executes this sort of code:
     GetUserAsync service = GetUserAsync.Util.getInstance();
     AsyncCallback<User> callback = new GetUserAsyncCallback();
 
     service.getUser(token, callback);
  1. This will require writing an inner class named GetUserAsyncCallback (or defining it inline).
  2. The Remote Services will need to be defined in the Application.gwt.xml file:
    <!-- Remote Services -->
    <servlet class="com.jettmarks.gmaps.server.RSSProxyImpl" path="/proxy"/>      
    <servlet class="com.jettmarks.gmaps.server.RouteSaveImpl" path="/saveRoute"/>      
    <servlet class="com.jettmarks.gmaps.server.ReadRouteNamesImpl" path="/readRouteNames"/>      
    <servlet class="com.jettmarks.gmaps.server.GetRouteImpl" path="/getRoute"/>
  1. I had thought that the Application.gwt.xml would update the web.xml, but I needed to put my new servlet into the web.xml as well.

Adding Signature to existing service

This applies when you already have a defined service that has been working and you're adding a new method call to that service. An example was generated 10/21/10 as part of VISCAT-89 and release 1.6.0 of ShowRoutes.

  1. Open the .service interface definition and add the signature.
  2. The compiler will automatically tell you that the Async interface is missing the matching call.
  3. Select the quick fix that will generate the new method call in the Async interface file.
  4. Proceed with implementation on the .server side.

Reference

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License