The deployment descriptor

Now it's time to view the deployment descriptor. As a reminder, this file tells the EJB server which classes make up the bean implementation, the home interface and the remote interface. If there is more than one EJB in the package, it indicates also how the EJBs interact with one another. In this simple example, there is only one EJB so we won't need to worry about that part.

Most commercial EJB servers are supplied with graphical tools for constructing the deployment descriptor. JBoss does have an XML editor, but it's just as easy to construct the deployment descriptor manually. Here it is:

Figure 1.6. Deployment descriptor for the Interest Bean


<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar>
     <description>JBoss Interest Sample Application</description>
     <display-name>Interest EJB</display-name>
     <enterprise-beans>
       <session>
         <ejb-name>Interest</ejb-name>
         <home>org.jboss.docs.interest.InterestHome</home>
         <remote>org.jboss.docs.interest.Interest</remote>
         <ejb-class>org.jboss.docs.interest.InterestBean</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Bean</transaction-type>
       </session>
     </enterprise-beans>
</ejb-jar>

The deployment descriptor must be called ejb-jar.xml and it must be in the directory ./META-INF. A common mistake is to name this directory `META_INF' (with an underscore, rather than a dash) or meta-inf which won't work.

In principle what we deploy on the server is an application, not a single EJB. In this example our application consists of only one EJB. In the deployment descriptor, the section <ejb-name>Interest</ejb-name> assigns a name to the EJB. JBoss by default will put the bean's home interface under the ejb-name moniker in the JNDI namespace if you don't override this default behavior.

In practice client applications are not forced to use this name. Typically a developer will not bother with specifying a different name for the JNDI namespace. However a production installation of a complete application comprising many beans, will usually use a different name rather than the one specified by the developer. Typically this would be something like "[application name]/[bean name]", which is what we shall use later.

Although the deployment descriptor format of the ejb-jar.xml file is common to all EJB servers, and precisely defined by a DTD you can get from Sun, it doesn't specify everything necessary for a particular EJB server. Specifically it does not indicate how to map ejb-name to a deployment JNDI name such as "[application name]/[bean name]".

The approach taken by JBoss is to provide a default behaviour that works from the ejb-jar.xml file in most cases. In case of advanced configurations you need to define JBoss specific behavior using a jboss.xml deployment descriptor. Refer to the advanced configuration section for the complete details of jboss.xml descriptor. The only item we will configure via the jboss.xml descriptor is the JNDI name used to access the Interest EJB home interface.

By default the JNDI name used to access an EJB's home interface is the same as the ejb-name value from ejb-jar.xml. For this example that means that the Interest bean would be located under the JNDI InitialContext as "Interest". We want to have the home interface available using "interest/Interest" so we need to specifiy this using the jboss.xml descriptor.

Overriding the default JNDI name in jboss.xml

To override the default behavior of using the ejb-jar.xml ejb-name element value as the JNDI name of a bean's home interface, you need to specify what JNDI name should be used using a jboss.xml descriptor like the following:

Figure 1.7. The JBoss XML descriptor.


<?xml version="1.0" encoding="UTF-8"?>
<jboss>
  <enterprise-beans>
    <session>
      <ejb-name>Interest</ejb-name>
      <jndi-name>interest/Interest</jndi-name>
    </session>
  </enterprise-beans>
</jboss>

All this file says is that the Bean called Interest is to be bound under the JNDI name of interest/Interest.

So now we've created the standard ejb-jar.xml deployment descriptor as well as the JBoss specific jboss.xml to set the JNDI name of the Interest EJB home interface to "interest/Interest". We also have the EJB classes and so we have all that we need to create the EJB jar package.