Enterprise beans

573 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Enterprise beans

  1. 1. EJB 3.1 Overview Vladan Pulec
  2. 2. Agenda  EJB features and functionality  Brief demo
  3. 3. 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
  4. 4. 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
  5. 5. 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)
  6. 6. Session Façade Example web EJB Entity ClassesSession Beans Client Client Client FinanceSessionBean PaymentSessionBean Order Payment Credit Card Authorization Database
  7. 7. 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)
  8. 8. 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
  9. 9. 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
  10. 10. EJB Components  Session Beans Stateless Stateful Singleton  Message-Driven Beans
  11. 11. 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
  12. 12. Difference between stateless and stateful session beans
  13. 13. 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
  14. 14. 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
  15. 15. 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

×