Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Google Guice with Atmosphere

DZone's Guide to

Using Google Guice with Atmosphere

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Starting with Atmosphere 0.5, you can now use Google Guice to configure Atmosphere.

Google Guice
support is enabled by referencing the Guice filter GuiceFilter and an application specific ServletContextListener that extends from GuiceServletContextListener in the web.xml. For example, the web.xml may be as follows:

   <web-app>
     <listener>
       <listener-class>org.atmosphere.samples.<b style="color: black; background-color: rgb(160, 255, 255);">guice</b>.GuiceChatModule</listener-class>
     </listener>
     <filter>

       <filter-name><b style="color: black; background-color: rgb(160, 255, 255);">Guice</b> Filter</filter-name>
       <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
     </filter>
     <filter-mapping>
       <filter-name><b style="color: black; background-color: rgb(160, 255, 255);">Guice</b> Filter</filter-name>

       <url-pattern>/*</url-pattern>
     </filter-mapping>
   </web-app>

and the application specific servlet context listener may be as follows:

public class GuiceChatConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return <b style="color: black; background-color: rgb(160, 255, 255);">Guice</b>.createInjector(new GuiceChatModule());
    }
} 

with:

public class GuiceChatModule  extends ServletModule{

    @Override
    protected void configureServlets(){
        Map params = new HashMap();
        params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "org.atmosphere.samples.<b style="color: black; background-color: rgb(160, 255, 255);">guice</b>");
        serve("/chat*").with(AtmosphereGuiceServlet.class, params);
    }
} 


That's it. You can download the previously Atmosphere-Jersey sample, this time Guice enabled from here. For any questions or to download Atmosphere, go to our main site and use our Nabble forum (no subscription needed) or follow us on Twitter and tweet your questions there!

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}