JEE6 全局 JNDI 名称和 Maven 部署

发布于 2024-08-31 23:30:59 字数 1350 浏览 4 评论 0原文

我的 EJB 资源的全局 JNDI 名称存在一些问题,这(或至少会)导致我的 JNDI 查找失败。该项目正在 Netbeans 上开发,是一个标准的 Maven Web 应用程序。当我的应用程序部署到 GF3.0 时,应用程序名称设置为:

com.example_myapp_war_1.0-SNAPSHOT

从 Netbeans 的角度来看,这一切都很好,因为它确保名称是唯一的,但它也意味着所有 EJB获取如下所示的全局名称:

java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService

这当然会导致问题,因为每次版本更改时,所有全局名称都会更改(我已经通过更改版本和名称确实发生了变化)。该名称是从 POM 文件生成的,它是以下内容的串联:

<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

到目前为止,我只是使用 @EJB 注入所有资源,但现在我需要从 JSF 转换器访问 CustomerService EJB,所以我正在这样做JNDI 查找如下:

try {
    Context ctx = new InitialContext();
    CustomerService customerService = (CustomerService)ctx.lookup( "java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService" );
    return customerService.get(submittedValue);
} catch( Exception e ) {
    logger.error( "Failed to convert customer.", e );
    return null;
}

当应用程序正确发布并且模块名称更改时,这显然会中断。所以,百万美元的问题:如何在 Maven 中设置模型名称或如何恢复 模块名称,以便我可以在 runtile 中以编程方式构建 JNDI 名称。我已尝试按照该链接的建议在 web.xml 文件中设置它,但它被忽略了。我想我宁愿在运行时构建名称,因为这意味着部署应用程序时出错的范围更小。

非常感谢您的帮助,我一整天都在为此烦恼。

I'm having some problems with the global JNDI names of my EJB resources which is (or at least will) cause my JNDI look ups to fail. The project is being developed on Netbeans and is a standard Maven Web Application. When my application is deployed to GF3.0 the application name is set to something like:

com.example_myapp_war_1.0-SNAPSHOT

which is all well and good from Netbeans point of view because it ensures the name is unique but it also means all the EJBs get global names such as this:

java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService

This, of course, is going to cause problems because every time the version changes all the global names change (I've tested this by changing the version and the names indeed changed). The name is being generated from the POM file and it's a concatenation of:

<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

Up until now I've got away with just injecting all the resources using @EJB but now I need to access the CustomerService EJB from a JSF Converter so I'm doing a JNDI look up like this:

try {
    Context ctx = new InitialContext();
    CustomerService customerService = (CustomerService)ctx.lookup( "java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService" );
    return customerService.get(submittedValue);
} catch( Exception e ) {
    logger.error( "Failed to convert customer.", e );
    return null;
}

which will clearly break when the application is properly released and the module name changes. So, the million dollar question: how can I set the modle name in maven or how do I recover the module name so that I can programatically build the JNDI name at runtile. I've tried setting it in the web.xml file as suggested by that link but it was ignored. I think I'd rather build the name at runtime as that means there is less scope for screw ups when the application is deployed.

Many thanks for any help, I've been tearing my hair out all day on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-09-07 23:30:59

如果 EJB 包含在 WAR 中,则只需使用 java:module 别名:

java:module/CustomerService

If the EJB is included in the WAR, just use the java:module alias:

java:module/CustomerService
难得心□动 2024-09-07 23:30:59

如何在 Maven 中设置模块名称或如何恢复模块名称,以便我可以在运行时以编程方式构建 JNDI 名称。

好吧,正如提供的链接中所写,关于 EJB 3.1 中可移植全局 JNDI 名称的 module-name

默认为不带包扩展名的包名称(.war 或 .jar)。

因此,我将配置 Maven 并将 finalName 设置为不包含 WAR 名称中的版本:

<project>
  ...
  <build>
    <finalName>${artifactId}</finalName>
    ...
  </build>
</project>

不确定为什么 NetBeans 在部署时包含包名称和下划线(NetBeans 正在这样做,对吧?) GlassFish,如果你能避免 NB 这样做。

how can I set the modle name in maven or how do I recover the module name so that I can programatically build the JNDI name at runtime.

Well, as written in the provided link, about the module-name of Portable Global JNDI name in EJB 3.1:

<module-name> defaults to bundle name (.war or .jar) without the bundle extension.

So I would configure Maven and set the finalName to not include the version in the name of the WAR:

<project>
  ...
  <build>
    <finalName>${artifactId}</finalName>
    ...
  </build>
</project>

Not sure why NetBeans includes the package name and underscore (NetBeans is doing that, right?) when deploying on GlassFish and if you can avoid NB doing that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文