Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Jersey with Tyrus

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?

like image 850
Giovanni Lovato Avatar asked Apr 20 '15 14:04

Giovanni Lovato


1 Answers

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.

like image 133
Pavel Bucek Avatar answered Nov 03 '22 08:11

Pavel Bucek