Skip to Content »

Tech Life of Recht » Calling GWT RemoteServices from native Java

 Calling GWT RemoteServices from native Java

  • September 15th, 2006
  • 1:27 pm

I recently had to implement an anonymous chat client using a Jabber backend in GWT. No problem, I just implemented a RemoteServiceServlet with the necessary methods, and then a GWT client which polls the service for data.
Next, I got a request about adding the same functionality to an Eclipse RCP based application. I now had the choice of either implementing the service again using REST, WebServices, or something entirely different, but it would be much easier if I just called the GWT service directly, which leads me to the real purpose of this post: Using a proxy pattern, I've created the necessary mechanisms to make this posssible.

It works like this:
You create a service implementation. Copy any interfaces, exception classes, and shared classes to the client project, and add gwt-java.jar to the classpath. Access the remote service like this:

CODE:
  1. import dk.contix.gwt.java.GWTJava;
  2. import dk.contix.gwt.java.example.ExampleService;
  3.  
  4. public class Test {
  5.   public void callService() {
  6.     ExampleService service = (ExampleService) GWTJava.create(
  7.         ExampleService.class,
  8.         "http://localhost:8080/example/exampleService");
  9.     service.voidTest();
  10.   }
  11.  
  12. }

Of course, you are bound by the same restrictions as when using the service from Javascript: Only classes which implement IsSerializable can be serialized, they must have empty constructors, and so on. But besides that, this is more or less what you need to do.

Want your say?

* Required fields. Your e-mail address will not be published on this site

You can use the following XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>