Specifying the deployment name of your beans

You have coded your beans. You now want to deploy them and be able to access them from your clients. For this, JBoss will register your beans in the JNDI namespace. All you have to tell is the name under which they must be registered.

Standard behaviour of jboss

The simplest way to deploy your bean is to use the ejb-jar.xml file to give it the same name you call it in the client. This is done in the <ejb-name> tag for the bean. In this case you DON'T need a jboss.xml file. Your ejb-jar.xml file will look like this (this is covered in the beginners trails):

ejb-jar.xml:


<ejb-jar>
  <enterprise-beans>
                                                                      
    <session>
      <ejb-name>MyBeanName</ejb-name>
      <home>MyBeanHome</home>
      <remote>MyBean</remote>
      <ejb-class>MyBeanEJB</ejb-class>
      <session-type>Stateful</session-type>
      <transaction-type>Container</transaction-type>
    </session>

  </enterprise-beans>
</ejb-jar>

And your bean will be available under the "MyBeanName" in JNDI. In your client code, your call will look like this:

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
   
public class Client {                                                 
  
   public static void main(String args[]) {
      ...  
      InitialContext namingContext = new InitialContext();
      Object ref = namingContext.lookup("MyBeanName");
      MyBeanHome home = (MyBeanHome)PortableRemoteObject.narrow(ref, MyBeanHome.class);
      ...
   }
  
}

Here the "MyBeanName" refers to the name of your bean in the JNDI namespace. JBoss does not currently allow you to use the java:comp/env namespace to call your beans from your clients.

Registering a bean with a JNDI deployment name different than the ejb-name

You may want to deploy your bean under another JNDI name. (for example, you may want to deploy the same bean under different names with different configurations). In this case, you must specify the JNDI name in a jboss.xml file. This file must be in the META-INF directory, along with ejb-jar.xml. Your files will look like this (note that the <ejb-name>tags in the two xml files must match):

jboss.xml:


<jboss>                                                               
  <enterprise-beans>
    <session>
      <ejb-name>MyBeanName</ejb-name>
      <jndi-name>somePath/otherName</jndi-name>
    </session>
  </enterprise-beans>
</jboss>

(you don't need to specify the rest of the jboss.xml file, only this information is sufficient in the new metadata, this is what we call "differential" jboss.xml).

Your bean is then available in JNDI under somePath/otherName

In your client code, your call will look like this:

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
   
public class Client {                                                 
   
   public static void main(String args[]) {
      ...
      InitialContext namingContext = new InitialContext();
      Object ref = namingContext.lookup("somePath/otherName");
      MyBeanHome home = (MyBeanHome)PortableRemoteObject.narrow(ref, MyBeanHome.class);
      ...
   }
   
}