Guice BlazeDS’ MessageBroker
Tuesday, October 20th, 2009 - 12:10 pm - Guice
In a previous post (http://www.connorgarvey.com/blog/?p=132), I wrote about how to use Guice injection for Flex services. I used web.xml to configure the MessageBrokerServlet and configured each Flex service to use Guice as a factory. Since then, on this project, we’ve had to introduce new servlets. Rather than continue to use web.xml’s verbose and fully-qualified-path based configuration, we moved to using Guice’s ServletModule class. Here are the steps we followed.
- Ensure the guice-servlet.jar is included in your project and is deployed with your build.
- Add the Guice filter to web.xml.
<filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- Create a servlet module, a class extending com.google.inject.servlet.ServletModule.
- Override the configureServlets() method of ServletModule and add the message broker servlet configuration.
this.bind(MessageBrokerServlet.class).in(Scopes.SINGLETON); final Map<String, String> params = new TreeMap<String, String>(); params.put("services.configuration.file", this.context .getRealPath("WEB-INF/config/flex/services-config.xml")); this.serve("/messagebroker/*").with(MessageBrokerServlet.class, params);
- Normally, servlets configured in Guice are tagged with @Singleton. Since the MessageBrokerServlet is third party, it’s marked as a singleton here, in the module.
- Add the new module to the Guice servlet context listener, which should already be configured in web.xml.
- Remove the servlet and servlet-mapping from web.xml.
An intriguing discussion is worth comment. I do believe that you need
to publish more about this topic, it may not be a taboo subject but typically folks don’t speak about these issues. To the next! Cheers!!
I guess in this second scenario, I still need the “Guice factory” from the previous post.
Thanks for your explanations !
Hi Pierre,
The beginning of the post contains a link to the previous one. I guess it wasn’t very obvious. Here it is again.
http://www.connorgarvey.com/blog/?p=132
[…] « Testing in Android with mock UI components Guice BlazeDS’ MessageBroker » adobe, blazeds, google, Guice, injection, Java, Redpoint, […]