Understanding EJB




   By:
   Asha Pathik
   Aniruddha Ray
Saastha Infotech

Agenda

• Overview and Basic Concepts
  –   What is EJB?
  –   When to use EJB
  –   Containers
  –   Evolution of EJB
• EJB Components
  –   Types of Enterprise Beans
  –   Session Beans
  –   Entity Beans
  –   Message-driven Beans
• Creating an EJB
• Summary


                                  2
Saastha Infotech




Overview and Basic concepts
        What is EJB?




             3
Saastha Infotech

What is EJB ( Enterprise Java Beans )?

• EJB is the J2EE standard for developing and
  deploying server-side distributed components in
  java
• It defines a contract between components and
  application servers that enables any component to
  run on any compliant server
   –   it is the ubiquitous industry standard
   –   portability is possible
   –   rapid application development
   –   physically, EJB is two things
        • a specification
        • a set of Java interfaces



                                     4
Saastha Infotech




Overview and Basic concepts
       When to use EJB




             5
Saastha Infotech

When to use EJB

• For large scale applications where resources and
  data are distributed.
• When the application is running on different servers
  at many locations.
• Where scalability is critical.
• Where transactions are required to ensure data
  integrity
• When a variety of clients need to handled




                          6
Saastha Infotech




Overview and Basic concepts
Evolution of Enterprise JavaBeans




               7
Saastha Infotech

Evolution of Enterprise Java Beans – Part 1

• EJB release 1.0 focused on the following aspects:
   – defined the distinct “EJB roles” that are assumed by the component
     architecture
   – defined the client view of enterprise beans
   – defined the developer’s view of enterprise beans
   – defined the responsibilities of EJB Container provider and Server
     Provider
   – defined the format of ejb.jar file, EJB’s unit of deployment
• EJB release 1.1 augmented these with following:
   – provided better support for application assembly and deployment
   – specified in greater detail the responsibilities of the individual EJB
     roles


                                   8
Saastha Infotech

Evolution of Enterprise Java Beans – Part 2

• EJB release 2.0 focused on the following aspects:
   – defined message-driven bean and the integration with the JMS
   – provided local client view and support for efficient, lightweight
     access to EJB from local clients
   – defined new architecture for container persistence
   – support for management of relationships among entity beans
   – declarative query syntax for finder and select methods for entity
     beans with container-managed persistence
   – support for additional methods in home interface
   – support for run-as security identity
   – provided for network interoperability among servers




                                  9
Saastha Infotech

Evolution of Enterprise Java Beans – Part 3

• EJB release 2.1 focused on the following aspects:
   – enabling enterprise beans to implement and utilize web services
   – providing a container-managed timer services
   – enhancing EJB QL with additional ORDER BY and aggregate
     operators
   – enhancing the message-driven bean component type to other
     messaging types
• The EJB release 3.0 is focused on a simplification of the
  Enterprise JavaBeans architecture from the developer’s
  point of view.




                                10
Saastha Infotech




Overview and Basic concepts
         Containers




            11
Saastha Infotech

Containers

• Are the interface between a component and low-level
  platform-specific functionality that supports the
  component
   – components are simple set of programs that are capable of
     performing a particular business logic
• Provide configurable settings : like data accessibility
• Manages non-configurable settings like enterprise
  bean and servlet life cycles, database connection
  resource pooling, data persistence etc.
• Before a J2EE component can be executed, it must
  be assembled into J2EE application and deployed
  into its container


                                12
Saastha Infotech

EJB Components




                 13
Saastha Infotech




EJB Components
  Session Beans




       14
Saastha Infotech

Session Bean : Concepts

• A Session Bean is non-persistent object that
  implements some business logic.
   –   private to one client connection
   –   represents an interactive session
   –   not recoverable after system crash or shutdown!
   –   when client terminates, bean terminates i.e. no longer active
• A Stateful Session Bean has Conversational State
   – activation/ passivation of a Bean is possible
   – E.g. items reviewed in a session at some sites.
• A Stateless Session Bean has no Conversational State
   –   no activation or passivation
   –   pooling of stateless Session Beans by the container
   –   very efficient
   –   E.g. computing value using a formula.
                                   15
Saastha Infotech

Stateful Session Bean : Part(1)

Conversational Bean
• Consists of attributes and referenced objects :
   –   all non-transient attributes of the Bean
   –   referenced data in the database
   –   open connections to network
   –   references to other Beans
   –   etc.
• Exists during one client session
• A Conversation State is not persistent and is not
  automatically rolled back when a transaction fails!



                                    16
Saastha Infotech

Stateful Session Bean : Part(2)

• Passivation:
   – container serializes Bean instance and saves it to disk
   – preparation for Passivation is done with the method ejbPassivate:
       • close all existing connections
       • resolve all external references
• Activation:
   – de-serialize Bean instance from disk
   – afterwards, ejbActivate method is invoked to:
       • re-establish external references
       • re-establish connections
• If non-serializable objects are part of the state, these
  methods must be implemented in the Bean class

                                     17
Saastha Infotech

Stateful Session Bean : Life Cycle




                       18
Saastha Infotech

Stateless Session Bean

• All instances are equivalent
• Private to one client only during one call
• Pooled while not in use




                           19
Saastha Infotech




EJB Components
  Entity Beans




      20
Saastha Infotech

Entity Bean : Concept

• An Entity Bean represents an object-oriented view of
  some entities stored in a persistent, crash-resistant
  storage (usually a relational database)
   – persistent and transient (modifier transient) attributes are allowed,
     but only persistent attributes will be saved in database.
   – each entity bean typically has an underlying table in a RDBMS(
     business data), and each instance of the bean corresponds to a row
     in that table.
   – transactional and recoverable on a server crash.
• Shared among multiple clients ( no per-client state)
• Primary Key : a unique attribute of bean or a serializable
  class (Primary Key Class) with one or more attributes
• Two Beans with same Home Interface and same
  Primary Key are regarded as identical ( Bean Identity)
                                 21
Saastha Infotech

Persistence Management

• Bean-Managed Persistence (BMP) : any java-accessible
  data storage is possible
   – implements persistence mechanisms in ejbLoad resp. ejbStore
   – implements ejbActivate, ejbPassivate and ejbRemove
   – ejbCreate must return a ‘real’ primary key ( e.g. return pk;)
• Container-Managed Persistence (CMP) : Container
  controls access to (usually) a relational database
   – empty implementation of ejbLoad, ejbStore, ejbActivate,
     ejbPassivate and ejbRemove
   – ejbCreate returns ‘null’ as primary key ( return null)
   – Object-Relational Mapping between Bean attributes and database
     entries ( Vendor –specific)
   – more details later


                              22
Saastha Infotech


Primary Key

• A Primary Key of an Entity Bean consists of one or more
  persistent Bean attributes
   – values of primary key attributes must be unique for a Bean instance
   – primary key attributes must be serializable
• The primary key class …
   – is a serializable class with one or several attributes which ( together)
     acts as primary key of an Entity Bean
       • one can use e.g. Long or String for single primary key attributes or
         specific classes
       • all primary key attributes must be persistent Bean attributes
   – must be declared in the Bean’s Deployment Descriptor




                                     23
Saastha Infotech

Activation and Passivation

• The EJB Container can passivate unused Entity
  Beans
  – ejbPassivate is invoked
  – used resources ( e.g. database connections, references to other
    Beans etc. ) are freed
  – bean instance is pooled
• The Bean is activated when used again
  – an instance of this Bean is taken from the pool
  – ejbActivate is invoked
  – needed resources are occupied ( e.g. database connections are
    re-established)



                               24
Saastha Infotech

Entity Bean : Life Cycle




                       25
Saastha Infotech




EJB Components
Message-driven Beans




        26
Saastha Infotech

Message-Driven Bean – Concepts (1)

• Like a Stateless Session Bean, a Message-driven Bean
  only provides a piece of business logic without state
  management ( similar life cycle)
• A Message-driven bean must implement the interfaces
  javax.ejb.MessageDrivenBean and
  javax.ejb.MessageListener
   – MessageListener declares a method
     public void onMessage(Message msg)
     which will be invoked when message arrives
• A Message-driven Bean has no client-visible interfaces



                               27
Saastha Infotech

Message-Driven Bean – Concepts (2)

• A Message-driven bean is assigned to a JMS
  destination by means of the Deployment Descriptor
  element
   – message-driven destination
• The bean is invoked by the container, when a JMS
  message to this destination arrives




                                  28
Saastha Infotech

Message-Driven Bean – Concepts (3)

• A message driven bean is an enterprise bean that
  allows J2EE applications to process messages
  asynchronously
• It acts a JMS listener, which is similar to an event
  listener except that it receives messages instead of
  events
• The messages can be sent to any J2EE component
  or a non-J2EE system using JMS
• It retains no data or conversational state




                           29
Saastha Infotech

Contents of an EJB

• Interfaces: The remote and home interface for
  remote access. Local and local home accesses for
  local access.
• Enterprise bean class: Implements the methods
  defined in the above interfaces.
• Deployment descriptor: An XML file that specifies
  information about the bean such as its type,
  transaction attributes, etc.
• Helper classes: non-bean classes needed by the
  enterprise bean class such as utility and exception
  classes.




                          30

EJB 3.0 and J2EE

  • 1.
    Understanding EJB By: Asha Pathik Aniruddha Ray
  • 2.
    Saastha Infotech Agenda • Overviewand Basic Concepts – What is EJB? – When to use EJB – Containers – Evolution of EJB • EJB Components – Types of Enterprise Beans – Session Beans – Entity Beans – Message-driven Beans • Creating an EJB • Summary 2
  • 3.
    Saastha Infotech Overview andBasic concepts What is EJB? 3
  • 4.
    Saastha Infotech What isEJB ( Enterprise Java Beans )? • EJB is the J2EE standard for developing and deploying server-side distributed components in java • It defines a contract between components and application servers that enables any component to run on any compliant server – it is the ubiquitous industry standard – portability is possible – rapid application development – physically, EJB is two things • a specification • a set of Java interfaces 4
  • 5.
    Saastha Infotech Overview andBasic concepts When to use EJB 5
  • 6.
    Saastha Infotech When touse EJB • For large scale applications where resources and data are distributed. • When the application is running on different servers at many locations. • Where scalability is critical. • Where transactions are required to ensure data integrity • When a variety of clients need to handled 6
  • 7.
    Saastha Infotech Overview andBasic concepts Evolution of Enterprise JavaBeans 7
  • 8.
    Saastha Infotech Evolution ofEnterprise Java Beans – Part 1 • EJB release 1.0 focused on the following aspects: – defined the distinct “EJB roles” that are assumed by the component architecture – defined the client view of enterprise beans – defined the developer’s view of enterprise beans – defined the responsibilities of EJB Container provider and Server Provider – defined the format of ejb.jar file, EJB’s unit of deployment • EJB release 1.1 augmented these with following: – provided better support for application assembly and deployment – specified in greater detail the responsibilities of the individual EJB roles 8
  • 9.
    Saastha Infotech Evolution ofEnterprise Java Beans – Part 2 • EJB release 2.0 focused on the following aspects: – defined message-driven bean and the integration with the JMS – provided local client view and support for efficient, lightweight access to EJB from local clients – defined new architecture for container persistence – support for management of relationships among entity beans – declarative query syntax for finder and select methods for entity beans with container-managed persistence – support for additional methods in home interface – support for run-as security identity – provided for network interoperability among servers 9
  • 10.
    Saastha Infotech Evolution ofEnterprise Java Beans – Part 3 • EJB release 2.1 focused on the following aspects: – enabling enterprise beans to implement and utilize web services – providing a container-managed timer services – enhancing EJB QL with additional ORDER BY and aggregate operators – enhancing the message-driven bean component type to other messaging types • The EJB release 3.0 is focused on a simplification of the Enterprise JavaBeans architecture from the developer’s point of view. 10
  • 11.
    Saastha Infotech Overview andBasic concepts Containers 11
  • 12.
    Saastha Infotech Containers • Arethe interface between a component and low-level platform-specific functionality that supports the component – components are simple set of programs that are capable of performing a particular business logic • Provide configurable settings : like data accessibility • Manages non-configurable settings like enterprise bean and servlet life cycles, database connection resource pooling, data persistence etc. • Before a J2EE component can be executed, it must be assembled into J2EE application and deployed into its container 12
  • 13.
  • 14.
  • 15.
    Saastha Infotech Session Bean: Concepts • A Session Bean is non-persistent object that implements some business logic. – private to one client connection – represents an interactive session – not recoverable after system crash or shutdown! – when client terminates, bean terminates i.e. no longer active • A Stateful Session Bean has Conversational State – activation/ passivation of a Bean is possible – E.g. items reviewed in a session at some sites. • A Stateless Session Bean has no Conversational State – no activation or passivation – pooling of stateless Session Beans by the container – very efficient – E.g. computing value using a formula. 15
  • 16.
    Saastha Infotech Stateful SessionBean : Part(1) Conversational Bean • Consists of attributes and referenced objects : – all non-transient attributes of the Bean – referenced data in the database – open connections to network – references to other Beans – etc. • Exists during one client session • A Conversation State is not persistent and is not automatically rolled back when a transaction fails! 16
  • 17.
    Saastha Infotech Stateful SessionBean : Part(2) • Passivation: – container serializes Bean instance and saves it to disk – preparation for Passivation is done with the method ejbPassivate: • close all existing connections • resolve all external references • Activation: – de-serialize Bean instance from disk – afterwards, ejbActivate method is invoked to: • re-establish external references • re-establish connections • If non-serializable objects are part of the state, these methods must be implemented in the Bean class 17
  • 18.
  • 19.
    Saastha Infotech Stateless SessionBean • All instances are equivalent • Private to one client only during one call • Pooled while not in use 19
  • 20.
  • 21.
    Saastha Infotech Entity Bean: Concept • An Entity Bean represents an object-oriented view of some entities stored in a persistent, crash-resistant storage (usually a relational database) – persistent and transient (modifier transient) attributes are allowed, but only persistent attributes will be saved in database. – each entity bean typically has an underlying table in a RDBMS( business data), and each instance of the bean corresponds to a row in that table. – transactional and recoverable on a server crash. • Shared among multiple clients ( no per-client state) • Primary Key : a unique attribute of bean or a serializable class (Primary Key Class) with one or more attributes • Two Beans with same Home Interface and same Primary Key are regarded as identical ( Bean Identity) 21
  • 22.
    Saastha Infotech Persistence Management •Bean-Managed Persistence (BMP) : any java-accessible data storage is possible – implements persistence mechanisms in ejbLoad resp. ejbStore – implements ejbActivate, ejbPassivate and ejbRemove – ejbCreate must return a ‘real’ primary key ( e.g. return pk;) • Container-Managed Persistence (CMP) : Container controls access to (usually) a relational database – empty implementation of ejbLoad, ejbStore, ejbActivate, ejbPassivate and ejbRemove – ejbCreate returns ‘null’ as primary key ( return null) – Object-Relational Mapping between Bean attributes and database entries ( Vendor –specific) – more details later 22
  • 23.
    Saastha Infotech Primary Key •A Primary Key of an Entity Bean consists of one or more persistent Bean attributes – values of primary key attributes must be unique for a Bean instance – primary key attributes must be serializable • The primary key class … – is a serializable class with one or several attributes which ( together) acts as primary key of an Entity Bean • one can use e.g. Long or String for single primary key attributes or specific classes • all primary key attributes must be persistent Bean attributes – must be declared in the Bean’s Deployment Descriptor 23
  • 24.
    Saastha Infotech Activation andPassivation • The EJB Container can passivate unused Entity Beans – ejbPassivate is invoked – used resources ( e.g. database connections, references to other Beans etc. ) are freed – bean instance is pooled • The Bean is activated when used again – an instance of this Bean is taken from the pool – ejbActivate is invoked – needed resources are occupied ( e.g. database connections are re-established) 24
  • 25.
  • 26.
  • 27.
    Saastha Infotech Message-Driven Bean– Concepts (1) • Like a Stateless Session Bean, a Message-driven Bean only provides a piece of business logic without state management ( similar life cycle) • A Message-driven bean must implement the interfaces javax.ejb.MessageDrivenBean and javax.ejb.MessageListener – MessageListener declares a method public void onMessage(Message msg) which will be invoked when message arrives • A Message-driven Bean has no client-visible interfaces 27
  • 28.
    Saastha Infotech Message-Driven Bean– Concepts (2) • A Message-driven bean is assigned to a JMS destination by means of the Deployment Descriptor element – message-driven destination • The bean is invoked by the container, when a JMS message to this destination arrives 28
  • 29.
    Saastha Infotech Message-Driven Bean– Concepts (3) • A message driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously • It acts a JMS listener, which is similar to an event listener except that it receives messages instead of events • The messages can be sent to any J2EE component or a non-J2EE system using JMS • It retains no data or conversational state 29
  • 30.
    Saastha Infotech Contents ofan EJB • Interfaces: The remote and home interface for remote access. Local and local home accesses for local access. • Enterprise bean class: Implements the methods defined in the above interfaces. • Deployment descriptor: An XML file that specifies information about the bean such as its type, transaction attributes, etc. • Helper classes: non-bean classes needed by the enterprise bean class such as utility and exception classes. 30