Documente Academic
Documente Profesional
Documente Cultură
JAVA/ J2EE
Notes prepared by
Mr. Ashok Kumar K
9742024066 | celestialcluster@gmail.com
www.vtuprojects.com | Final year IEEE project development and training from scratch by Mr. Ashok Kumar K.
Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.
Unit 8:
An Enterprise Java Bean (EJB) is a component of the J2EE architecture that primarily provides business logic
to a J2EE application and interacts with other server side J2EE components. The nature of business logic and
the interaction with the other server side J2EE components are dependent on the J2EE application.
EJB provides an architecture to develop and deploy component based enterprise applications considering
robustness, high scalability and high performance. An EJB application can be deployed on any of the
application server compliant with J2EE 1.3 standard specification..
Advantages of EJB
Types of EJBs
Session Bean
Session bean contains business logic used to provide a service to a client and exists for the
duration of the client server session. A session bean terminates once the session with their
client server terminates.
Entity Bean
Entity beans represent persistent data storage. User data can be saved to database via
entity beans and later on can be retrieved from the database in the entity bean.
EJB container is a vendor provided entity located on the EJB server that manages system level services for
EJB. EJB container is one of the several containers, each of which handles a J2EE component such as JSP or
servlets.
A clients request for web services is made to a web server, which forwards the clients request to the
appropriate server. Typically either the JSP or Servlet receives the client request and uses JNDI to look up the
EJB resource. The EJB object is returned and is used to get the EJBs home or local class. The home class is
used to reference the bean class through the EJBs remote class.
Deployment descriptor describes how EJBs are managed at runtime and enables the customization of EJB behavior
without modification to the EJB code such as describing runtime attributes of transactional context. The behavior of an
EJB can be modified within the deployment descriptor without having to modify the EJB class or the EJB interface.
A deployment descriptor is written in a file using XML syntax. Many times an IDE (Integrated Development Environment)
like Eclipse will automatically generates the deployment descriptor. The deployment descriptor file is packaged in the Java
Mr. Ashok Kumar K | 9742024066 | celestialcluster@gmail.com
www.vtuprojects.com | Final year IEEE project development and training from scratch by Mr. Ashok Kumar K.
Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.
Archive (JAR) file along with other files that are required to deploy EJB. An EJB container references the deployment
descriptor file to understand how to deploy and manage EJBs contained in the package.
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>myEJB</ejb-name>
<home>com.aklc.ejb.MyEJBHome</home>
<remote>com.aklc.ejb.MyEJBRemote</remote>
<ejb-class>com.aklc.ejb.MyEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
</entity>
</enterprise-beans>
</ejb-jar>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>myEJB</ejb-name>
<home>com.aklc.ejb.MyEJBHome</home>
<remote>com.aklc.ejb.MyEJBRemote</remote>
<local-home>com.aklc.ejb.MyEJBHomeLocal</local-home>
<local>com.aklc.ejb.MyEJBLocal</local>
<ejb-class>com.aklc.ejb.MyEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
</entity>
</enterprise-beans>
</ejb-jar>
Element Description
<!DOCTYPE> Defines the URL for the DTD and the organization that defines
the DTD
<enterprise-bean> Describes one or more EJB contained within a JAR file
<entity> Describes the type of EJB as entity bean
<session> Describes the type of EJB as session bean
<message-driven> Describes the type of EJB as message driven bean
The <env-entry> element is used in the deployment descriptor to define values that an EJB can use to customize the
EJBs behavior
<env-entry>
<env-entry-name>PassingGrade</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>35</env-entry-value>
</env-entry>
Security elements
<security-role-ref>
<description>myEJB security role</description>
<role-name>Supervisor</role-name>
</security-role-ref>
Transaction elements
A transaction is a single unit of work items which follows the ACID properties. ACID stands for Atomic, Consistent,
Isolated and Durable.
Atomic: If any of work items fails, the complete unit is considered failed. Success meant all items executes
successfully.
Consistent: A transaction must keep the system in consistent state.
Isolated: Each transaction executes independent of any other transaction.
Durable: Transaction should survive system failure if it has been executed or committed.
EJB Container/Servers are transaction servers and handles transactions context propagation and distributed transactions.
Transactions can be managed by the container or by custom code handling in bean's code.
Container Managed Transactions: In this type, container manages the transaction states.
Bean Managed Transactions: In this type, developer manages the life cycle of transaction states.
REQUIRED
Indicates that business method has to be executed within transaction otherwise a new transaction will be started
for that method.
REQUIRES_NEW
Indicates that a new transaction is to be started for the business method.
SUPPORTS:
Indicates that business method will execute as part of transaction.
NOT_SUPPORTED:
Indicates that business method should not be executed as part of transaction.
MANDATORY:
Indicates that business method will execute as part of transaction otherwise exception will be thrown.
NEVER:
Indicates if business method executes as part of transaction then an exception will be thrown.
A session bean contains business logic used to provide a service to a client and exists for the duration of the client server
session. A session bean terminates once the session with the client server terminates. A session bean is not persistent.
(That is, its data is not saved to a database)
A stateful session bean retains the state (data) between the method calls with a client during a session.
Because the client interacts (talks) with its bean, this state is often called the conversational state. As
its name suggests, a session bean is similar to an interactive session. A session bean is not shared; it
can have only one client, in the same way that an interactive session can have only one user. When the
client terminates, its session bean appears to terminate and is no longer associated with the client. The
state is retained for the duration of the client/bean session. If the client removes the bean, the session
ends and the state disappear. This transient nature of the state is not a problem, however, because
when the conversation between the client and the bean ends, there is no need to retain the state.
A stateless session bean does not retain the state between method calls and typically performs
business logic that doesnt require data to be maintained during the session.
When a client invokes the methods of a stateless bean, the beans instance variables may contain a
state specific to that client but only for the duration of the invocation. When the method is finished, the
client-specific state should not be retained. Clients may, however, change the state of instance
variables in pooled stateless beans, and this state is held over to the next invocation of the pooled
stateless bean. Except during method invocation, all instances of a stateless bean are equivalent,
allowing the EJB container to assign an instance to any client. That is, the state of a stateless session
bean should apply across all clients. Because they can support multiple clients, stateless session
beans can offer better scalability for applications that require large numbers of clients. Typically, an
application requires fewer stateless session beans than stateful session beans to support the same
number of clients. A stateless session bean can implement a web service, but a stateful session bean
cannot.
Retains state between the method calls Does not retain state between the method
calls
Cannot be shared between clients Can be shared between clients
Its life time is controlled by clients Its life time is controlled by the container
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import java.rmi.RemoteException;
Entity bean is used to manage a collection of data retrieved from a database and stored in memory. An entity bean
inserts, updates, and removes data while maintaining the integrity of the data.
Data collected and managed by an entity bean is referred to as persistent data and is managed in one of the two ways
Container managed persistence
Bean managed persistence
A container managed persistence (CMP) bean requires the container to manage the persistence. A CMP entity is heavily
dependent on support from the EJB container. The EJB container synchronizes the state of the entity bean with the
database. However, EJB container support varies by vendor. Most vendors support automatic persistence between the
entity bean and a relational database.
}
public Product getProduct() {
return myProduct;
}
public void setProduct(Product prod) {
myProduct = prod;
Mr. Ashok Kumar K | 9742024066 | celestialcluster@gmail.com
www.vtuprojects.com | Final year IEEE project development and training from scratch by Mr. Ashok Kumar K.
Registration started. Contact 9742013378 or 9742024066 and lock your project at the earliest.
}
public void setEntityContext(EntityContext cntx) {
}
public void unsetEntityContext() {
}
public void ejbLoad() {
}
public void ejbStore() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbRemove() {
}
}
The product object definition
import java.io.Serializable;
Home interface
Remote Interface
A bean managed persistence (CMP) bean requires a bean to manage the persistence. A BMP bean uses the JDBC API
or another appropriate database API to interface with the database. However, these interactions take place under the
direction of the EJB container. That is, the EJB container tells the BMP bean when to insert a new record, retrieve data,
modify data, or delete data.
A message driven bean is designed for clients to invoke server side business logic using asynchronous communication,
which is a special case of a stateless session bean.
Skeleton of a MDB
}
public void ejbCreate() {
}
public void ejbRemove() {
}
public void onMessage(Message clientMessage) {
}
}
The onMessage() method is where the MDB processes messages received indirectly from a client.
Concept
EJB classes and related files are packaged together into a Java Archive (JAR) file for deployment. The JAR file is a
compressed file format that was originally designed to reduce the size of software so it could be easily be transported.
The JAR file used to package an EJB must contain the following; however it is customary to keep the dependent classes
and interfaces in different JAR files.
EJB classes
Dependent classes
Remote interface
Home interface
Dependent interface
Primary key class
Deployment descriptor
You can use the JAR utility by entering jar cf followed by name of the JAR file, and then followed by the path and names
of files that will be archived.