Enterprise JavaBeans (EJB): A Guide to Developing Enterprise Applications
This comprehensive guide explores Enterprise JavaBeans (EJB), a server-side component architecture for building distributed, enterprise-level Java applications. Learn about the different types of enterprise beans and how EJB simplifies the development of robust and scalable applications.
Enterprise Java Beans (EJB) Interview Questions
What is EJB?
Question 1: What is EJB?
EJB (Enterprise JavaBean) is a server-side component architecture for developing distributed, enterprise-level applications in Java. EJB components run within an application server and provide services like transaction management, security, and persistence.
Types of Enterprise Beans
Question 2: Types of Enterprise Beans
Three main types:
- Session Beans: Encapsulate business logic; stateless, stateful, or singleton.
- Message-Driven Beans (MDBs): Asynchronous message processing.
- Entity Beans: Represent persistent data (largely replaced by JPA in modern EJB).
Session Beans
Question 3: Session Beans
Session beans encapsulate business logic. They are not persistent.
Stateless Session Beans
Question 4: Stateless Session Beans
Stateless session beans do not maintain conversational state between client requests. Each method call is independent.
Learn More About Stateless Session Beans
Creating Stateless Session Beans
Question 5: Creating a Stateless EJB
Steps:
- Create a local interface (using `@Local` annotation).
- Create a remote interface (using `@Remote` annotation, if needed for different JVMs).
- Implement the bean class (using `@Stateless` annotation).
Stateful Session Beans
Question 6: Stateful Session Beans
Stateful session beans maintain conversational state across method calls from the same client. Data persists between requests from the same client.
Learn More About Stateful Session Beans
Singleton Session Beans
Question 7: Singleton Session Beans
Singleton session beans create only one instance for the application. The bean instance exists for the lifetime of the application.
JMS (Java Message Service)
Question 8: JMS (Java Message Service)
JMS is a messaging standard for creating, sending, and receiving messages asynchronously. It decouples applications and supports reliable messaging.
Advantages of JMS
Question 9: Advantages of JMS
Advantages:
- Asynchronous communication.
- Reliable message delivery.
- Loose coupling of applications.
PTP (Point-to-Point) Model
Question 10: PTP (Point-to-Point) Messaging Model
In the PTP (Point-to-Point) model, one message is delivered to only one consumer. A queue acts as an intermediary.
Pub/Sub (Publish/Subscribe) Model
Question 11: Pub/Sub (Publish/Subscribe) Messaging Model
In the Pub/Sub model, messages are broadcast to multiple consumers (subscribers). A topic acts as an intermediary.
Message-Driven Beans (MDBs)
Question 12: Message-Driven Beans (MDBs)
MDBs (Message-Driven Beans) are components designed to handle asynchronous messages. They're often used with JMS to process messages from queues or topics.
Entity Beans
Question 13: Entity Beans
Entity beans represent persistent data (database records). They're largely replaced by JPA (Java Persistence API) in modern EJB.
Session Facade
Question 14: Session Facade
Session Facade is a design pattern that simplifies access to enterprise beans. It provides a single interface to clients, abstracting away the complexities of underlying business objects.
Persistence API Actors
Question 15: Key Actors in Persistence API
Key actors in a persistence API (e.g., JPA): Entities, Entity Manager, Persistence Unit, Data Source.
EJB Persistence Example
Question 16: Steps for Demonstrating EJB Persistence
Steps to demonstrate EJB persistence (JPA):
- Create database tables.
- Create entity classes.
- Define a persistence unit and data source.
- Create and use a stateless session bean to interact with the database.
`javax.ejb.Stateful` Attributes
Question 17: Attributes of `javax.ejb.Stateful`
Attributes: `name`, `mappedName`.
`javax.ejb.EJB` Attributes
Question 18: Attributes of `javax.ejb.EJB`
Attributes: `beanInterface`, `beanName`, `mappedName`.
Interceptor Methods
Question 19: Interceptor Method Levels
Interceptor methods can be applied at the default, class, or method level.
`@Lob` Annotation
Question 20: `@Lob` Annotation
The `@Lob` annotation in JPA maps Java types to large object (LOB) types in the database (e.g., `java.sql.Blob`, `java.sql.Clob`, `byte[]`, `String`).
ACID Properties
Question 21: ACID Properties of a Transaction
ACID properties ensure database integrity:
- Atomicity: All operations succeed or none do.
- Consistency: Data remains valid.
- Isolation: Transactions are independent.
- Durability: Changes are permanent.