简体   繁体   中英

Dependency injection into websocket endpoint on embedded jetty

I have a the following websocket endpoint:

import javax.inject.Inject;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/blabla")
public class WebsocketService {

    @Inject
    private DatabaseProvider dbProvider;

    @OnOpen
    public void onOpen(Session session) throws IOException {
        //do something
    }

    @OnMessage
    public void onMessage(Session session, String socketPacket) throws IOException {
        //do something else
    }
    ...
}

The code to start the embedded server:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
import javax.websocket.server.ServerContainer;
//other imports

public static void main(String[] args) {
    Server server = null;
    try {
        server = new Server(3081);
        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(context);
        serverContainer.addEndpoint(WebsocketService.class);
        server.setHandler(context);
        server.start();
        server.join();
    } catch (Exception e) {
        logger.error(e.getMessage());
    } finally {
        if (server != null) {
            server.destroy();
        }
    }
}

The code above works perfectly for the case without dependency injection. However, I want to inject the dbProvider into my WebsocketService and use it in the onMessage method.

QUESTION 1: How to do the injection for the websocket server?

PS There are multiple examples of how dependency injection is done for REST endpoinds using ResourceConfig + AbstractBinder + ServletContainer, but I am not sure how it can be applied for the case with the websocket server.

QUESTION 2: How to add a simple resource endpoint to the same server (to serve javascript)?

Quite a few moving parts in this question.

First you have to setup Weld (the CDI implementation) to properly integrate it with your ServletContextHandler

Typically seen like this ...

ServletContextHandler context = new ServletContextHandler();
// Enable Weld + CDI
context.setInitParameter(
  CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE,
  CdiDecoratingListener.MODE);
context.addBean(
  new ServletContextHandler.Initializer(context, 
      new CdiServletContainerInitializer()));
context.addBean(
  new ServletContextHandler.Initializer(context, 
      new org.jboss.weld.environment.servlet.EnhancedListener()));

Then the injection (actually decoration) is automatically taken care of internally between Jetty and Weld.

Note: the ServletContexthandler.Initializer is a convenience class to allow your embedded-jetty to run an arbitrary javax.servlet.ServletContainerInitializer without all of the overhead of a full blown WebApp and it's complex initialization process.

  • The CdiServletContainerInitializer is a ServletContainerInitializer that Jetty provides which sets up various things in the ServletContext to allow Weld to wire itself up properly to the ServletContext .
  • The EnhancedListener is also a ServletContainerInitializer that weld provides which does it's side of the wiring up for Weld + CDI.

For serving static files, you'll want to have a "Base Resource" defined in your ServletContextHandler and then add the DefaultServlet to the "default" url-pattern of "/" .

ServletContextHandler context = new ServletContextHandler();
context.setBaseResource(Resource.newResource(webRootUri));
context.addServlet(DefaultServlet.class, "/");

If you want to see all of this together, check out the example project at

https://github.com/jetty-project/embedded-jetty-weld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM