Friday, February 1, 2008

GWT RemoteServiceServlet + Guice = GuiceRemoteServiceServlet

If you use Guice and GWT you'll probably find this handy:
GuiceRemoteServiceServlet is a wrapper servlet for GWT RPC utilizing Guice for managing RemoteService implementation object's lifecycle.


<servlet>
<servlet-name>GWT-RPCServlet</servlet-name>
<servlet-class>com.gc.gwt.server.GuiceRemoteServiceServlet</servlet-class>
<init-param>
<param-name>guice.module</param-name>
<param-value>com.test.guice.MyGuiceModule</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>GWT-RPCServlet</servlet-name>
<url-pattern>/GWT.rpc</url-pattern>
</servlet-mapping>


  1. map all you remote service implementations in Guice Module
  2. define and map GuiceRemoteServiceServlet as shown above
  3. point ALL services to the same /GWT.rpc


The source for the GuiceRemoteServiceServlet can be found here.

Good luck.

PS: GuiceRemoteServiceServlet depends on Guice and apache-commons beanutils.

PS2: The source of GuiceRemoteServiceServlet is distributed under Apache 2.0 License.

6 comments:

NarZo said...

Hi,
The httpServlet(Request|Session) should be injected by Guice, using GuiceFilter which set them (request|session) in a thread local variable.
hih

Karajdaar said...

In which case the servlet becomes simply
public class GuiceRemoteServiceServlet extends RemoteServiceServlet {

@Inject
private Injector injector;

@Override
public String processCall(String payload) throws SerializationException {

try {
RPCRequest rpcRequest = RPC.decodeRequest(payload);

RemoteService service = getServiceInstance(rpcRequest.getMethod()
.getDeclaringClass());

return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
rpcRequest.getParameters(), rpcRequest
.getSerializationPolicy());

} catch (IncompatibleRemoteServiceException ex) {
getServletContext()
.log(
"An IncompatibleRemoteServiceException was thrown while processing this call.",
ex);
return RPC.encodeResponseForFailure(null, ex);
}
}

@SuppressWarnings("unchecked")
private RemoteService getServiceInstance(Class serviceClass) {
try {
RemoteService remoteService = (RemoteService) injector
.getInstance(serviceClass);
return remoteService;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

GrandeXo said...

Hello

How do i call the servlet from the Presenter

And about the RemoteService of the Servlet say
interface MyGuiceService

It does not to be included ?

I can not start the Service
Do you have a complete example or so

regards toine

Pavel Jbanov said...

Please check this post out for a newer version based on Guice 2.0.

http://stuffthathappens.com/blog/2009/09/14/guice-with-gwt/

GrandeXo said...

Hello again


I tried to use it but i get the error
Error 404: SRVE0190E: File not found: /bw_gxt_01/GWT.rpc
protected void configureServlets() {
serve(”/bw_gxt_01/GWT.rpc”).with(GuiceRemoteServiceImpl.class);
System.out.println(”BwServletModel.configureServlets()”);
bind(ListService.class).to(ListServiceImpl.class);
}
@RemoteServiceRelativePath(”GWT.rpc”)
public interface ListService extends RemoteService {
Am i forgetting something
regards toine

Pavel J said...

Try:
serve("/GWT.rpc").with(GuiceRemoteServiceImpl.class);

I'm assuming "/bw_gxt_01" is the context path of your webapp, so you don't need to specify it when calling "serve()".