EJB

Comprehensive study notes, diagrams, and exam preparation for EJB.

Enterprise JavaBeans (EJB)

Definition

Enterprise JavaBeans (EJB) is a server-side component architecture for the Java Platform, Enterprise Edition (Java EE). It provides a standardized framework for building scalable, secure, and transactional enterprise-level applications by abstracting complex low-level middleware tasks like security, concurrency, and persistence.


Main Content

1. Session Beans

  • Stateless Session Beans: These do not maintain any client-specific state between method calls. They are highly scalable and used for generic tasks like sending an email or performing calculations.
  • Stateful Session Beans: These maintain conversational state with a specific client. If a user is performing a multi-step process like an online shopping cart, the bean remembers the user’s previous actions.

2. Message-Driven Beans (MDB)

  • Asynchronous Processing: Unlike session beans, MDBs are triggered by messages sent to a queue or topic rather than direct method calls.
  • Decoupling: They allow the sender of the message to continue its work without waiting for the recipient to process the request, which is vital for high-traffic systems.

3. EJB Container

  • Runtime Environment: The container acts as a bridge between the EJB and the underlying server. It handles system-level services such as transaction management, dependency injection, and security.
  • Isolation: Developers focus on business logic while the container manages the plumbing, ensuring the application remains robust.
[Client] -> [EJB Container] -> [Session Bean / MDB] -> [Database]
                 |
        (Manages Transactions, 
         Security, Pooling)

Working / Process

1. Client Request

  • The client (e.g., a web browser or another service) invokes a method on an EJB interface.
  • The request is intercepted by the EJB Container rather than going directly to the bean instance.

2. Container Interception

  • The container checks security credentials to ensure the user is authorized.
  • It starts a transaction if the bean method is marked with transactional requirements, ensuring data integrity.

3. Business Logic Execution

  • The container selects an available bean instance from a pre-managed pool or creates a new one.
  • The specific business logic is executed, and the result is returned through the container back to the client.

Advantages / Applications

  • Transactional Integrity: EJBs support Container-Managed Transactions (CMT), which automatically handle complex database rollback and commit operations, preventing data corruption.
  • Scalability and Pooling: By managing a pool of bean instances, the EJB container handles high volumes of concurrent requests efficiently, reducing the overhead of object creation.
  • Distributed Computing: EJB supports RMI (Remote Method Invocation), allowing components to reside on different servers while communicating seamlessly as if they were local.

Summary

EJB is a powerful component-based architecture designed to simplify the development of large-scale enterprise Java applications. By offloading resource-intensive tasks like transaction management, security, and persistence to the EJB container, developers can focus exclusively on writing business logic. Key terms to remember include Session Beans (for business flow), Message-Driven Beans (for async tasks), EJB Container (the runtime environment), and Transactions (for data consistency).