Enterprise Java Beans - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Enterprise Java Beans

Description:

Server-side component that encapsulates the business logic of an application. ... Represent a single client inside the application server ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 36
Provided by: quinn5
Category:
Tags: beans | enterprise | java | snell

less

Transcript and Presenter's Notes

Title: Enterprise Java Beans


1
Enterprise Java Beans
2
Web Applications
  • Web Tier or Presentation Tier
  • JSP, servlet, HTML
  • Views
  • Business Tier
  • Controllers
  • Business logic
  • Integration Tier
  • Model

Presentation Tier
Business Tier
Integration Tier
3
Business Tier
  • Controller (MVC)
  • Business Logic
  • Business Methods
  • Data Transformations

4
What didnt we address?
  • Scalability
  • How do we scale servlets beans, etc?
  • Distributed resources
  • How do we access?
  • Component Reuseability

5
Enterprise Java Beans
  • Not Java beans!
  • Server-side component that encapsulates the
    business logic of an application.
  • i.e. orderProduct, transfer, deposit
  • Can be accessed remotely
  • Location transparent
  • Run inside a Container

6
EJB Container
  • Loads java objects
  • Provides for
  • Remote access
  • Security
  • Resource pooling
  • Persistence
  • Concurrency
  • Transaction control

Client
container
Interceptor
bean
bean
bean
bean
7
Why use EJBs?
  • Simplify the development of large, distributed
    applications
  • Container provides many services
  • Beans contain application logic
  • Separated from presentation
  • Particularly important for small devices
  • EJBs are portable
  • Use for new apps easily

8
EJB Types
  • Business Tier
  • Session
  • Stateless
  • Stateful
  • Message-Driven
  • Integration Tier
  • Entity

9
Session Beans
  • Represent a single client inside the application
    server
  • Clients access the application by invoking a
    session beans methods
  • The session bean performs the work
  • Shields client from complexity by executing
    business tasks inside the application server

10
Session Beans
  • Similar to an interactive session
  • Not shared - only one client
  • Not persistent
  • When the client terminates its connection,
  • The session bean appears to terminate
  • And is no longer associated with the client

11
Stateful Session Beans
  • State consists of the values of instance
    variables
  • Often called the conversational state
  • State is retained for the duration of the session
  • Disappears when client terminates connection

12
Stateful Session Beans
  • Stateful session beans are appropriate if
  • A bean needs to be associated with a particular
    client through the life of the connection
  • The bean needs to hold information between method
    invocations
  • The bean manages work-flow of several enterprise
    beans
  • The bean mediates between the client and other
    components of the application

13
Stateful Session Bean Life Cycle
14
Stateless Session Beans
  • Does not maintain conversational state
  • When method is finished, state is not retained
  • All instances of stateless beans are equivalent
  • Pooled
  • Can support multiple clients
  • Can be used to implement a web service

15
Stateless Session Beans
  • Choose session beans if
  • No direct association between a client and a
    specific bean
  • The bean performs a self-contained and generic
    task
  • All the information the bean needs can be passed
    as arguments to the method
  • Example
  • Transfer(amount, account1, account2)
  • MailConfirmation(ordernumber, emailaddress)

16
Stateless Session Bean Life Cycle
17
Message-Driven Beans
  • Responds to and processes messages
  • Normally acts as a JMS listener
  • Sent by any Java EE component
  • Can also process other types of messages
  • Not accessed through interfaces
  • When the message arrives, the container calls the
    beans onMessage method.

18
Message-Driven Beans
  • Resembles a stateless session bean
  • Instances retain no data or conversational state
  • All instances are equivalent.
  • Container can assign a message to any instance
  • Container can pool instances
  • A single bean can process messages from multiple
    clients
  • However, message-driven beans are asynchronous

19
Message-Driven Bean Life Cycle
20
Entity Beans
  • A lightweight persistence domain object
  • Represents a table in a relational database
  • Each instance represents a row in the table
  • Persistent state is represented through
    persistent fields or properties
  • Object/relational mappings
  • Plain Old Java Object (POJO) with annotations

21
Entity Beans
  • Managed by an EntityManager
  • The EntityManager API creates and removes
    persistent entity instances, finds entities by
    the entity's primary key, and allows queries to
    be run on entities.
  • EntityManagers may be container controlled or
    application controlled

22
Questions to Ponder...
  • Entity bean, stateful session bean, stateless
    session bean?
  • object which requires access by multiple clients
    over its lifetime
  • object whose work accepts and returns all
    information in the call
  • object that must exist after server crash
  • object that must exist between method invocations
  • object that encapsulates a set of client actions
    to multiple beans

23
Example Session BeanRemote Interface
package com.sun.tutorial.javaee.ejb import
java.math.BigDecimal import javax.ejb.Remote _at_R
emote() public interface Converter public
BigDecimal dollarToYen(BigDecimal
dollars) public BigDecimal yenToEuro(BigDecimal
yen)
24
Example Session BeanImplementation
package com.sun.tutorial.javaee.ejb import
java.math.BigDecimal import javax.ejb. _at_Statel
ess() public class ConverterBean implements
Converter private BigDecimal yenRate new
BigDecimal("115.3100") private BigDecimal
euroRate new BigDecimal("0.0071") public
BigDecimal dollarToYen(BigDecimal dollars)
BigDecimal result dollars.multiply(yenRate
) return result.setScale(2, BigDecimal.ROUND_UP
) public BigDecimal yenToEuro(BigDecimal
yen) BigDecimal result yen.multiply(euroRa
te) return result.setScale(2,
BigDecimal.ROUND_UP)
25
package com.sun.tutorial.javaee.ejb import
java.math.BigDecimal import javax.ejb.EJB publi
c class ConverterClient _at_EJB private static
Converter converter public ConverterClient(Stri
ng args) public static void main(String
args) ConverterClient client new
ConverterClient(args) client.doConversion()
public void doConversion() try
BigDecimal param new BigDecimal("100.00")
BigDecimal yenAmount converter.dollarToYen(para
m) System.out.println("" param " is "
yenAmount " Yen.") BigDecimal
euroAmount converter.yenToEuro(yenAmount)
System.out.println(yenAmount " Yen is "
euroAmount " Euro.")
System.exit(0) catch (Exception ex)
System.err.println("Caught an
unexpected exception!") ex.printStackTrace(
)
26
The Session Bean Interface
  • A plain Java interface that defines all the
    business methods of the bean
  • The interface is assumed _at_Local unless defined as
    _at_Remote
  • _at_Remote interface methods must have arguments and
    return types that are valid RMI types

27
package com.sun.tutorial.javaee.ejb import
java.util.Listimport javax.ejb.Remote _at_Remote p
ublic interface Cart public void
initialize(String person) throws
BookException public void initialize(String
person, String id) throws BookException publi
c void addBook(String title) public void
removeBook(String title) throws
BookException public ListltStringgt
getContents() public void remove()
28
Session Bean Implementation
  • Must implement the interface
  • Must be annotated _at_Stateful or _at_Stateless
  • _at_Stateful session beans may also
  • Implement lifecycle callback methods
    _at_PostConstruct, _at_PreDestroy, _at_PostActivate,
    _at_PrePassivate
  • Implement any optional _at_Remove methods

29
package com.sun.tutorial.javaee.ejb import
java.util.ArrayList import java.util.List import
javax.ejb.Remove import javax.ejb.Stateful _at_St
ateful public class CartBean implements Cart
String customerName String
customerId ListltStringgt contents public void
initialize(String person) throws BookException
if (person null) throw new
BookException("Null person not allowed.")
else customerName person
customerId "0" contents new
ArrayListltStringgt()
30
public ListltStringgt getContents()
return contents _at_Remove public void
remove() contents null
31
Lifecycle Callback Methods
  • Must be public, return void, and have no
    parameters
  • _at_PostConstruct methods
  • invoked by the container on newly constructed
    bean instances after all dependency injection has
    completed and before the first business method is
    invoked on the enterprise bean.
  • _at_PreDestroy methods
  • invoked after any method annotated _at_Remove has
    completed, and before the container removes the
    enterprise bean instance.

32
Lifecycle Callback Methods
  • _at_PrePassivate methods
  • invoked by the container before the container
    passivates the enterprise bean, meaning the
    container temporarily removes the bean fom the
    environment and saves it to secondary storage.
  • _at_PostActivate methods
  • invoked by the container after the container
    moves the bean from secondary storage to active
    status.

33
Business Methods
  • The signature of a business method must conform
    to these rules
  • The method name must not begin with ejb
  • The access control modifier must be public.
  • If the bean allows remote access, the arguments
    and return types must be legal RMI types.
  • If the bean is a web service endpoint, the
    arguments and return types for the methods
    annotated _at_WebMethod must be legal types for
    JAX-WS.
  • The modifier must not be static or final.
  • In case of system-level errors, throw
    javax.ejb.EJBException

34
Building Packaging
  • Enterprise Java Beans reside in jar files

35
Naming Conventions
Write a Comment
User Comments (0)
About PowerShow.com