Enterprise Java Bean
Enterprise Java Beans (EJB) is a framework for creating reusable components that manage business logic in large-scale applications. These components are typically managed by application servers like Glassfish, which provide services such as transaction management, security, and concurrency control. EJB simplifies the development of enterprise applications by offering a standardized approach to managing business logic and interacting with enterprise resources such as databases and messaging systems. By abstracting away many low-level details, developers can focus on writing business logic rather than dealing with infrastructure-level code.
An Enterprise Bean (EJB) is a server-side component that encapsulates the business logic of an application, excluding tasks related to presentation or direct database operations. The management of Enterprise Beans is handled by an EJB container, which takes care of tasks like object registration, providing remote interfaces, instance creation and destruction, security checks, managing object states, and coordinating distributed transactions.
There are three types of Enterprise Java Beans:
Session Bean: Session beans encapsulate business logic and can be invoked by various types of clients, including local, remote, and web service clients. Their lifecycle, including creation, activation, passivation, and removal, is managed by the application server. Session beans play a crucial role in Java EE applications, offering a modular and scalable approach to encapsulating business logic and serving different types of clients.
Local Client: These clients exist within the same application or server as the session bean and communicate with it directly through method calls.
Remote Client: These clients access the session bean from a different application or server, communicating over a network using protocols like Remote Method Invocation (RMI).
Web Service Clients: These clients communicate with the session bean using web services, typically through protocols like Representational State Transfer (REST) or Simple Object Access Protocol (SOAP).
There are three subtypes of session beans:
Stateful Session Bean: Maintains conversational state with clients across multiple method invocations, allowing them to maintain context and continuity throughout the session.
Stateless Session Bean: Does not maintain conversational state and performs specific tasks without holding information about previous invocations.
Singleton Session Bean: Creates and shares a single instance of a bean across all clients within the application, following the singleton design pattern.
Message-Driven Bean (MDB): An MDB handles messages asynchronously in Java EE applications. Unlike session beans, which are invoked directly, MDBs are triggered by incoming messages from messaging systems like the Java Message Service (JMS). They contain business logic specifically designed to process messages independently of the main application flow. MDBs are commonly used when applications need to asynchronously handle messages from message-oriented middleware, facilitating scalable and efficient processing.
Entity Bean: An Entity Bean is a server-side Java EE component that acts as a bridge between the application and the database. It handles tasks such as storing, retrieving, updating, and deleting data. Each entity bean corresponds to a row in a database table and follows specific rules and mappings to ensure that data is stored and accessed correctly.
Subscribe to my newsletter
Read articles from Udayaraj Subedi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Udayaraj Subedi
Udayaraj Subedi
Software Engineer