从WebSocket @ServerEndpoint使用CDI @SessionScoped bean [英] Using a CDI @SessionScoped bean from a WebSocket @ServerEndpoint

查看:88
本文介绍了从WebSocket @ServerEndpoint使用CDI @SessionScoped bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序中,用户使用Servlet HTTP会话。一些数据存储在CDI @SessionScoped bean中。在某些页面的后面,将在用户浏览器和服务器之间执行WebSocket通信。

In a web application, users use a servlet HTTP session. Some data are stored in CDI @SessionScoped beans. Later in some pages WebSocket communications are performed between the user browser and the server.

对于GlassFish 4,当从带有GlassFish的WebSocket @ServerEndpoint使用注入的@SessionScoped CDI bean时4.0我收到错误消息:
org.jboss.weld.context.ContextNotActiveException:WELD-001303作用域类型javax.enterprise.context.SessionScoped

With GlassFish 4, when using an injected @SessionScoped CDI bean from a WebSocket @ServerEndpoint with GlassFish 4.0 I get an error message: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped

从JSP / servlet类型请求中,使用@SessionScoped CDI bean很好,但不能从WebSocket @ServerEndpoint中使用。

From JSP/servlet type requests, using the @SessionScoped CDI bean is fine, but not from the WebSocket @ServerEndpoint.

还请注意,使用@Singleton CDI bean从@ServerEndpoint可以正常工作,因此这不是通用的依赖项注入问题。

Also note that using @Singleton CDI beans from the @ServerEndpoint works fine, so this is not a general purpose dependency injection problem.

也许我做错了。

所以我的问题是可以从WebSocket @ServerEndpoint bean的方法中使用@SessionScoped CDI bean吗?如果是,怎么办?

So my questions is can @SessionScoped CDI beans be used from methods of WebSocket @ServerEndpoint beans? If yes, how?

谢谢您的时间。

推荐答案

可能不是完全相同的问题,但是问题非常相似,因此此处的答案适用。基本上,就像@JoakimErdfelt指出的那样,Websocket对CDI的支持充其量是有问题的。 Websocket规范忽略了什么作用域。

It may not be the exact same question, but the issue is similar enough that the answers there apply here. Basically, as @JoakimErdfelt notes websocket support for CDI is problematic at best. The websocket spec neglected to mention what scopes are active.

开箱即用,这是Tyrus支持的功能: https://github.com/tyrus-project/tyrus/ tree / master / samples / cdi / src / main / java / org / glassfish / tyrus / sample / cdi

Out of the box, this is what Tyrus supports: https://github.com/tyrus-project/tyrus/tree/master/samples/cdi/src/main/java/org/glassfish/tyrus/sample/cdi

如果需要,可以扩展它启动会话作用域(供参考,Apache DeltaSpike的CDI上下文控制),但是由于协议的不同,它将是与已经通过HTTP建立的会话不同的会话。

If you want, you can extend it to start a session scope (for reference, Apache DeltaSpike's CDI Context Control), but because of the protocol difference it would be a different session than the one already established via HTTP.

这篇关于从WebSocket @ServerEndpoint使用CDI @SessionScoped bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆