从嵌入式 Glassfish 3.1 获取上下文
现在有没有人可以使用 Embeddable API 获取服务器上下文(使用 org.glassfish.embeddable.GlassFish,而不是 javax.ejb.embeddable.EJBContainer)? 如果有一种方法可以从正在运行的 Glassfish 获取 EJBContainer,但我什至找不到可用于查找的服务列表。
Does anyone now a way to obtain server Context using Embeddable API (using org.glassfish.embeddable.GlassFish
, not javax.ejb.embeddable.EJBContainer
)?
It would be possible if there's a way to obtain EJBContainer from a running Glassfish, but I can't find even the list of services available for lookup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个解决方法 - 我们可以获取 InitialContext 作为外部客户端。
有关完整说明,请检查 EJB_FAQ 。这样至少可以测试远程 EJB:
因此完整的示例如下所示:
注意末尾有一个 System.exit(0) - com.sun.corba.ee.impl.javax.rmi.CORBA.Util.KeepAlive即使服务器停止阻止 JVM 停止,线程仍在运行...
Here's a workaround - we can obtain InitialContext as an external client.
For the full explanation check EJB_FAQ . This way at least remote EJBs could be tested:
So the full example will look like:
Note there's a System.exit(0) at the end - com.sun.corba.ee.impl.javax.rmi.CORBA.Util.KeepAlive thread is running even after the server stop preventing JVM from stopping...
据我所知,您可以初始化
InitialContext
类来获取上下文,该上下文可进一步用于执行查找。这已经过测试,发现可以在查找部署在嵌入式容器中的 EJB 的上下文中工作。 EJB 未配置为允许访问特定角色,在这种情况下,com.sun.appserv.security.ProgrammaticLogin
类(未通过 Embeddable EJB API 公开)可能会有所帮助;这尚未经过测试,但这是为访问 EJB 的线程初始化Principal
的推荐方法。下面是一个或多或少完整的示例,该示例从 Maven 运行并使用 POM 中嵌入的 Glassfish 依赖项(为简洁起见,此处未复制):
EJB 接口:
会话 Bean:
单元测试:
请注意,EJB 是使用显式可移植 JNDI 名称(通过
@EJB
注释)进行部署的,因为我还有其他使用公共嵌入式 EJB 的测试其他测试中的API,在此类测试中指定应用程序名称或多或少有些困难;每个测试执行可能会导致 EJB 产生不同的 JNDI 名称,因此需要指定显式 JNDI 名称。As far as I know, you can initialize the
InitialContext
class to obtain a context, that can further be used to perform the lookup. This was tested, and found to work in the context of looking up an EJB, deployed in the embedded container. The EJB was not configured to allow access to specific roles, in which case thecom.sun.appserv.security.ProgrammaticLogin
class (not exposed via the Embeddable EJB API) might help; this was not tested, but is the recommended way to initialize thePrincipal
for the thread accessing an EJB.A more or less complete example that runs from Maven and uses the embedded Glassfish dependency in a POM (not reproduced here, for brevity) follows:
The EJB interface:
The Session Bean:
The unit test:
Note that the EJB is deployed with a explicit portable JNDI name (via the
@EJB
annotation), as I have other tests that use the public embeddable EJB API in other tests, and it is more or less difficult to specify an application name in such tests; each test execution might result in a different JNDI name for the EJB, thus necessitating an explicit JNDI name to be specified.