Enterprise beans

15
EJB 3.1 Overview Vladan Pulec

description

 

Transcript of Enterprise beans

Page 1: Enterprise beans

EJB 3.1 OverviewVladan Pulec

Page 2: Enterprise beans

Agenda EJB features and functionality Brief demo

Page 3: Enterprise beans

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

Page 4: Enterprise beans

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

Page 5: Enterprise beans

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)

Page 6: Enterprise beans

Session Façade Exampleweb EJB

Entity ClassesSession BeansClient

Client

Client

FinanceSessionBean

PaymentSessionBean

Order

Payment

Credit Card

Authorization

Database

Page 7: Enterprise beans

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)

Page 8: Enterprise 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

Page 9: Enterprise beans

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

Page 10: Enterprise beans

EJB Components Session Beans

Stateless Stateful Singleton

Message-Driven Beans

Page 11: Enterprise 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

Page 12: Enterprise beans

Difference between stateless and stateful session beans

Page 13: Enterprise 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

Page 14: Enterprise beans

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

Page 15: Enterprise beans

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