Wednesday, February 14, 2007

EJB Notes


  • Enterprise JavaBeans -- remote specification

  • Enterprise Java Bean -- remote component













EJB Container

runtime environment for managing enterprise beans

 

instantiates and controls the enterprise beans providing system-level services


System-level services:

  • transaction management for the bean

  • security for the bean

  • persistence of the bean

  • remote access to the bean

  • database connection pooling

  • instance pooling for the bean



















Enterprise Beans

Session Beans

  • stateful

  • stateless



invoked synchronously by an enterprise bean client

Entity Beans

invoked synchronously by an enterprise bean client

Message-driven Beans (MDBs)

invoked by a message container e.g. publish/subscribe topic


★ Session Beans


  • performs business logic for the client


  • represents a single client and not shared across clients


  • not persisted across multiple sessions



■ Stateful Session Bean
⇒ maintains a conversational state with one client for the duration of a single session
⇒ maintain instance variables across multiple invocations from one client during a single session
⇒ life ends when EJB container removes the enterprise bean

■ Stateless Session Bean
⇒ all data is transient between invocations for each client
⇒ each invocation is a request to a brand new object instance

★ Entity Beans

  • share the same qualities found in relational databases

  • intended to represent the business logic for an entity existing in persistent storage

  • responsible for inserting, updating, selecting, removing data within the data source


⇒ persistent
⇒ allow shared access (container handles concurrency)
⇒ have primary keys
⇒ may participate in relationships
⇒ can participate in transactions

Persistence

  • process of writing information to an external data source



▲ Bean Managed Persistence (BMP)
⇒ all access to data source is determined by programmer

▲ Container Managed Persistence (CMP)
⇒ programmer is freed from writing data-access code
⇒ entity bean can be deployed in different containers and/or against different data sources



★ Message Driven Beans

  • allows enterprise applications to handle messages asynchronously





EJB can help solve

  • Scalability (deployable across many different hardware platforms transparently)

  • Transaction Support

  • Client-location Transparency (architecture is designed to hide the location of the EJB from the client program)

  • Data Source Portability (data source access can be handled by container)

  • Reusability (business logic can be written once and reused easily as needed)

  • Asynchronous Messaging (for applications requiring asynchronous communication)



Steps in EJB Deployment



  • Write classes and interfaces for enterprise bean

    • an interface that extends javax.ejb.EJBObject (component interface)

    • an interface that extends javax.ejb.EJBHome (home interface)

    • a class that implements either javax.ejb.SessionBean or javax.ejb.EntityBean (enterprise bean class)




  • Write deployment descriptor (ejb-jar.xml)

    <? xml version="1.0" encoding="UTF-8" ?>
    <ejb-jar>
    <description>xxx</description>
    <display-name>XXX</display-name>
    <enterprise-beans>
    <session>
    <ejb-name/>
    <home/>
    <remote/>
    <ejb-class/>
    <session-type/>
    <transaction-type/>
    </session>
    </enterprise-beans>
    </ejb-jar>



  • Package enterprise bean and associated files into a jar file


  • Deploy the bean