Combine Jersey with Tyrus
Asked Answered
B

1

6

I have a Jersey JAX-RS application which runs on a Grizzly instance:

public class Application {
    public static final String BASE_URI = "http://127.0.0.1:8080/rest";

    public static void main(String[] args) throws IOException {
        ResourceConfig rc = new ResourceConfig().packages("my.package.rest");
        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
        System.out.println("Server started, press any key to stop.");
        System.in.read();
        server.shutdownNow();
    }
}

inside my.package.rest there are JAX-RS annotated resources. I'd like to add websocket annotated (@ServerEndpoint, @OnOpen, @OnMessage, etc.) resources using Tyrus on the same server instance (say in "http://127.0.0.1:8080/websocket") but all the documentation I was able to find shows how to start a standalone websocket server using Tyrus + a Grizzly container, not combined with Jersey. I'm looking for something like:

server.getServerConfiguration().addHttpHandler(new SomeTyrusHttpHandler("/websocket"));

but I can't find anything similar to SomeTyrusHttpHandler. How can I combine Jersey and Tyrus in a single Grizzly server?

Bolter answered 20/4, 2015 at 14:46 Comment(0)
K
1

good question, but there is currently no good answer for this one. You still can do that, but it would require to deep dive into Grizzly internals; Tyrus is registered by an addon (see WebSocketAddOn) and it can be combined with Jersey way of registering into that container.

Please have in mind that integrating these two frameworks together is not trivial challenge - running them in a single container is a first step, but there are other things which need to be taken care of, like "instance provider", lifecycle adjustment, etc. I believe this topic exceeds the scope of one SO answer - you can expect blogpost from me or some other Tyrus/Jersey team member about this. (I'll post it here once that is done).

Anyway, I would recommend to take more conservative approach and use some lightweight container which supports Servlet 3.1; that should require far less work on your side and you'll have standard Servlet runtime / lifecycle.

Kindly answered 21/4, 2015 at 9:17 Comment(3)
Just use Atmosphere, it does exactly what you want and allow to run Jersey on top of Grizzly, GlassFish etc. github.com/Atmosphere/atmosphere. For example goo.gl/s7OwGc is super easy example of Jersey 2 running transparently on top of websockets.Pitarys
see github.com/pavelbucek/placeholder -- PoC, which combines Jersey and Tyrus (on top of Grizzly + HK2).Kindly
Hello, is there any news of such an integration? I would like to add a WebSocket resource to my Jersey project.Cotenant

© 2022 - 2024 — McMap. All rights reserved.