Enterprise Java Beans (EJB)
Background
Enterprise Java Beans (EJB) have been part of Java EE since 1999, but it wasn't until the release of EJB version 3.0 (part of Java EE 5) in 2006 that they really became easy to use. The older versions had used a complicated set of local and remote interfaces to manage access to EJBs. Version 3 made EJBs out of POJOs (Plain Old Java Objects), considerably simplifying things.
EJBs are server-side components and require an EJB container in which to operate. The container provides the management structure for creating, naming, sharing and shutting down EJBs. Without a container, the programmer would have to write a lot of code to do this (and other things) themselves. The container automates this (easing development by performing default - common case - configuration automatically). GlassFish is the reference implementation for the EJB specification.
EJBs are closely tied with the Java Persistence API (JPA) and provide many of the facilities that a JPA application needs to run.
The EJB container is a runtime environment that provides a number of core features that are common to many applications (see container functionality below)
What do I need to know about EJBs?
EJB basics
- Types of EJB
(and their annotations)
- Session beans
- Stateless (@Stateless)
- Stateful (@Stateful)
- Singleton (@Singleton)
- Message beans
- Dependency injection: accessing an EJB within another component of an application (@EJB)
- Stateless beans as a means of implementing business methods
- Stateful beans as a means of implementing tasks that need to be done in several steps (e.g. multiple HTTP requests from client)
- @Remove annotation for methods that conclude the task
- Callbacks: @PostConstruct, @PreDestroy, @PrePassivate, @PostActivate
- Singleton beans to provide single application-wide services
- Initialising singletons; chaining singletons
EJB details
- The SessionContext object
- The TimerService
- Using an XML deployment descriptor instead of, or to override, annotations
- EJB interface views
- Local interface
- Remote interface
- No-interface view
- Interceptors
- @AroundInvoke
- Method interceptors
- Container-managed transaction support (6 types provide fine-tunable control)
- Marking a transaction for rollback
- Exception handling
- Security
- Principals and roles
- Authentication vs Authorisation
- Declarative security: @RolesAllowed, @RunAs and other annotations
- Remote client communication
- Dependency injection
- State management
- Pooling beans
- Component life cycle
- Messaging
- Transaction management
- Security
- Concurrency support
- Interceptors (aspect-oriented programming)
- Asynchronous method invocation
Reading
- Goncalves A, Beginning Java EE 7 , Apress, 2013.
- David R. Heffelfinger, D R, Java EE 6 Development with NetBeans 7, Packt, 2011 (Chapter 7)
- Keith M, Schincariol M, Pro EJB 3 - Java Persistence API, Apress, 2006