enterprise beans
DESCRIPTION
TRANSCRIPT
EJB 3.1 OverviewVladan Pulec
Agenda EJB features and functionality Brief demo
New Features
• Previous versions of EJBs suffered from:– Heavyweight programming model– Direct use of JNDI– Verbose XML descriptor
• EJB 3 eliminates this by:– Metadata annotations– Minimal deployment descriptors (optional)– Dependency injection
Component Types
EJB Technology
Session(Stateless, stateful,
singleton)Message-Driven
Session EJB Components:• Business logic running within an EJB
Component
Message-Driven Beans:• Asynchronous message consumer• Different operational characteristics from
session beans
Entity EJBs:• replaced by JPA (Java Persistence API)• Supported only for backward compatibility
EJB Tiers
• Session beans act as a façade for the entity components:– Minimizes network overhead– Clearer separation of concerns
• Design pattern (not mandated by Java EE specification)– Service tier (mostly session beans and message-driven
beans)– Object-relational mapping tier (entity classes and
related supporting classes)
Session Façade Exampleweb EJB
Entity ClassesSession BeansClient
Client
Client
FinanceSessionBean
PaymentSessionBean
Order
Payment
Credit Card
Authorization
Database
EJB Component Model Components are encapsulated within the EJB
container Container provides proxies to allow limited access to
the components Clients make calls to the interfaces (container
exposes proxies, not the actual beans) Container provides life-cycle management, security,
resource management, and transaction management for the components
Container also provides timer services and monitors message queue (for message-driven beans)
EJB Container
• Resource management– Encapsulates access to external resources– Connection pooling
• Life-cycle management• Isolates implementation classes from clients• Timer services
– scheduled work• Monitors message queue
Embedded EJB Container (EJB lite) Subset of EJB functionality Added in Java EE 6 Runs outside of an EJB container (using SDK only)
No application server required Supports stateless, stateful, and singleton
components Local and no interface only Synchronous invocation only Container-managed security and transactions Declarative and programmatic security
EJB Components Session Beans
Stateless Stateful Singleton
Message-Driven Beans
Timer Service
• Container invokes either a stateless session bean or Message-Driven bean– Use timer service in the bean to configure the
timer• Injected
• Via context
– Configure timing– Configure timeout method
web EJB
Timer Serv ice EJB
Setup Timer
Invoke Timeout method
Difference between stateless and stateful session beans
EJB Objects
• EJB components are never called directly by clients
• Clients get a reference to the component via EJBObject, which acts as a proxy– Stateful beans require separate proxies for each
call– Stateless beans share the same proxy
Anatomy of a session bean
• Consists of an interface and implementation class
• Must be a concrete class (cannot be final or abstract)
• Must have no argument constructor• Cannot start with “’ejb”• Business and life-cycle methods
Session bean best practices
• Choose bean type carefully– Stateless session beans fit most of the needs
• Examine interface types– Remote interface require network access and will be slow.
Use local in using within the same JVM• For DI, make sure not to inject stateful session bean into
a stateless bean• Examine what kind of data will be stored in a
conversational state (stateful beans)– Use primitive variables rather than large nested composite
objects