Atmosphere and Dropwizard 0.7

1,820 views
Skip to first unread message

Tomasz Kubacki

unread,
Apr 4, 2014, 4:38:54 AM4/4/14
to dropwiz...@googlegroups.com
Hi,

Anyone got working example of
Dropwizard 0.7 + Atmosphere ?

(I'm only interested in websockets connections)

Cheers,
t.k



Olve Hansen

unread,
Apr 10, 2014, 3:37:22 AM4/10/14
to dropwiz...@googlegroups.com
I am trying to get this running as well- I have a working Atmoshpere servlet, but it creates a separate Jersey instance. Main problem is now that all DW Jersey providers (Guava - Jackson modules etc) are not added to the Atmosphere servlet.

Some interesting points came up here. Tried the ReflectorServletProcessor approach but didn't work. Same error as last post.
"The Atmosphere Framework is not installed properly and unexpected result may occurs."

I will investigate more today.

-- 
Olve

Olve Hansen

unread,
Apr 11, 2014, 3:22:37 AM4/11/14
to dropwiz...@googlegroups.com
I have basically given up the atmosphere integration.

The pros of using the Atmosphere jersey integration would be the dropwizard way of handling resources, metrics etc. But using atmosphere it sets up its own Jersey instance as well as polluting the existing one with AtmosphereFilter via META-INF/services/jersey-server-components.


When dropwizard uses Jetty 9.1 the javax.websocket standard can also be used. 

Regards, 
Olve

ThePodux

unread,
Apr 11, 2014, 5:51:43 AM4/11/14
to dropwiz...@googlegroups.com
I have the same prob! With 0.6.2 my Atmosphere websocket works very well but not with DW 0.7.0 and jetty websocket-server / jetty-server 9.0.7.

        Dynamic addFilter = environment.servlets().addFilter("/chat", CrossOriginFilter.class);
        addFilter.setInitParameter("*", CrossOriginFilter.ALLOWED_ORIGINS_PARAM);

        AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
        atmosphereServlet.framework().addInitParameter("com.sun.jersey.config.property.packages", "com.vicust.socket");
        environment.servlets().addServlet("/chat/*", atmosphereServlet);
        environment.jersey().packages("com.vicust.resources");
        environment.healthChecks().register("TestHealthCheck", new TestHealth());

The resource uses the @ManagedService annotation.

The Service starts without errors but the client websocket user can't connect  and DW logs:

127.0.0.1 -  -  [11/Apr/2014:09:45:23 +0000] "GET /chat HTTP/1.1" 404 293 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36" 26 (I removed the debug rows)

Nick Telford

unread,
Apr 11, 2014, 6:04:42 AM4/11/14
to dropwiz...@googlegroups.com
I don't know much about Atmosphere, so I may be missing something, but Jetty has pretty solid WebSocket support.

For the API available in Dropwizard 0.7.0, see the Jetty 9 docs: http://www.eclipse.org/jetty/documentation/9.0.6.v20130930/websockets.html

As for integrating Atmosphere; I think you'll always have trouble trying to integrate two different application frameworks. Each one usually assumes that it owns the lifecycle of the application, making integrating it with anything else tricky at best.

Olve Hansen

unread,
Apr 11, 2014, 7:06:04 AM4/11/14
to dropwiz...@googlegroups.com
I ended up doing it the jetty-way as Nick links to. Be aware that the docs only lists how to wokr an existing websocket connection, not the upgrade part. For that you need to have a WebSocketServlet that basically has one line of active code (minimum):
public class MyWebSocketServlet extends WebSocketServlet{
 @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(MyWebSocketEndpoint.class);
    }
}

But from here on just follow the docs:
@WebSocket
public class MyWebSocketEndpoint {

        @OnWebSocketMessage
        public void onMessage(Session session, String s) throws IOException {
            session.getRemote().sendString("Returned; "+s);
        }

}
I found Atmosphere to be cumbersome to understand, but it gives you a great deal of good tools to use when broadcasting and handling non-websocket fallbacks etc.
Looking forward to Jetty 9.1.x for javax.websockets!
O.

Olve Hansen

unread,
Apr 11, 2014, 7:11:10 AM4/11/14
to dropwiz...@googlegroups.com
I managed to get this running on 0.7, but the problem was that the DW object mapper and other goodness in jersey from DW was not set. 
I think this is about what made it tick:
                AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
                final ServletRegistration.Dynamic websocket = environment.servlets().addServlet("websocket", atmosphereServlet);
                websocket.setAsyncSupported(true);
                websocket.addMapping("/websocket/*");
                websocket.setInitParameters(ImmutableMap.<String, String>of(
                        "com.sun.jersey.config.property.packages", "com.vimond.event.resources.websocket, io.dropwizard.jersey",
                        "org.atmosphere.websocket.messageContentType", "application/json"

                ));

ThePodux

unread,
Apr 11, 2014, 7:36:53 AM4/11/14
to dropwiz...@googlegroups.com
Yeah now it works! ! Thank you very much !

ThePodux

unread,
Apr 14, 2014, 7:24:38 AM4/14/14
to dropwiz...@googlegroups.com
I published my example project on github 

Olve Hansen

unread,
Apr 30, 2014, 5:34:25 AM4/30/14
to dropwiz...@googlegroups.com
Yes- this works, kind of. Problem is that you now have two jersey-environments in your process. One for DW things, and one for Atmosphere. And the manifest-files supplied with Atmosphere now adds filters to your existing DW jersey configuration. I don't think they do too much harm, and it might be possible to remove the filters, but I found it to be too much hassle in the end, and went for standar Jetty websocket, a low-level approach.

Yun Zhi Lin

unread,
Apr 20, 2015, 10:11:36 AM4/20/15
to dropwiz...@googlegroups.com
To overcome the issue of 2 jersey-environments, has anyone tried to replace the DW ServletContainer with an extension of AtmosphereServlet or AtmosphereFilter?

I.e. something like this:

environment.jersey().replace(new Function<ResourceConfig, Servlet>() {
@Nullable
@Override
public ServletContainer apply(ResourceConfig resourceConfig) {
return new AtmosphereFilterContainer(resourceConfig);
}
});

It's something I'm attempting at the moment but isn't trivial either.

Olve Hansen

unread,
Apr 29, 2015, 9:24:59 AM4/29/15
to dropwiz...@googlegroups.com
I think I looked into that solution, but not sure why I didn't do that. This was also pre 0.8, so maybe somethings have changed since then.

Yun Zhi Lin

unread,
May 4, 2015, 10:21:44 PM5/4/15
to dropwiz...@googlegroups.com
@Olve my approach doesn't work with 0.8 either. The AtmosphereServlet appears to need to create its own independent ServletContainer and Filters. By forcing it inside the Dropwizard Jersey environment all sorts of issues came up. I really wanted to use Atmosphere due to their support for clustered Broadcast/Redis cache out of box. But it was just too hard.

I did managed to get the Native Jetty WebSocket implementation to work based on your comments and this example(in Japanese): http://qiita.com/kivatek/items/1a1f6ac3d0808fd137b6 

I also tried the Jetty JSR356 implementation following this https://github.com/jetty-project/embedded-jetty-websocket-examples but in the end I settled for using https://pusher.com/ to avoid implementing my own broadcast cache.
Reply all
Reply to author
Forward
0 new messages