Introduction to Enterprise Java Bean 3

What is EJB ?

  • An Enterprise Java Bean (EJB) is a managed, server-side component architecture for modular construction of enterprise applications that encapsulates the business logic of an application.
  • The EJB specification is one of several Java APIs in the Java Platform and Enterprise Edition.
  • By using EJB you can write scalable, reliable and secure applications without writing your own complex distributed component framework.
  • EJB is designed to support application portability and reusability across any vendor's enterprise middle services.
  • The EJB specification was originally developed in 1997 by IBM
  • Later adopted by Sun Microsystems
  • Later Enhanced under the Java Community Process

EJB Framework Components :

  • EJB Server - Provides the actual primary services to all EJB components. An Application server may host one or more EJB containers. And many EJB components can be created in a EJB container. It is also called an Enterprise Java Server (EJS).
  • Following are most common Enterprise Java Servers :
  • EJB Container - Provides the runtime environment for the enterprise bean instances, and is the intermediary between the EJB component and the server.
  • EJB Component - These represent the actual EJBs themselves. There are two types of enterprise beans: Session Beansand Message Driven Beans.
  • EJB Interfaces and EJB bean - The interfaces for client access (Remote/Local and EJB object) and the EJB bean class.
  • EJB Client - A program that accesses EJB componnets. It can be : a Standalone Java application, an Applet, Servlet and JSP etc.

[ Note : EJB deployment descriptor (XML) is optional in EJB 3.0 ]

Types of Enterprise Java Bean

Session Bean

  • Business-logic components who provide coordination to other beans
  • Usually represent Model use-case interactions of an application.
  • Do not represent data in data store, although can access/update such data
Stateless Session Bean :
  • Do not maintain a conversational state for a particular client
  • Contain values only for the duration of the single invocation
  • The bean's state has no data for a specific client
  • Except during method invocation, all instances of stateless session bean are equivalent
  • In a single method invocation, the bean performs a generic task for all clients
Stateful Session Bean :
  • The bean's state represents the interaction between the bean and a specific client.
  • The bean needs to hold information about the client across method invocations.
  • The bean mediates between the client and the other components of the application, presenting a simplified view to the client.
  • Represents a client inside a J2EE server
  • Contains the conversational state of a client session is not retained when the session ends

Entities using JPA in EJB 3

  • An EJB 3.0 entity is a light-weight entity object that manages persistent data, performs complex business logic, potentially uses several dependent Java objects, and can be uniquely identified by a primary key
  • These represent persistent data stored in a relational database automatically using container managed transaction
  • The EJB 3.0 entity bean class is a non-abstract POJO class with implementations for the accessor/mutator methods in comparison to the EJB 2.1 entity bean class, which is abstract with abstract accessor/mutator methods
  • EJB 3.0 does not require component and home interfaces
  • The EntityManager class is used to create, find, and update an entity EJB.
  • In EJB 3.0,deployment descriptors are not required and are replaced with metadata annotations 
  • An entity models a business entity or models multiple actions within a business process

Need of Entities classes :

  • Persistent data represents object (Easier to use)
  • Become reusable components.
  • Functionality become more easy and powerful
    • Concurrency
    • Transactions
    • Security

Features of ORM

  • Objects, not tables : Applications should be written in terms of the domain model and not be bound to the relational model.
  • Convenience, not ignorance : The task of mapping will be and should be done by someone familiar with relational technology.
  • Unobtrusive, not transparent : It is unreasonable to expect that persistence be transparent since an application always needs to have control of the objects that it is persisting and be aware of the entity life cycle.
  • Legacy data, new objects : It is far more likely that an application will target an existing relational database schema instead of creating a new one.

Message Driven Bean (MDB)

  • Newly introduced from EJB 2.0
  • Acts as a consumer of asynchronous messages
  • Cannot be called directly by clients
  • Activated upon message arrival
  • No home or remote interface
  • Only Create the Bean class by implementing MessageDrivenBean and MessageListener interface
  • Clients interact with MDB by sending messages to the queues or topics to which they are listening
  • Stateless
  • All instances are equivalent
  • Allowing the EJB container to assign a message to any message-driven bean instance
  • Container can pool these instances to allow streams of messages to be processed concurrently.
  • Message driven bean can process messages from multiple clients

Characteristics of Message-Driven Bean

  • They execute upon receipt of a single client message.
  • They are invoked asynchronously.
  • They are relatively short-lived.
  • They do not represent directly shared data in the database, but they can access and update this data.
  • They can be transaction-aware.
  • They are stateless.

Services provided by EJB Container :

  • Persistence : EJB container manages the persistence of data for enterprise  that are declared as Entities
  • Transaction : EJB Container manages the transactions in the EJB application. A transaction is a set of operation that is executed as single unit
  • Security : EJB Container provides security by allowing only authorized user to access enterprise beans
  • Life Cycle management : An enterprise bean passes through various stages in its life cycle. EJB container is responsible for changing the state of an enterprise bean
  • Connection Pooling : EJB container creates a pool of database connections and allocates these to the Entities as and when required
  • Remote Client Connectivity : EJB container allows clients at remote locations to access methods of an enterprise bean stored in the J2EE Server
  • Interceptor : Aspect-oriented programming (AOP) is used in EJB in a very lightweight, accessible manner using interceptors. It is just like Filter of Servlet technology.
    • An interceptor is a method that you can interpose in the invocation flow of an enterprise b ea n.
    • The in terceptor method runs before any of the bean's business methods are invoked or you can define an interceptor to intercept lifecycle events for an enterprise bean.
    • The interceptor method runs as a callback method for the bean's lifecycle events.
    • In essence, interceptors give you a way to add functionality to your business methods without modifying the methods' code.
    • you can use an interceptor to validate parameters before they're passed to a business method, or perform security checks at the time the business method is called.
  • Timer : The EJB Timer Services are services that are provided by the container (or the ApplicationServer) and developers can take advantage of the timer services by registering one or more enterprise beans for time-based notification
    Two forms of Timer objects:
    • Single Action Timer : A single action timer (or a single interval timer) is a one which will expire only once
    • Interval Timer : Interval timer (or multiple action timer), as its name suggests will recur at multiple intervals of time. That is, these kinds of timer will have multiple expirations at regular intervals of time.
  • StateManagement :The EJB container manages components state transparently.
  • Messaging : A developer can write messaging components to send/receive message using JMS service through ConnectionFactory and Queue/Topic JNDI.

Previous Next

Comment by : Sarathi November 28th, 2012
Comment : Your tutorial is very nice... Pls complete site construction as soon as possible...

Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :