Sunteți pe pagina 1din 13

VTU

7th sem B.E (CSE/ISE)

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:

Enterprise Java Beans (EJB)

Mr. Ashok Kumar K


9742024066 | celestialcluster@gmail.com

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.

15 reasons to choose VTUPROJECTS.COM for your final year project work

1. Training from the scratch


We train our students on all the languages and technologies required for developing the projects from the
scratch. No prerequisites required.
2. Line by Line code explanation
Students will be trained to such an extent where they can explain the entire project line by line code to their
respective colleges.
3. Study Materials
We provide the most efficient study material for each and every module during the project development
4. Trainers
Each faculty in AKLC will be having 6+ years of corporate Industry experience and will be a subject matter
expert in strengthening student's skillset for cracking any interviews. He will be having a thorough experience
in working on both product and service driven industries.
5. Reports and PPTs
Project report as per the university standards and the final presentation slides will be provided and each
student will be trained on the same.
6. Video manuals
Video manuals will be provided which will be useful in installing and configuring various softwares during
project development
7. Strict SDLC
Project development will be carried out as per the strict Software Development model
8. Technical Seminar topics
We help students by providing current year's IEEE papers and topics of their wish for their final semester
Technical seminars
9. Our Availability
We will be available at our centers even after the class hours to help our students in case they have any
doubts or concerns.
10. Weightage to your Resume
Our students will be adding more weightage to their resumes since they will be well trained on various
technologies which helps them crack any technical interviews
11. Skype/ Team viewer support
In case the student needs an emergency help when he/she is in their colleges, we will be helping out them
through Skype/ Team viewer screen sharing
12. Practical Understanding
Each and module in the project will be implemented and taught to the students giving practical real world
applications and their use.
13. Mock demo and presentations
Each student will have to prepare for mock demo and presentations every week so that he/she will be
confident enough to demonstrate the project in their respective colleges
14. Communication & Soft skills Training
We provide communication and soft skills training to each students to help improve their presentation and
demonstration skills.
15. Weekly monitoring
Each student will be monitored and evaluated on the status of the project work done which helps the students
to obtain thorough understanding on how the entire project will be developed

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.

8.1 Enterprise Java Beans

Concept and Definition

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

Simplifies the development of large scale enterprise level application.


Application Server/ EJB container provides most of the system level services like transaction handling,
logging, load balancing, persistence mechanism, exception handling and so on. Developer has to focus
only on business logic of the application.
EJB container manages life cycle of EJB instances thus developer needs not to worry about when to
create/delete EJB objects.

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.

Message Driven Bean


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. It is used
to receive message from a JMS resource (Queue or Topic)

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.
EJB Architecture

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.

The session and entity beans must have two interfaces


Home interface or local interface
The local clients that are on the same JVM as the EJB interact with the EJB using the home interface.
Remote interface
Remote interface is used by remote clients that are capable of accessing the EJB container from an
application that is compliant with RMI and IIOP.

8.2 Deployment Descriptors

Concept and Definition

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.

Format of the deployment descriptor

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.

Deployment descriptor for EJB 1.1

<!DOCTYPE ejb-jar >

<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>

Deployment descriptor for EJB 2.0

<!DOCTYPE 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>

Description of the elements used

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

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.
<ejb-name> Describes the name of session or entity bean
<home> Describes the fully qualified class name of the session or entity
bean remote home interface
<remote> Describes the fully qualified class name of the session or entity
bean remote interface
<ejb-class> Describe the fully qualified class name of the session or entity
bean class
<persistence-type> Specifies either container managed persistence or bean
managed persistence
<prim-key-class> Describes the primary key class for entity beans
<reentrant> Specifies that back (reentrant invocations) is allowed or not
<local-home> Describes the fully qualified class name of the session or entity
bean local home interface
<local> Describes the fully qualified class name of the session or entity
bean local interface

Customizing the EJB behavior by defining the environment variables

The <env-entry> element is used in the deployment descriptor to define values that an EJB can use to customize the
EJBs behavior

Defining the environment variable

<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>

Accessing the environment variable

InitialContext jc = new InitialContext();


Integer grade = (Integer) jc.lookup("java:comp/env/PassingGrade");

Security elements

Defining the <security-role> element

<security-role-ref>
<description>myEJB security role</description>
<role-name>Supervisor</role-name>
</security-role-ref>

Verifying the role name from within EJB

boolean supervisor = context.isCallerInRole("Supervisor");


if (!supervisor) {
throw new AccessDeniedException();
}

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.

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.

EJB Transaction Attributes


An EJB server will manage a transaction based on the EJBs transaction attribute when the EJB is deployed. This is
referred to as container managed. A transaction attribute can apply at the EJB level or at the method level. An EJBs
transaction attribute is defined in the deployment descriptor by assigning one of the following values to the transaction
attribute:

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.

8.3 Session Java Bean

Concept and Definition

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)

Types of Session Beans

Session beans are of three types: stateful and stateless.


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.

Stateful Session Beans

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.

Stateless Session Beans

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.

Stateful session bean Stateless session bean

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

A code skeleton of a session bean

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import java.rmi.RemoteException;

public class CalculatorBean implements SessionBean {

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 int add(int a, int b) throws RemoteException {
return a + b;
}

public void setSessionContext(SessionContext sessionContext)


throws EJBException, RemoteException {
}

public void ejbCreate() throws EJBException, RemoteException {


}

public void ejbRemove() throws EJBException, RemoteException {


}

public void ejbActivate() throws EJBException, RemoteException {


}

public void ejbPassivate() throws EJBException, RemoteException {


}
}

8.4 Entity Java Bean

Concept and Definition

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

Container managed persistence (CMP)

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.

Skeleton of an entity bean

class myEJB implements EntityBean {


int iD;

private Product myProduct;

public void ejbPostCreate() {

}
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;

class Product implements Serializable {


public String prodName, prodDescription;

public Product(String name, String desc) {


prodName = name;
prodDescription = desc;
}
}

Home interface

public interface myEJBHome extends EJBHome {


public myEJB create(Integer prodID) throws RemoteException, CreateException;

public myEJB findByPrimaryKey(Integer prodID) throws RemoteException,


FinderException;
}

Remote Interface

public interface myEJBRemote extends EJBObject {


public getProduct() throws RemoteException;

public setProduct(Product prod) throws RemoteException;


}

Accessing the CMP bean

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.
Calling the CMP using the home interface

javax.naming.Context jndiContext = new InitialContext();


Object obj = jndiContext.lookup("java:comp/env/ejb/myEJBHome");
myEJB mybean = obj.create(4321);

Calling the CMP using the remote interface

javax.naming.Context jndiContext = new InitialContext();


Object obj = jndiContext.lookup("java:comp/env/ejb/myEJBHome");
Product prod = obj.getProduct();

Bean managed persistence (BMP)

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 skeleton of a BMP bean

class myBMPEJB implements EntityBean {

public void ejbLoad() {


// Read data from a database
}

public void ejbStore() {


// Save data to a database
}

public void ejbCreate() {


// Insert a record into the database
}

public void ejbRemote() {


// Remove a record from a database
}

public Product findProduct() {


// Find the specified product
}
}

8.5 Message Driven Bean

Concept and Definition

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.

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.
There isnt any home interface or remote interface for a MDB. Instead, a MDB monitors Java Message Service (JMS)
communications and reacts to messages sent by clients. Client dont directly access a MDB. Instead, the MDB intercedes
and processes requests anonymously. This is possible because a MDB is stateless.

Skeleton of a MDB

class myMDB implements MessageListener, MessageDrivenBean {


public void setMessageDrivenContext(MessageDrivenContext mdc) {

}
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.

8.6 The JAR file

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.

Mr. Ashok Kumar K | 9742024066 | celestialcluster@gmail.com

S-ar putea să vă placă și