6

I have EJBs in EJB 3.1 which I am trying to deploy in JBoss EAP 6, but when I start the server. It appends version no in JNDI names as shown below.

18:27:57,068 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-6) JNDI bindings for session bean named TestService in deployment unit subdeployment "TestGroup-war-3_0_0-SNAPSHOT.war" of deployment "TestGroup-ear-3_0_0-SNAPSHOT.ear" are as follows:

java:global/TestGroup-ear-3_0_0-SNAPSHOT/TestGroup-war-3_0_0-SNAPSHOT/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:app/TestGroup-war-3_0_0-SNAPSHOT/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:module/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:global/TestGroup-ear-3_0_0-SNAPSHOT/TestGroup-war-3_0_0-SNAPSHOT/TestService
java:app/TestGroup-war-3_0_0-SNAPSHOT/TestService
java:module/TestService

How do I remove version number "-3_0_0-SNAPSHOT" from my JNDI names ? I have ejb-jar.xml which is placed in ejb jar file when I deploy the ear.

StepUp
  • 36,391
  • 15
  • 88
  • 148
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60

3 Answers3

3

According to the EJB JNDI Naming Reference, the JNDI lookup name for a session bean has the following syntax:

ejb:<appName>/<moduleName>/<distinctName>/<beanName>!<viewClassName>?stateful 

Therefore, what you want can be achieved in two ways:

  1. Modify the name of your deliverable files (WAR and EAR)

In order to remove the version from your WAR you could just do the following in your WAR's POM:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <warName>${project.artifactId}</warName>
            </configuration>
        </plugin>
    </plugins>
</build>

Regarding your EAR, in order to remove the version from it, you could place the following in your EAR's POM:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <configuration>
                (...)

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

                (...)

            </configuration>
        </plugin>
    </plugins>
</build>

With the configuration above, you'd have something like:

.../TestGroup-ear/TestGroup-war/...
  1. Make use of ejb-jar.xml and application.xml files

Create an ejb-jar.xml, with the content below, and place it under your WAR's src/main/webapp/WEB-INF folder:

<ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
    version="3.2">
    <module-name>someModuleName</module-name>
</ejb-jar>

Afterwards, place an application.xml file, under your EAR's src/main/resources/META-INF folder, with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                                 http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
    version="7">

    <application-name>someApplicationName</application-name>

    </<module>
        <web>
            <web-uri>TestGroup-war-${project.version}.war</web-uri>
            <context-root>testGroup</context-root>
        </web>
    </module>
</application>

Then, on your JNDI, you'll have something like:

java:global/someApplicationName/someModuleName/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:app/someModuleName/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:module/TestService!org.pkg.ejb.local.CRMDataServiceLocal
java:global/someApplicationName/someModuleName/TestService
java:app/someModuleName/TestService
java:module/TestService

UPDATE

As of version 2.5, the Maven EAR plugin has the option no-version that can be set to the property fileNameMapping, in order to omit the version from your artifact:

<plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <configuration>
        (...)

        <fileNameMapping>no-version</fileNameMapping>

        (...)

    </configuration>
</plugin>
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
  • I just want version not to appear in jndi name. These changes which you have suggested removes the versioning from EAR and WAR files. – Jigar Naik Mar 24 '16 at 04:23
2

The answers above did not work for me. In my case I upgraded the maven-ear-plugin to version 2.10, and provided the 'fileNameMapping' property as 'no-version':

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <!-- Tell Maven we are using Java EE 7 -->
        <version>7</version>
        <fileNameMapping>no-version</fileNameMapping>    
        <generateApplicationXml>true</generateApplicationXml>
        <defaultLibBundleDir>/lib</defaultLibBundleDir>
        <modules>
            <ejbModule>
                <groupId>${project.groupId}</groupId>
                <artifactId>dpc-ejb</artifactId>
                <bundleDir>/</bundleDir>
            </ejbModule>
            <webModule>
                <groupId>${project.groupId}</groupId>
                <artifactId>dpc-war</artifactId>
                <bundleDir>/</bundleDir>
                <contextRoot>dpc</contextRoot>
            </webModule>
        </modules>
    </configuration>
</plugin>
Meindert
  • 384
  • 2
  • 9
0

You don't have to delete the version information. You can get the correct application and module name via JNDI by self! See also this answer here: https://stackoverflow.com/a/7066600/1465758

If you are already inside the EJB container you can use the @Resource annotation:

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

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

If you are outside the EJB container you must use a JNDI lookup, like:

Context jndiCtxt = new InitialContext();
String appName = (String) jndiCtxt.lookup("java:app/AppName");
String moduleName = (String) jndiCtxt.lookup("java:module/ModuleName");

You can then access your TestService via:

Context jndiCtxt = new InitialContext();
TestService testSvc = (TestService) jndiCtxt.lookup(
                          "java:global/" + appName + "/" + moduleName + "/TestService");

EDIT:

If you have more as one module, may be a JNDI lookup on java:module/ModuleName returns the wrong module name. You can also get all available modules by calling

NamingEnumeration<NameClassPair> list = 
     jndiCtxt.list("java:global/" + appName + "/");

Or even better: While starting your application you can load all known Entities / Classes and the corrosponding JNDI URI in a static Map. At example:

public final class EJBUtil<T> {
  private static final Logger log = PSLogger.getLogger(EJBUtil.class);

  /** list of all known JNDI entities sort by the class: {Class; JNDI URI} */
  private final static Map<Class<?>, String> jndiEntities = new HashMap<>();
  /** synchronisation object for filling jndiEntities */
  private final static ReentrantLock jndiEntitiesLock = new ReentrantLock();

  /** the initial context */
  private static Context jndiCtxt = null;

  /**
   * class of the bean to get
   */
  private final Class<T> beanClass;

  /**
   * Constructs a new EJBUtil for the specified simple bean class name.
   * @param beanClass bean class for which this EJBUtil is
   */
  public EJBUtil(Class<T> beanClass) {
    this.beanClass = beanClass;
  }

  /**
   * Creates and returns a bean object from the EJB container.
   * @return the bean instance or null
   */
  @SuppressWarnings("unchecked")
  public final T getBean() {
    String jndiURI = null;

    try {
      if (jndiEntities.isEmpty()) {
        try {
          loadEntityMap();
        } catch (NamingException exc) {
          exc.printStackTrace();

          log.error(null, "! ! ! ! !   C A N N O T   L O A D   T H E   J N D I   E N T I T I E S  ! ! ! ! !",
              exc);

          return null;
        }
      }

      jndiURI = jndiEntities.get(beanClass);

      if (jndiURI == null) {
        log.error(null, "! ! ! ! !   C A N N O T   L O A D   T H E   \"" + beanClass.getSimpleName()
                + "\"   B E A N  (Entity not found in Naming Directory  ! ! ! ! !");
        return null;
      }

      Object obj = jndiLookup(jndiURI);
      return (T) obj;

    } catch (NamingException exc) {
      exc.printStackTrace();

      log.error(null, "! ! ! ! !   C A N N O T   L O A D   T H E   \"" + beanClass.getSimpleName()
              + "\"   B E A N  (JNDI URI: \"" + jndiURI + "\")  ! ! ! ! !",
          exc);

      return null;
    }
  }

  /**
   * Creates and returns a bean object from the EJB container.
   * @param name JNDI name of the resource to return
   * @return the resource object or null
   * @throws NamingException if the bean could not be created
   */
  public static final Object jndiLookup(String name) throws NamingException {
    try {
      if (jndiCtxt == null) {
        jndiCtxt = new InitialContext();
      }

      return jndiCtxt.lookup(name);
    } catch (NamingException exc) {
      exc.printStackTrace();

      log.error(null, "! ! ! ! !   C A N N O T   L O A D   T H E   J N D I   R E S O U R C E   \"" + name +
          "\"  (JNDI: \"" + name + "\")  ! ! ! ! !", exc);

      throw exc;
    }
  }

  private void loadEntityMap() throws NamingException {
    jndiEntitiesLock.lock();

    try {
      if (!jndiEntities.isEmpty()) {
        // map already filled
        return;
      }

      loadEntityMap("java:global/");
    } finally {
      jndiEntitiesLock.unlock();
    }

  }

  private void loadEntityMap(String jndiBaseURI) throws NamingException {
    if (jndiCtxt == null) {
      jndiCtxt = new InitialContext();
    }

    NamingEnumeration<NameClassPair> list = jndiCtxt.list(jndiBaseURI);

    while (list.hasMore()) {
      NameClassPair nameClassPair = list.next();

      if (nameClassPair.getClassName() != null &&
          !nameClassPair.getClassName().equals(Context.class.getName())) {

        try {
          jndiEntities.put(
              Class.forName(nameClassPair.getClassName()), jndiBaseURI + "/" + nameClassPair.getName());
        } catch (ClassNotFoundException e) {
          log.error(null, "Cannot load JNDI entity class " + nameClassPair.getClassName(), e);
        }
      }

      if (nameClassPair.getClassName() == null ||
          nameClassPair.getClassName().equals(Context.class.getName())) {

        loadEntityMap(jndiBaseURI + nameClassPair.getName() + "/");
      }
    }
  }
}

You can load then the EJB TestService by calling:

TestService testSvc = new EJBUtil<TestService>(TestService.class).getBean();
Steffen
  • 2,500
  • 4
  • 31
  • 47