Java
  Home arrow Java arrow Page 5 - Introduction to Enterprise JavaBeans
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Introduction to Enterprise JavaBeans
By: Martin Bond
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 39
    2004-06-21


    Table of Contents:
  • Introduction to Enterprise JavaBeans
  • Types of EJB and Common Uses of EJBs
  • What's in an EJB?
  • The Home Interface
  • Implementing the Home Interface
  • Verifying an EJB
  • The EJB-JAR File
  • How Do I Deploy an EJB?
  • Disposing of the EJB
  • Using the J2EE Reference Implementation
  • Examining the Case Study Application
  • Verifying the Case Study Application
  • Managing J2EE RI Applications
  • Troubleshooting the Case Study Application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to Enterprise JavaBeans - Implementing the Home Interface
    ( Page 5 of 14 )

    Remember that the intention of the EJB environment is that you will spend most of your time writing business logic rather than network and database "plumbing." Beyond writing the business logic, the only additional thing the bean writer needs to do is to provide lifecycle "hooks" that allow the container to manage the bean.

    Each of the different types of EJBs discussed earlier (Session, Entity, and MDB) has a slightly different lifecycle, but the common parts are as follows:

    • Bean creation and initialization

    • Bean destruction and removal

    • The saving and restoring of the bean's internal state (if applicable)

    The details associated with each type of bean lifecycle will be discussed as they are covered. For now, all you need to know is that

    • An EJB will implement one or more lifecycle interfaces depending on its type. The interfaces are defined in the javax.ejb package.

    • Standard lifecycle methods must be provided.

    • The lifecycle methods will generally begin with ejb so that they can be easily distinguished from the business methods around them, for example, ejbCreate().

    Listing 4.4 contains the lifecycle methods in the example AgencyBean with most of the implementation code removed for clarity

    Listing 4.4 Lifecycle Methods on the AgencyBean

    package agency;

    import java.rmi.*;
    import java.util.*;
    import javax.ejb.*;
    // Remaining imports removed for clarity

    public class AgencyBean implements SessionBean
    {
      private DataSource dataSource;
      private String name = "";

      public void ejbCreate () throws CreateException { 
        try {
          InitialContext ic = new InitialContext();
          dataSource = (DataSource)ic.lookup("java:comp/env/jdbc/Agency");
          name = (String)ic.lookup("java:comp/env/AgencyName");
        }
        catch (NamingException ex) {
          error("Error connecting to java:comp/env/Agency:",ex);
        }
      }

      public void ejbActivate() { ... }

      public void ejbPassivate() { ... }

      public void ejbRemove() { ... }

      private SessionContext ctx;
     
      public void setSessionContext(SessionContext ctx) {
        this.ctx = ctx;
      }
    }

    As you can see, the example AgencyBean implements the SessionBean interface. This means that it must implement the ejbRemove(), ejbActivate(), ejbPassivate(), and setSessionContext() methods. The context passed in setSessionContext() provides a way for the bean to communicate with the container. It is usual to save the session context object in an instance variable for use by the other bean methods.

    In an Entity bean the ejbCreate() method takes on the role of constructor in that most of the bean initialization will take place in this method, and corresponds to the create() method defined in the home interface. The ejbCreate() method is not defined in the SessionBean interface, because its signature will vary from one EJB to another (as described tomorrow when Session EJBs are discussed in detail).

    The Deployment Descriptor

    The final piece of the EJB jigsaw lies in the provision of configuration information, or meta data, for the EJB. This provides a way of communicating the EJB's requirements and structure to the container. If an EJB is to be successfully deployed, the container will have to be provided with extra information, including

    • An identifier or name for the EJB that can be used for JNDI lookup to locate the bean.

    • The bean type Session, Entity, or Message-Driven.

    • The EJB's remote interface class. This interface will typically just be named according to the EJB's functionality, for example, Agency or BankTeller.

    • The EJB's home interface class. The name for an EJB's home interface will typically be derived from its remote interface name.

    • The bean class itself. Again, the name for the bean will typically be derived from the associated remote interface name.

    • Any name/value pairs to be provided as part of the bean's environment. Effectively, these are variables that can be given values by the assembler or deployer as well as the developer.

    • Information about any external resources required by the EJB, such as database connections or other EJBs.

    All of this essential information is bundled into a deployment descriptor that accompanies the EJB classes. The deployment descriptor is defined as an XML document, and is discussed in more detail later when examining the packaging of an EJB.

    In addition to the essential information, the deployment descriptor can also carry other metadata such as:

    • Declarative attributes for security and transactions

    • Structural information about bean relationships and dependencies

    • Persistence mapping (if applicable)

    You will see examples of all of these as you progress through this book.

    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 Articles          >>> More By Martin Bond
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek