Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
EJB 3.0 and J2EE
-
Upload
aniruddha-ray -
Category
Technology
-
view
1.902 -
download
3
description
Transcript of EJB 3.0 and J2EE
UnderstandingUnderstanding EJBEJB
By:
Asha Pathik
Aniruddha Ray
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
Saastha Infotech
2
Overview and Basic concepts What is EJB?
Saastha Infotech
3
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
Saastha Infotech
4
Overview and Basic concepts When to use EJB
Saastha Infotech
5
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
Saastha Infotech
6
Overview and Basic concepts Evolution of Enterprise JavaBeans
Saastha Infotech
7
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
Saastha Infotech
8
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
Saastha Infotech
9
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.
Saastha Infotech
10
Overview and Basic concepts Containers
Saastha Infotech
11
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
Saastha Infotech
12
EJB Components Saastha Infotech
13
EJB Components
Session Beans
Saastha Infotech
14
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.
Saastha Infotech
15
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!
Saastha Infotech
16
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
Saastha Infotech
17
Stateful Session Bean : Life Cycle
Saastha Infotech
18
Stateless Session Bean
• All instances are equivalent
• Private to one client only during one call
• Pooled while not in use
Saastha Infotech
19
EJB Components
Entity Beans
Saastha Infotech
20
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)
Saastha Infotech
21
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
Saastha Infotech
22
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
Saastha Infotech
23
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)
Saastha Infotech
24
Entity Bean : Life Cycle
Saastha Infotech
25
EJB Components
Message-driven Beans
Saastha Infotech
26
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
Saastha Infotech
27
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
Saastha Infotech
28
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
Saastha Infotech
29
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.
Saastha Infotech
30