简体   繁体   English

Undertow Websocket Bean注入CDI问题

[英]Undertow Websocket Bean Injection CDI Issue

I don't understand why CDI use of injection doesn't work with websockets, using undertow. 我不明白为什么CDI使用注入不适用于使用undertow的websocket。

Below is the code I have for a simple websocket endpoint. 下面是我为一个简单的websocket端点编写的代码。

@ServerEndpoint("/")
public class TestWebSocketEndpoint {

    @Inject
    private RetrieveAccessor retrieveAccessor;

    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @OnOpen
    public void onConnectionOpen(Session session) {
        logger.info("Connection opened ... " + session.getId());
    }

    @OnMessage
    public String onMessage(String message) {

        if (!message.isEmpty()) {
            return message;
        }

        System.out.println("RETRIEVE BEAN -> " + retrieveAccessor);
        if (retrieveAccessor != null) {
            return "BEAN NOT NULL";
        }
        return ":(";
    }

    @OnClose
    public void onConnectionClose(Session session) {
        logger.info("Connection close .... " + session.getId());
    }

}

Of course the issue is that the injected property is null. 当然,问题在于注入的属性为null。 I have no problems of course using the rest side of things for this deployment and injection of the stateless bean described below. 我当然没有问题,使用其余的东西进行此部署和下面介绍的无状态Bean的注入。 Is there a work around for this, what are the problems I could run into if I just init properties I need that are beans? 有没有解决的办法,如果我只初始化需要的bean属性,会遇到什么问题? Because that definitely works. 因为那绝对可行。

RetrieveAccessor retrieveAccessor = new.... {code} RetrieveAccessor RetrieveAccessor = new .... {代码}

An easy way to get injection working on your @ServerEndpoint annotated classes is to set a custom configurator that handles the creation of your endpoint instance by overriding the getEndpointInstance(Class endpointClass) method to instantiate with CDI. 一种在@ServerEndpoint带注释的类上进行注入的简单方法是设置一个自定义配置器,该配置器通过重写getEndpointInstance(Class endpointClass)方法来使用CDI实例化端点实例的创建。

https://tyrus.java.net/documentation/1.13/user-guide.html#d0e464 https://tyrus.java.net/documentation/1.13/user-guide.html#d0e464

Annotated endpoint: 带注释的端点:

@ServerEndpoint(value = "/", configurator = CDIEndpointConfigurator.class)
public class TestWebSocketEndpoint {
   ...
}

Custom configurator: 自定义配置器:

public class CDIEndpointConfigurator extends ServerEndpointConfig.Configurator {

    @Override
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
        return CDI.current().select(endpointClass).get();
    }
}

Undertow is only a servlet container. Undertow只是一个servlet容器。 Weld (or OWB) provide CDI support. Weld(或OWB)提供CDI支持。 I'm not sure how you're instantiating Undertow, but you need to leverage Weld (or some other CDI implementation). 我不确定如何实例化Undertow,但是您需要利用Weld(或其他一些CDI实现)。

Here's one example how to do it. 这是一个示例。 Leverage a CDI Extension to find the endpoints, and once you have them you can register them in Undertow 利用CDI扩展来查找端点,一旦有了端点,就可以在Undertow中注册它们

Feel free to leverage Hammock for this. 随意利用Hammock。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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