18

In EJB 3.1 JNDI Lookups can be made with different Lookup-Names:

java:global[/<app-name>]/<module-name>/<bean-name>!<fully-qualifiedbean interface-name>           
java:global[/<app-name>]/<module-name>/<bean-name> 
java:app/<module-name>/<bean-name>!<fully-qualified-bean-interface-name> 
java:app/<module-name>/<bean-name> 
java:module/<bean-name>!<fully-qualified-bean-interface-name> 
java:module/<bean-name>

In my JavaEE 6 Project (with Maven 2, Netbeans 6 and Glassfish v3) the Application name is X-Snapshot.ear and the EJB-Module is Y-Snapshot.jar. How can i config this maven project to use another application and module name? I don't wnat to change all JNDI Lookups when this names change!! So is it possible to config application and module names for JNDI LookUps?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Michael W.
  • 531
  • 2
  • 7
  • 17

2 Answers2

18

Naive approach

The Maven EAR Plugin allows to Customize A Module Filename and you can set the final name or the EAR using project.build.finalName.

Much better approach

Override the <application-name> and the <module-name> in the application.xml and the ejb-jar.xml respectively. Quoting Portable Global JNDI name in EJB 3.1:

In addition to the above name, if the EJB exposes just a single client view (that is it implements just one interface or the no interface view), the container is also mandated to map the bean to

java:global/[<application-name>]/<module-name>/<bean-name>

Where

  1. <aplication-name> defaults to the bundle name (.ear file name) without the bundle extension. This can be overridden in application.xml. Also, <application-name> is applicable only if the bean is packaged inside a .ear file.
  2. <module-name> defaults to bundle name (.war or .jar) without the bundle extension. Again, this can be overridden in ejb-jar.xml.
  3. <bean-name> defaults to the unqualified class name of the bean. However, if @Stateful or @Stateless or @Singleton uses the name attribute, then the value specified there will be used as the bean name.
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Thank you Pascal.Before i read your post i tried something else which worked: In the application.xml of the ear-Module you can use abc-app. In the ejb-jar.xml of the ejb-Module you can use abc-module. Your suggestion might also work (i haven't tried it). By the way (for those reading this post): Good links concerning JNDI are: http://blogs.sun.com/MaheshKannan/entry/portable_global_jndi_names http://blogs.sun.com/kensaks/entry/portable_global_jndi_names http://blogs.sun.com/kensaks/entry/application_specified_portable_jndi_names – Michael W. Aug 08 '10 at 06:55
  • @Michael: Much better to override these names in the deployment descriptor. I've updated my answer. – Pascal Thivent Aug 08 '10 at 11:44
  • 1
    @PascalThivent: I added the tags in application.xml and deployed it to the Glassfish (v4) server, but it is being ignored. Is that a bug or do you have any idea why this would happen? – bchetty Apr 10 '15 at 09:48
  • @bchetty: This might very well be a bug, i have the same problem here - currently checking the glassfish sources to find out... – Benjamin Seiller Jun 19 '15 at 09:20
8

The application name and module names can be looked up at runtime via JNDI:

@Resource(lookup = "java:app/AppName")
private String appName;

@Resource(lookup = "java:module/ModuleName")
private String moduleName;

Although you can configure the application-name and module-name in your application deployment descriptor as described, these names can still be overridden at deployment-time (per the Java EE specification, as indicated below), so it's best not to hard-code these values in your application code.

EE.8.5.2 Deploying a Java EE Application and EE.8.5.1 Deploying a Stand-Alone Java EE Module

The deployment tool must ensure that the application name is unique in the application server instance. If the name is not unique, the deployment tool may automatically choose a unique name or allow the Deployer to choose a unique name

EE.8.1.1 Component Creation

If and only if the name is not unique (e.g., because two names are identical after removing different filename extensions) the deployment tool may choose new unique names for any of the conflicting modules; module names that do not conflict must not be changed. The algorithm for choosing unique names in such a case is product specific.

Community
  • 1
  • 1
shelley
  • 7,206
  • 4
  • 36
  • 63
  • I am deploy ear with ejb and web module, how can I get ejb module name by this way? – phanhongphucit Mar 27 '15 at 18:42
  • I think to get the appName and moduleName via ``@Resource`` annotation works only if you already inside the EJB container. If you are outside the EJB-container you must call: ``Context jndiCtxt = new InitialContext(); String appName = (String) jndiCtxt.lookup("java:app/AppName"); String moduleName = (String) jndiCtxt.lookup("java:module/ModuleName");`` I think this is the one and only way to enter the EJB container from outside without worrying about the version information in the appName and the ModuleName – Steffen Jul 01 '22 at 07:35