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

查看:164
本文介绍了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规范中所述。
Websocket实现Java EE平台的一部分需要
支持字段,方法和使用
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.

这很奇怪,因为 Java EE 7开发人员手册 声称基于相同的方法在第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中,我们还必须将[我们的服务器端点]声明为无状态
EJB与@Stateless命令注入[另一个EJB]作为依赖。
(这是Java对于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.

那么,无论什么原因(bug或规范的后果),我无法使我的依赖注入工作在终端类本身或实例字段上使用的任何生动的注释组合。

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-196 WEBSOCKET_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天全站免登陆