Java EE 7:如何将 EJB 注入 WebSocket ServerEndpoint? [英] Java EE 7: How-to inject an EJB into a WebSocket ServerEndpoint?

查看:35
本文介绍了Java EE 7:如何将 EJB 注入 WebSocket ServerEndpoint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结我失败的项目:我的 @ServerEndpoint 类与 beans.xml 文件一起打包在 WAR 中.我的 WAR 又打包在一个 EAR 中,这个 EAR 文件被部署到内部使用 Tyrus 的 GlassFish 4 服务器.

To sum up my failing project: My @ServerEndpoint class is packaged in a WAR together with the beans.xml file. My WAR in turn is packaged in a EAR and this EAR file is what gets deployed to a GlassFish 4 server that internally use Tyrus.

WebSocket 规范说:

The WebSocket specification says:

在 Java EE 平台上运行的 Websocket 端点必须具有完整的CDI 规范中描述的依赖注入支持.Java EE 平台的 Websocket 实现部分需要支持字段、方法和构造函数注入使用javax.inject.将注解注入所有 websocket 端点类,以及对这些类使用拦截器.

Websocket endpoints running in the Java EE platform must have full dependency injection support as described in the CDI specification. Websocket implementations part of the Java EE platform are required to support field, method, and constructor injection using the javax.inject. Inject annotation into all websocket endpoint classes, as well as the use of interceptors for these classes.

这一段我唯一能理解的是,将 Enterprise JavaBean 注入 WebSocket 应该不是火箭科学.然而,对我来说,无论我做什么,它都不起作用.我觉得最直观的应该只需要在服务器端点实例字段前面加上 @EJB@Inject 注释,但这些注释都不起作用.变量将为空.

The only thing I can understand of this paragraph is that injecting an Enterprise JavaBean into a WebSocket should be no rocket science. However, for me, whatever I do, it fails to work. I feel that most intuitively one should only need to prefix a server endpoint instance field with the @EJB or @Inject annotation, but no one of these annotations work. The variable will be null.

一个互联网来源有点神秘地说由于错误",他必须使用构造函数注入.我看到他已将注释 @Named 添加到服务器端点.我使用了著名的复制粘贴模式,并完全按照他所做的去做,有和没有 @Named 注释,但它仍然不起作用.事实上,我的 @Inject 注释构造函数甚至从未被调用!

One Internet source says a bit cryptically that "due to a bug" he must use constructor injection. I saw that he had added the annotation @Named to the server endpoint. I used the famous copy paste pattern and did exactly what he did, with and without the @Named annotation, and it still don't work. In fact, my @Inject annotated constructor is never even called!

Tyrus 用户指南 表示可以将任何著名的会话 bean 声明注释与服务器端点(@Stateful@Stateless@Singleton)混合在一起.所以我做了,仍然注射失败.使用注解@Inject 或@EJB 都没有关系.

The Tyrus user guide says that one can mix together anyone of the famous session bean declaration annotations with the server endpoint (@Stateful, @Stateless and @Singleton). So I did, still the injection fails to happen. It doesn't matter if I use the annotation @Inject or @EJB.

这很奇怪,因为这本书 JavaEE 7 Developer Handbook 声称在第 27 页和第 28 页上有一个基于相同方法的工作示例.作者 Peter Pilgrim 注释了他的服务器端点 @Stateless.然后他使用 @Inject 进行注入.他说:

And that is strange, because the book Java EE 7 Developer Handbook claims to have a working example on page 27 and page 28 based on the same approach. Author Peter Pilgrim annotates his server endpoint @Stateless. He then uses @Inject to do the injection. He says:

在 Java EE 7 中,我们还必须将 [我们的服务器端点] 声明为无状态带有@Stateless 的 EJB,以便将 [另一个 EJB] 作为依赖项注入.(这是 Java for WebSocket 1.0 规范的结果.)我们可以从 CDI 使用 @javax.annotation.Inject.

In Java EE 7 we must also declare [our server endpoint] as a stateless EJB with @Stateless in order to inject [another EJB] as a dependency. (This is a consequence of Java for WebSocket 1.0 specification.) Note that we can use @javax.annotation.Inject from CDI.

好的,所以他说我们必须使用@Stateless 注释,并注意"可以使用@Inject.对我来说,我们必须"在服务器端点上使用 @Stateless 注释听起来很奇怪,根据规范,它是无状态(!)之外的一切.我在互联网上的其他地方读到过,使用 @Inject 而不是 @EJB 应该是一种解决方法.彼得注意到"我们可以使用"@Inject 但它闻起来很腥,好像他根本没有让@EJB 工作,现在试图逃避责任.

Okay so he says we must use a @Stateless annotation, and "notes" that one can use @Inject. For me, it sounds utterly strange that we "must" use a @Stateless annotation on a server endpoint which according to the specification, is everything else than stateless (!). I've read elsewhere on the Internet that using @Inject instead of @EJB should be one fix. Peter "notes" that "we can use" @Inject but it smells fishy, as if he never got @EJB to work at all and now tries to flee responsibility.

好吧,无论是什么原因(错误"或规范的后果"),我都无法让我的依赖注入起作用,无论我在端点类本身或实例字段上使用了何种生动的注释组合.

Well, whatever the reason ("bug" or "consequence of the specification"), I couldn't get my dependency injection to work whatever vivid mix of annotations I used on the endpoint class itself or on the instance field.

是以编程方式使用 JNDI 查找,但它看起来很丑,应该避免.

Is to programmatically use a JNDI lookup, but it looks ugly and should be avoided.

推荐答案

(只是重申我在评论中写的内容,以便从未答复"列表中获取此问题)

(just restating what I wrote into comment to get this question from "unanswered" list)

您应该查看 Tyrus CDI 示例/测试.

它演示了您可以使用当前实现做什么的列表.我们总是对新的测试用例开放,但规范本身存在一些问题 - 标准请求范围不适用于 WebSocket 运行时,因为它处理 servlet 服务/doFilter 方法之外的消息.请参阅 WEBSOCKET_SPEC-196WEBSOCKET_SPEC-197.

It demonstrates list what you can do with current implementation. We are always open for new test cases, but there are some issues with the spec itself - standard Request scopes don't work for WebSocket runtime, because it handles messages outside of servlets service/doFilter methods. See WEBSOCKET_SPEC-196 and WEBSOCKET_SPEC-197.

这篇关于Java EE 7:如何将 EJB 注入 WebSocket ServerEndpoint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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