Overview of EJB - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Overview of EJB

Description:

Beans contain business logic client developer makes thin (presentation ... Beans ... The Life Cycles of Enterprise Beans. Life cycles are managed by ... – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 65
Provided by: patrick99
Category:
Tags: ejb | beans | overview

less

Transcript and Presenter's Notes

Title: Overview of EJB


1
Overview of EJB
  • The core of transaction J2EE applications

2
What Well Cover
  • What Is an Enterprise Bean?
  • Benefits of Enterprise Beans
  • When to Use Enterprise Beans
  • Types of Enterprise Beans
  • The Contents of an Enterprise Bean (OOO)
  • Naming Conventions for Enterprise Beans (OOO)
  • The Life Cycles of Enterprise Beans (OOO)

3
What Is an Enterprise Bean?
  • Server side component that encapsulates some
    business logic
  • Business logic code that fulfills the
    applications purpose
  • Example EJBs implement inventory controls
  • checkInventoryLevel
  • orderProduct
  • Remote clients can access these services

4
Benefits of Enterprise Beans
  • Simplify the development of large distributed
    applications
  • EJB container provides system level services
    (like JTA and Security)
  • Beans contain business logic client developer
    makes thin (presentation only) clients
  • Beans are portable assemblers can put together
    new applications from existing beans

5
When to Use Enterprise Beans
  • If the application must be scalable (can be moved
    to multiple servers transparently)
  • Transactions are required to insure data
    integrity
  • Application requires a variety of clients

6
Types of Enterprise Beans
  • Session - Performs a task for a client
    implements a Web service
  • Entity - Represents a business entity object
    that exists in persistent storage
  • Message-Driven - Acts as a listener for the Java
    Message Service API, processing messages
    asynchronously

7
The Contents of an Enterprise Bean
  • The following files are required for EJBs
  • Deployment descriptor An XML file that specifies
    information about the bean such as its
    persistence type and transaction attributes.
  • Enterprise bean class Implements the methods
    defined in the following interfaces
  • Interfaces remote/local/Web service
  • Helper classes Other classes needed by the
    enterprise bean class, such as exception and
    utility classes

8
Naming Conventions for Enterprise Beans
  • Enterprise bean name (DD) EJB AccountEJB
  • EJB JAR display name (DD) JAR AccountJAR
  • Enterprise bean class Bean AccountBean
  • Home interface Home AccountHome
  • Remote interface Account
  • Local home interface LocalHome
    AccountLocalHome
  • Local interface Local AccountLocal
  • Abstract schema (DD) Account

9
The Life Cycles of Enterprise Beans
  • Life cycles are managed by the J2EE server
  • Life cycles vary between types of EJBs
  • Most life cycle events are reflected by call back
    methods in the EJBs

10
What We Covered
  • What Is an Enterprise Bean?
  • Benefits of Enterprise Beans
  • When to Use Enterprise Beans
  • Types of Enterprise Beans
  • The Contents of an Enterprise Bean (OOO)
  • Naming Conventions for Enterprise Beans (OOO)
  • The Life Cycles of Enterprise Beans (OOO)

11
Session Beans
  • EJB Workhorse

12
What Well Cover
  • What Is a Session Bean?
  • State Management Modes
  • When to Use Session Beans
  • The Life Cycle of a Stateful Session Bean (OOO)
  • The Life Cycle of a Stateless Session Bean (OOO)

13
What Is a Session Bean?
  • Representative of a single client on the server
  • Session bean performs work for its client
  • Similar to an interactive session it is not
    shared
  • It is not persistent

14
State Management Modes
  • Stateful/Stateless
  • Stateful instance variables (state) are
    maintained for the life of the session freed
    when terminated
  • Stateless instance variables maintain state
    only for the life of a single invocation
  • All instances equivalent any can go to any
    other client
  • Only stateless session beans can implement Web
    Services
  • May have better performance

15
When to Use Session Beans
  • Use a session bean if
  • one client at a time has access to instance.
  • The state of the bean is not persistent.
  • The bean implements a Web service.
  • Use a Stateful bean if
  • state represents the interaction between the bean
    and a specific client.
  • needs to hold information about the client across
    method invocations.
  • The bean mediates between the client and the
    other components, presenting a simplified view to
    the client.
  • the bean manages the work flow of several
    enterprise beans.
  • You can use stateless if
  • The bean's state has no data for a specific
    client.
  • the bean performs a generic task for all clients
    in a single invocation.
  • The bean fetches from a database a set of
    read-only data that is often used by clients.

16
The Life Cycle of a Stateful Session Bean
17
The Life Cycle of a Stateful Session Bean
(narrative)
  • client initiates the life cycle by invoking the
    create method
  • The EJB container instantiates bean and invokes
    the setSessionContext and ejbCreate methods
  • Bean is ready for business methods use (ready
    stage)
  • While ready the EJB container may decide to
    passivate the bean by moving it from memory to
    secondary storage.
  • ejbPassivate method invoked immediately before
    passivating
  • ejbActivate method called before moved back to
    ready
  • Client invokes remove method - container calls
    bean's ejbRemove
  • Instance eligible for garbage collection
  • Only create/remove called by client

18
The Life Cycle of a Stateless Session Bean
  • life cycle has just two stages nonexistent and
    ready for the invocation of business methods

19
What We Covered
  • What Is a Session Bean?
  • State Management Modes
  • When to Use Session Beans
  • The Life Cycle of a Stateful Session Bean (OOO)
  • The Life Cycle of a Stateless Session Bean (OOO)

20
Entity Beans
  • Persistently stored business objects

21
What Well Cover
  • What Is an Entity Bean?
  • What Makes Entity Beans Different from Session
    Beans?
  • Container-Managed Persistence
  • When to Use Entity Beans
  • The Life Cycle of an Entity Bean

22
What Is an Entity Bean?
  • A business object in persistent storage
  • Examples, customers, orders, products
  • In J2EE usually stored in RDBMS
  • Each bean may have an underlying table
  • Each instance may correspond to a row

23
What Makes Entity Beans Different from Session
Beans?
  • Entity beans are persistent
  • Exists beyond lifetime of application or J2EE
    server
  • Bean managed vs container managed
  • Entity Beans may be shared by clients
  • Since it may be changed, needs to work in
    transactions
  • Each has a primary key Entity bean with the
    same key is the same bean

24
Container-Managed Persistence
  • EJB container handles all database access
  • abstract schema defines the bean's persistent
    fields and relationships (name specified in DD)
  • persistent fields are stored in the underlying
    data store (the state of the bean)
  • relationship field is like a foreign key (defines
    a related bean)
  • Multiplicity one to one, one to many, many to
    one, many to many
  • Direction bidirectional or unidirectional
  • EJB QL often used to navigate across relationships

25
Abstract Schema (diagram)
26
CMR (Multiplicity)
  • One-to-one Each entity bean instance is related
    to a single instance of another entity bean.
  • One-to-many An entity bean instance related to
    multiple instances of the other entity bean.
  • Many-to-one Multiple instances of an entity bean
    related to a single instance of the other entity
    bean.
  • Many-to-many The entity bean instances related
    to multiple instances of each other

27
CMR - Direction
  • Bidirectional each has a reference to the other
  • Unidirectional one has a relationship field
    that refers to the other
  • EJB QL often used to negotiate relationships
    directions dictate its ability to do so.

28
When to Use Entity Beans
  • The bean represents a business entity, not a
    procedure (CreditCard, not CreditCardVerifier)
  • The bean's state must be persistent

29
The Life Cycle of an Entity Bean
30
The Life Cycle of an Entity Bean (narrative)
  • EJB container creates the instance
  • Calls setEntityContext passing context to bean
  • Moved to a pool of available instances
  • Identity assigned to instance when it moves to
    the ready stage
  • Two paths from pooled to ready
  • Client calls create container calls ejbCreate
    and ejbPostCreate
  • 2nd EJB container invokes the ejbActivate
  • Two paths from ready to pooled
  • Client calls remove container calls EJBRemove
  • EJB container may invoke the ejbPassivate method
  • End of life container calls unsetEntityContext
  • With bean managed primary key not set
    automatically must be done in ejbCreate and
    ejbActivate

31
What We Covered
  • What Is an Entity Bean?
  • What Makes Entity Beans Different from Session
    Beans?
  • Container-Managed Persistence
  • When to Use Entity Beans
  • The Life Cycle of an Entity Bean

32
Message Driven Beans
  • Always listening

33
What Well Cover
  • What Is a Message-Driven Bean?
  • What Makes Message-Driven Beans Different from
    Session and Entity Beans?
  • When to Use Message-Driven Beans
  • The Life Cycle of a Message-Driven Bean

34
What Is a Message-Driven Bean?
  • Allows J2EE applications to process messages
    asynchronously
  • Acts as a JMS listener (similar to an event
    listener)
  • Messages may be sent by Java client, J2EE server,
    other messaging system
  • Can process JMS messages or other kinds of
    messages

35
What Makes Message-Driven Beans Different?
  • Much like a stateless session bean
  • instances retain no data or conversational state
  • All instances are equivalent
  • single bean can process messages from multiple
    clients
  • Execute on receipt of client message in a queue
  • Asynchronously invoked
  • Short lived
  • May be transaction aware
  • When message arrives onMessage method called
  • If part of a transaction, on rollback, message
    will be redelivered

36
When to Use Message-Driven Beans
  • Session and entity beans only allow synchronous
    send/receive of messages (with MDB you shouldnt
    receive messages in them)
  • Only asynchronous means is MDB

37
The Life Cycle of a Message-Driven Bean
38
What We Covered
  • What Is a Message-Driven Bean?
  • What Makes Message-Driven Beans Different from
    Session and Entity Beans?
  • When to Use Message-Driven Beans
  • The Life Cycle of a Message-Driven Bean

39
EJB Client Types
  • Multiple uses for interfaces

40
What Well Cover
  • Defining Client Access with Interfaces
  • Remote Clients
  • Local Clients
  • Local Interfaces and Container-Managed
    Relationships
  • Deciding on Remote or Local Access
  • Web Service Clients
  • Method Parameters and Access

41
Defining Client Access with Interfaces
  • Does not apply to message beans
  • Access thru interfaces define client access
  • All else hidden (DD, implementations, etc.
  • Well designed interfaces
  • Shield clients from complexity
  • Allow change of implementation
  • Decide remote/local/web service interface

42
Remote Clients
  • Traits of a remote client
  • May run on different machine/JVM
  • May be web component, application client or EJB
  • Location of the EJB is transparent
  • Must have remote/home interface
  • Remote interface has business methods
  • Home interface has life cycle methods create
    /remove
  • Entity beans have finder methods

43
Remote Clients (diagram)
44
Local Clients
  • Traits of local clients
  • It must run in the same JVM as the enterprise
    bean it accesses.
  • It may be a Web component or another enterprise
    bean.
  • To the local client, the location of the
    enterprise bean it accesses is not transparent.
  • It is often an entity bean that has a
    container-managed relationship with another
    entity bean.
  • Must have local interface and local home
    interface for EJBs

45
Local Interfaces and Container-Managed
Relationships
  • Entity beans which are target of CMR must have a
    local interface
  • Direction of relationship determines whether it
    is a target bidirectional, both must have local
    interfaces
  • EJBs that are part of CMR must live in same EAR.

46
Deciding on Remote or Local Access
  • CMR requires local access
  • Tightly coupled beans candidate for local access
  • Type of client app clients always require
    remote. Web/EJB depends on distribution
  • Distributed scenarios allow remote access
  • Performance local quicker than remote
  • Flexibility
  • Can allow both remote and local interfaces

47
Web Service Clients
  • Two ways to access
  • JAX-RPC Java xml remote procedure calls
  • Invoke business methods on stateless session
    beans
  • Correct protocols (SOAP, HTTP, WSDL) independent
    of client implementation
  • Accessed through the Web service endpoint
    interface
  • Not accompanied by a home interface

48
Method Parameters and Access
  • Isolation
  • Clients and bean operate on different copies
  • Protects from client mod of data
  • Even though local clients can modify same copy
    dont depend on it may need to make remote
  • Granularity of data
  • Remote calls should have coarser grained data

49
What We Covered
  • Defining Client Access with Interfaces
  • Remote Clients
  • Local Clients
  • Local Interfaces and Container-Managed
    Relationships
  • Deciding on Remote or Local Access
  • Web Service Clients
  • Method Parameters and Access

50
EJB Query Language
51
What Well Cover
  • Terminology
  • Simplified Syntax
  • Example Queries
  • Full Syntax
  • EJB QL Restrictions

52
Terminology
  • Abstract schema definition of persistent fields
    and relationships
  • Abstract schema name logical name of schema
  • Abstract schema type all ejbql evaluates to a
    type- if a schema name then the local interface

53
Terminology (continued)
  • Backus-Naur Form (BNF) notation for describing
    syntax
  • Navigation traversing relationship .
  • Path expression expression that navigates to a
    related entity bean
  • Persistent field virtual field for entity bean
    stored in DB
  • Relationship field virtual field defining a
    related entity bean

54
Simplified Syntax
  • EJB QL query has four clauses SELECT, FROM,
    WHERE, and ORDER BY
  • High level BNF - EJB QL select_clause
    from_clause where_clauseorderby_clause
  • Select defines type of object
  • From defines scope of query
  • Where conditional that restricts objects selected
  • Order By return in a particular order

55
Example Queries - Simple Finder Queries
  • SELECT OBJECT(p) FROM Player p
  • Data retrieved All players
  • Finder method findall()
  • The FROM clause declares an identification
    variable named p, omitting the optional keyword
    AS
  • Player element is the abstract schema name of the
    PlayerEJB returns LocalPlayer from
    LocalPlayerHome

56
Example Queries - Simple Finder Queries
(continued)
  • SELECT DISTINCT OBJECT(p) FROM Player p WHERE
    p.position ?1
  • Data retrieved players with position specified
    by parameter
  • Finder method findByPosition(String position)
  • Description OBJECT keyword must precede a
    stand-alone variable p. DISTINCT keyword
    eliminates duplicate values. Where restricts
    player to position ?1 value.

57
Example Queries - Simple Finder Queries
(continued)
  • SELECT DISTINCT OBJECT(p) FROM Player p WHERE
    p.position ?1 AND p.name ?2
  • Data retrieved The players with the specified
    position and name.
  • Finder method findByPositionAndName(String
    position, String name)
  • Description position and name elements are
    persistent fields of the PlayerEJB entity bean

58
Example Queries - Queries That Navigate to
Related Beans
  • SELECT DISTINCT OBJECT(p) FROM Player p, IN
    (p.teams) AS t WHERE t.city ?1
  • Data retrieved The players whose teams belong to
    the specified city.
  • Finder method findByCity(String city)
  • Description p variable represents the PlayerEJB
    and t represents related TeamEJB
  • P.teams navigation operator collection op and
    terminal
  • t. used as single instance

59
Example Queries -Queries That Navigate to
Related Beans
  • SELECT DISTINCT OBJECT(p) FROM Player p, IN
    (p.teams) AS t WHERE t.league ?1
  • Data retrieved The players that belong to the
    specified league.
  • Finder method findByLeague(LocalLeague league)
  • Description as above, but parameter is object

60
Example Queries -Queries That Navigate to Related
Beans
  • SELECT DISTINCT OBJECT(p) FROM Player p, IN
    (p.teams) AS t WHERE t.league.sport ?1
  • Data retrieved The players who participate in
    the specified sport.
  • Finder method findBySport(String sport)
  • Description sport persistent field belongs to
    the LeagueEJB bean

61
Example Queries - Other Conditional Expressions
  • SELECT OBJECT(p) FROM Player p WHERE p.teams IS
    EMPTY
  • Data retrieved All players who do not belong to
    a team.
  • Finder method findNotOnTeam()
  • Description If a player does not belong to a
    team, then the teams collection is empty and the
    conditional expression is TRUE

62
Example Queries - Other Conditional Expressions
  • SELECT DISTINCT OBJECT(p) FROM Player p WHERE
    p.salary BETWEEN ?1 AND ?2
  • Data retrieved The players whose salaries fall
    within the range of the specified salaries.
  • Finder method findBySalaryRange(double low,
    double high)
  • Description BETWEEN expression has three
    arithmetic expressions a persistent field
    (p.salary) and the two input parameters (?1 and
    ?2).
  • Equals p.salary ?1 AND p.salary

63
Example Queries - Other Conditional Expressions
  • SELECT DISTINCT OBJECT(p1) FROM Player p1, Player
    p2 WHERE p1.salary p2.salary AND p2.name ?1
  • Data retrieved All players whose salaries are
    higher than the salary of the player with the
    specified name.
  • Finder method findByHigherSalary(String name)
  • Description two identification variables (p1 and
    p2) of the same type (Player). Two identification
    variables are needed because the WHERE clause
    compares the salary of one player (p2) with that
    of the other players (p1).

64
What We Covered
  • Terminology
  • Simplified Syntax
  • Example Queries
  • Full Syntax
  • EJB QL Restrictions
Write a Comment
User Comments (0)
About PowerShow.com