0% found this document useful (0 votes)
120 views6 pages

EJB

EJB is an acronym for Enterprise Java Bean. EJBs run in a separate container called as EJB Container. EJBs are built over the foundations of Remote Method Invocation RMI. Types of beans include Session Bean, Entity Bean, and Message Driven Bean.

Uploaded by

hima bindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views6 pages

EJB

EJB is an acronym for Enterprise Java Bean. EJBs run in a separate container called as EJB Container. EJBs are built over the foundations of Remote Method Invocation RMI. Types of beans include Session Bean, Entity Bean, and Message Driven Bean.

Uploaded by

hima bindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EJB

EJB is an acronym for Enterprise Java Bean. When the application is an enterprise scale application with
huge number of users accessing data services concurrently, the designer has to take care of
concurrency, transaction integrity and security while designing normal java beans which are POJOs. In
such cases EJBs come as a solution. EJBs run in a separate container called as EJB Container.

To run EJB application, an application server (EJB Container) such as Jboss, Glassfish, Weblogic,
Websphere etc is needed. The services that an Enterprise Bean container offers to its components
include.
a. Managing the lifecycle and number of instances of the Enterprise Bean.
b. Managing the concurrency of access to methods exposed by the Enterprise Bean.
c. Allowing Enterprise Beans to implement asynchronous methods
d. Allowing multiple Enterprise Bean method calls to be handled atomically within a single
transaction.
e. Gating calls to Enterprise Beans to allow them to declare which users are allowed access to their
method.
EJBs are built over the foundations of Remote Method Invocation RMI. The RMI allows an object to
invoke methods on an object running in another JVM. The RMI provides remote communication
between the applications using two objects stub and skeleton. In response to a call, a stub initiates a
connection to the remote JVM, marshalls and transmits the parameters of the call. A skeleton on the
other side receives, unmarshals the parameters, invokes the method, receives the result, marshals it and
transmits it to the stub. The stub unmarshals the result and returns to the caller.

Types of beans:

Session Bean: Session bean stores data of a particular user for a single session. It can be stateful or
stateless. It is less resource intensive as compared to entity bean. Session bean gets destroyed as soon
as user session terminates.
Stateless Session Bean: It doesn't maintain state of a client between multiple method calls.
Stateful Session Bean: It maintains state of a client across multiple requests.
Singleton Session Bean: One instance per application, it is shared between clients and supports
concurrent access.

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 beans are used in context of JMS (Java Messaging Service).
Message Driven Beans can consumes JMS messages from external entities and act accordingly.
An EJB component can have remote and local interfaces. Clients not located in the same application
server instance as the bean use the remote interface to access the bean. Calls to the remote interface
require marshalling and transportation of the arguments over the network, un-marshaling the
arguments at the receiving end. Thus, using the remote interface entails significant overhead. Following

Annotations:
The following annotations are used in EJB 3.0 for dependency injection.
@EJB − used to inject other EJB reference.
@Resource − used to inject data source or singleton services like sessionContext, timerService.
There are only two states of stateless session bean: does not exist and ready. There are 3 important
annotations used in stateless session bean:
@Stateless : The class decorated with this annotation facilitates the creation of a stateless bean.
@PostConstruct : The method decorated with this annotation is executed before the bean becomes
ready.
@PreDestroy : The method decorated with this annotation is executed before the bean is destroyed.
EJB Lifecycle:
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. There are 5
important annotations used in stateful session bean:
@Stateful : The class decorated with this annotation facilitates the creation of a stateful bean.
@PostConstruct : The method decorated with this annotation is executed before the bean becomes
active.
@PreDestroy: The method decorated with this annotation is executed before the bean is destroyed.
When a user is not actively communicating with the bean then the bean is passivated, for example when
the user is entering the data in a html form. After the user enters the data and submits, the bean is
activated once again.
@PrePassivate : Designates a method to receive a callback before a stateful session bean is passivated.
@PostActivate: Designates a method to receive a callback after a stateful session bean has been
activated.

Stateless Bean Local Interface


//Session Bean
package ejb;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloBeanLocal {
public String hello(){
return "Hello, Local World";
}
}

package ejb;
import javax.ejb.Local;
@Local
public interface HelloBeanLocal {
String hello();
}
//Servlet in webapp
package web;
import ejb.HelloBeanLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

@EJB
private HelloBeanLocal helloBean;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<h1>"+helloBean.hello()+"</h1>");

}
}
______________________________________________________________________

Stateless Bean Remote Interface


//Session Bean
package ejb;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloBeanRemote {
public String hello(){
return "Hello, Remote World";
}
}
//Remote interface created in a java class library
package ejb;
import javax.ejb.Remote;
@Remote
public interface HelloBeanRemote {
String hello();
}
//Servlet in webapp
package web;
import ejb.HelloBeanRemote;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

@EJB
private HelloBeanRemote helloBean;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<h1>"+helloBean.hello()+"</h1>");

}
}

You might also like