0% found this document useful (0 votes)
8 views

Enterprise Java Bean

Enterprise JavaBeans (EJB) is a server-side component architecture for building distributed, transactional, and secure enterprise applications, part of Java EE. It includes various types of beans such as stateless, stateful, and message-driven beans, each serving different purposes and managing business logic. The EJB container provides essential services like lifecycle management, transaction management, and security, allowing developers to focus on business logic rather than system-level concerns.

Uploaded by

uditchaudhary140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Enterprise Java Bean

Enterprise JavaBeans (EJB) is a server-side component architecture for building distributed, transactional, and secure enterprise applications, part of Java EE. It includes various types of beans such as stateless, stateful, and message-driven beans, each serving different purposes and managing business logic. The EJB container provides essential services like lifecycle management, transaction management, and security, allowing developers to focus on business logic rather than system-level concerns.

Uploaded by

uditchaudhary140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Enterprise JavaBeans (EJB)

Enteprise java bean : Creating a javabeans, Javabeans


properties, Type Of beans, Stateful Session bean ,
Stateless session bean, entity bean.
What is EJB?
•Enterprise JavaBeans (EJB) is a server-side component architecture for building distributed,
transactional, and secure enterprise-level applications.

•It is a part of Java EE (Jakarta EE).


•EJB components are deployed in an EJB container (like WildFly, GlassFish), which provides services like:
•Transaction management
•Security
•Remote method invocation
•Concurrency
•Lifecycle management

When use Enterprise Java Bean?


1.Application needs Remote Access. In other words, it is distributed.
2.Application needs to be scalable. EJB applications supports load balancing, clustering and fail-over.
3.Application needs encapsulated business logic. EJB application is separated from presentation and persistent layer.
JavaBeans (Not EJB)

A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans
API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java classes −
•It provides a default, no-argument constructor.
•It should be serializable and that which can implement the Serializable interface.
•It may have a number of properties which can be read or written.
•It may have a number of "getter" and "setter" methods for the properties.
JavaBean Properties

A JavaBean property is a named attribute that can be accessed by the user of the object.
The attribute can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties
are accessed through two methods in the JavaBean's implementation class −

JavaBean properties are the attributes or fields of a JavaBean class. These properties follow
naming conventions:
•getPropertyName() – for reading the property/ This method is called accessor.
•setPropertyName(value) – for writing the property / This method is called mutator.
Types of Properties:

Type Description Example


Simple Property A single value private String name;
Indexed Property Array or list of values private String[] items;
Notifies listeners when the via
Bound Property
value changes PropertyChangeListener
Listeners can veto a
Constrained Property via VetoableChangeListen
property change
Accessing JavaBeans
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a
scripting variable that can be accessed by both scripting elements and other custom tags used in
the JSP.

Here values for the scope attribute can be a page, request, session or application based on
your requirement. The value of the id attribute may be any value as a long as it is a unique
name among other useBean declarations in the same JSP.
Accessing JavaBeans Properties
Along with <jsp:useBean...> action, you can use the <jsp:getProperty/> action to access the get methods and
the <jsp:setProperty/> action to access the set methods. Here is the full syntax −

The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean action.
The property attribute is the name of the get or the set methods that should be invoked.
Types of Enterprise JavaBeans

Session beans, Entity beans, and Message-driven beans

Session Beans:
•Stateful:
Maintain state across multiple method calls for a specific client, managing business functions for a client's
session.

•Stateless:
Do not maintain state across calls and are used for business services, providing stateless logic.

•Singleton:
A single instance of the bean is shared by all clients, providing a shared resource.
Entity Beans:

•Manage persistent data, typically interacting with a database,

•Deprecated in newer versions of Java EE, with JPA (Java Persistence API) being the
preferred approach for persistence.

Message-driven Beans:

•Receive messages from a JMS (Java Message Service) queue or topic.

•Designed for asynchronous communication and event handling.


A. Session Beans
Used for implementing business logic. They can be further divided into:

i. Stateless Session Bean


•Does not maintain any client state.
•A single bean instance can serve multiple clients.
•Ideal for simple tasks like validation, calculations, sending emails.

Key Features:
•No data is retained between method calls.
•Lightweight and scalable.
•Annotated with @Stateless.
Annotations used in Stateless Session Bean
There are 3 important annotations used in stateless session bean:
1.@Stateless
2.@PostConstruct
3.@PreDestroy

EJB Container creates and maintains a pool of session bean


first. It injects the dependency if then calls the @PostConstruct
method if any. Now actual business logic method is invoked by
the client. Then, container calls @PreDestory method if any.
Now bean is ready for garbage collection.
1) Create stateless bean component 2) Create stateless bean client
ii. Stateful Session Bean
•Maintains state across multiple method calls and transactions.
•Each client gets a dedicated instance of the bean.
•Ideal for scenarios like shopping cart, user sessions.

Key Features:
•Holds data specific to a client.
•Destroyed when the client session ends.
•Annotated with @Stateful.
Conversational state between multiple method calls is maintained by the container in stateful session bean.
Annotations used in Stateful Session Bean
There are 5 important annotations used in stateful session bean:
1.@Stateful
2.@PostConstruct
3.@PreDestroy
4.@PrePassivate
5.@PostActivate

1) Create stateful bean component


Message Driven Bean
A message driven bean (MDB) is a bean that contains business logic. But it is invoked by passing the message. So, it is
like JMS Receiver.
MDB asynchronously receives the message and processes it.
A message driven bean receives message from queue or topic, so you must have the knowledge of JMS API.

A message driven bean is like stateless session bean that encapsulates the business logic and doesn't maintain state.
Message Driven Bean Example

To create the message driven bean, you need to declare @MessageDriven annotation and implement MessageListener
interface.
In eclipse ide, create EJB Project then create a class as given below:
In glassfish server, click on applications -> deploy -> select mdb jar file
by Choose File -> OK.
B. Entity Beans (Deprecated)
•Used to represent data stored in a database.
•Mapped to a row in a table.
•Replaced by JPA (Java Persistence API) from EJB 3.x onwards.

Key Features:
•Persistent by nature.
•Automatically synchronized with the database.
•Annotated with @Entity.
EJB Container
The EJB Container is a runtime environment provided by the application server (such as WildFly, GlassFish,
or JBoss) that manages the execution of Enterprise JavaBeans (EJBs). It provides essential services to the
beans automatically so that the developer can focus on business logic rather than system-level concerns.

1. What is an EJB Container?


An EJB Container is a part of the Java EE (Jakarta EE) server that:
•Manages the lifecycle of EJB instances.
•Provides runtime services like transaction management, security, and dependency injection.
•Ensures declarative programming, where developers specify behavior using annotations or XML (e.g.,
@Stateless, @TransactionAttribute).
Think of it as the "manager" or "controller" of your EJBs that handles all the heavy lifting like creating
objects, handling method calls, transactions, etc.
2. Services Provided by the EJB Container
Service Description
Creates, initializes, pools, and destroys EJB instances
Lifecycle Management
automatically.
Supports container-managed or bean-managed
Transaction Management
transactions.
Security Manages access using role-based security annotations.

Manages access to beans from multiple clients


Concurrency Control
(especially for singleton/session beans).
For entity beans (via JPA), the container manages
Persistence
object-relational mapping.
Injects references of other beans or resources using
Dependency Injection
annotations like @EJB, @Resource.
Exposes beans as remote services (RMI, CORBA, web
Remote Access / Networking
services).
Manages system and application exceptions
Exception Handling
automatically.
Lifecycle Management of EJBs
The EJB container is responsible for managing the entire lifecycle of each type of EJB. Here’s how it works
for each bean:

Stateless Session Bean Lifecycle


1.Instantiation: Container creates a pool of instances at deployment/startup.
2.Method Invocation: The client invokes a method; the container assigns an instance from the pool.
3.Destroy: Container may destroy the bean or return it to the pool.

Annotations Used:
•@PostConstruct: Called after bean creation.
•@PreDestroy: Called before bean destruction.
Stateful Session Bean Lifecycle

1.Instantiation: One instance per client.


2.Business Method Calls: The same instance is used for the client's session.
3.Passivation (Optional): Bean is serialized and stored if inactive.
4.Activation: Bean is brought back to memory when needed.
5.Destroy: On timeout or manual removal.

Annotations Used:
•@PostConstruct, @PreDestroy, @PrePassivate, @PostActivate

Entity Bean Lifecycle (JPA Entity)

1.New: Entity is created but not associated with a database row.


2.Managed: Entity is in the persistence context and synchronized with DB.
3.Detached: Entity is not managed by the container anymore.
4.Removed: Entity is marked for deletion.

Managed using JPA EntityManager methods.


Deployment & EJB Container Interaction

•EJBs are packaged in .jar files inside .ear or .war applications.


•When deployed to the server:
• The container scans annotations.
• Creates instances.
• Registers JNDI bindings for remote/local access.
• Sets up interceptors and proxies.

Types of EJB Containers


Container Type Description
Stateless Session Container Manages stateless beans; uses pooling.
Stateful Session Container Manages client-specific stateful beans.
(Legacy) Manages persistent entity beans (now
Entity Container
replaced by JPA).
Manages message-driven beans for asynchronous
Message-Driven Bean Container
communication (e.g., via JMS).
Example: Using Container-Managed Services
EJB container is a server-side component that comprises the business logic. It offers local and remote access to
the enterprise beans. In other words, we can say that it provides a runtime environment for EJB applications within
the application server. A single EJB container can have one or more EJB modules. It acts as an intermediate action
between business logic and enterprise application. The following figure depicts the structure of the EJB container.

You might also like