Search code examples
atmosphereplayframework-2.5

How do you route the path to ManagedService in Atmosphere on Play Framework


I am using the atmosphere-play plugin in Play Framework, together with the basic atmosphere-runtime. See https://github.com/Atmosphere/atmosphere-play

I'm trying to set up a class with the ManagedService annotation, as in the tutorial, but can't figure out how to map the path through the Play router file and haven't been able to find anyone else having done this. The documentation skips this step completely and I just get a 404 error when trying to connect to the server through the atmosphere client script.

Example code:

@ManagedService(path = "/poll")
public class PostPoller {
  ...
}

Client:

var socket = $.atmosphere;
var subSocket;
var transport = 'websocket';

// We are now ready to cut the request
var request = { url: '/poll',
    contentType : "application/json",
    trackMessageLength : true,
    shared : true,
    transport : transport ,
    fallbackTransport: 'long-polling'};

How do I set up the route to the ManagedService in the Play router?


Solution

  • In case anyone else is looking for this, I was able to get it working in Atmosphere-Play 2.3.0 by adding the following to the main Application Controller:

    import static org.atmosphere.play.AtmosphereCoordinator.instance;
    
    public class Application extends Controller {
    
        @Inject
        public Application(ApplicationLifecycle lifecycle) {
    
            // replace PostPoller with the ManagedService class you are using
            instance.discover(PostPoller.class).ready();
    
            lifecycle.addStopHook(() -> {
                instance().shutdown();
                return CompletableFuture.completedFuture(null);
            });
    
            ...
        }
    
        ...
    }
    

    Found the answer at https://github.com/Atmosphere/atmosphere-play/issues/39.