Home arrow Java & J2EE arrow Page 8 - Introduction to Enterprise JavaBeans

How Do I Deploy an EJB? - Java

This chapter looks at one of the principal types of component in the Java 2 Platform, Enterprise Edition (J2EE) — Enterprise JavaBeans (EJBs). See how EJBs are applied and how they are deployed. (This is chapter 4 from Sams Publishers, author Martin Bond, et. al., Teach Yourself J2EE in 21 Days, second edition, ISBN: 0-672-32558-6).

  1. Introduction to Enterprise JavaBeans
  2. Types of EJB and Common Uses of EJBs
  3. What's in an EJB?
  4. The Home Interface
  5. Implementing the Home Interface
  6. Verifying an EJB
  7. The EJB-JAR File
  8. How Do I Deploy an EJB?
  9. Disposing of the EJB
  10. Using the J2EE Reference Implementation
  11. Examining the Case Study Application
  12. Verifying the Case Study Application
  13. Managing J2EE RI Applications
  14. Troubleshooting the Case Study Application
By: Martin Bond
Rating: starstarstarstarstar / 41
June 21, 2004

print this article



After an EJB is packaged, it can be deployed in an appropriate J2EE server. This is done using tools supplied by the J2EE Server vendor. Often these will be Java GUI applications, but command-line tools and Web interfaces are also used by some vendors. The typical process is to define the target J2EE server, identify the EAR file to be deployed, supply user credentials (typically administrative username and password) and away you go.

Remember that J2EE defines a separate role for the application Deployer. It may be that, for particular installations the databases or other resource names need to be changed to match the local environment. When configuring the application, the Deployer may need to alter the EJB or enterprise deployment descriptors but will not need to change the Java source code.

When an EJB is deployed into a particular EJB container, the EJB must be plugged into that container. To do this, an EJB Object must be generated based on the EJB's remote interface. This EJB Object will be specific to that EJB container and will contain code that allows the EJB Object to interface with that container to access security and transaction information. The container will examine the deployment descriptors supplied with the EJB to determine what type of security and transaction code is required in the EJB Object.

The container will also generate a class that implements the EJB home interface so that calls to the create, find, and destroy EJB instances are delegated to container-defined methods.

Finally, the container will register the JNDI name for the home interface of the EJB. This allows other application components to create and find this application's EJBs.

After the EJB has been deployed, any subsequent changes to its functionality will mean that the EJB must be redeployed. If the enterprise application or EJB is no longer needed, it should be undeployed from the container.

How Do I Use an EJB?

Given that EJBs are middle-tier business components, they are of little use without a client to drive them. As mentioned earlier, those clients can be Web components, standalone Java clients, or other EJBs.

Regardless of the type of client, using an EJB requires the same set of steps—namely, discovery, retrieval, use, and disposal. Listing 4.7 shows a simple client for use with the Agency EJB illustrating these steps. The following three sections cover these steps in more detail.

Listing 4.7 Simple Agency Client SimpleClient.java

package client;

import agency.*;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;

public class SimpleClient
  private static String agencyJNDI = "java:comp/env/ejb/Agency";

  public static void main(String[] args) {
    if (args.length == 1)
      agencyJNDI = args[0];
    else if (args.length > 1) {
      System.err.println("Usage: SimpleClient [ AgencyJNDI ]");
    try {
      InitialContext ic = new InitialContext();
      Object lookup = ic.lookup(agencyJNDI);
      AgencyHome home = (AgencyHome)
        PortableRemoteObject.narrow(lookup, AgencyHome.class);    
      Agency agency = home.create();
      System.out.println("Welcome to: "+agency.getAgencyName());
      System.out.println("Customer list: "+agency.getAgencyName());
      Collection customers = agency.findAllCustomers();
      Iterator it = customers.iterator();
      while (it.hasNext())
        String name = (String)it.next();
    catch (NamingException ex) {
    catch (ClassCastException ex) {
    catch (CreateException ex) {
    catch (RemoteException ex) {
    catch (RemoveException ex) {


To create or find an EJB, the client must call the appropriate method on the EJB's home interface. Consequently, the first step for the client is to obtain a remote reference to the home interface. The client obtains the reference by accessing the J2EE server's name service using JNDI.

For this simple process to work, the following three actions must have taken place:

  • The developer has defined the EJB reference used in the Java code

  • The Deployer has mapped this reference onto the actual JNDI name of the EJB

  • The EJB container has registered the home interface using the JNDI name specified as part of the platform specific deployment descriptor

As discussed on Day 3, each J2EE component has its own Java Component Environment space and the JNDI name used in the client code will be prefixed with java:comp/env. The object retrieved is a reference to the RMI-IIOP remote stub that must be downcast to the home interface. The following code shows the initial lookup required for the Agency EJB:

  InitialContext ic = new InitialContext();
  Object lookup = ic.lookup("java:comp/env/ejb/Agency");
  AgencyHome home =
     (AgencyHome)PortableRemoteObject.narrow(lookup, AgencyHome.class);
 catch (NamingException ex) { /* Handle it */ }
 catch (ClassCastException ex) { /* Handle it */ }

Now that you have a reference to the home interface, you can create the EJB.

Retrieval and Use

Once you have a reference to the EJB home interface, you can call the create() method you saw defined on the AgencyHome interface. The create() method returns a remote reference to the newly-created EJB. If there are any problems with the EJB creation or the remote connection, a CreateException or RemoteException will be thrown. CreateException is defined in the javax.ejb package, and RemoteException is defined in the java.rmi package, so remember to import these packages in your client class.

Now that you have a reference to an EJB, you can call its business methods as follows:

  Agency agency = home.create();
  System.out.println("Welcome to: " + agency.getAgencyName());
 catch (RemoteException ex) { /* Handle it */ }
 catch (CreateException ex) { /* Handle it */ }

The previous code sample shows the getAgencyName() method being called on the returned Agency reference. Again, whenever you call a remote method that is defined in an EJB remote interface, you must be prepared to handle a RemoteException.

Note - You will see later that some Entity beans are found rather than created. In this case, all steps are the same except that the create() method is replaced by the appropriate finder method and find-related exceptions must be handled. You still end up with a remote reference to an EJB. All of this is covered when Entity beans are discussed on Day 6.

This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.

>>> More Java & J2EE Articles          >>> More By Martin Bond

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: