4.1. Understand the relationship between the EJB container and an EJB component.
EJB components:
- Remote business interface. exposes business logic to remote clients.
- Local business interface. exposes business logic to local clients.
- Local no-interface. exposes public methods of the bean without using interface.
- Bean class.
* Note: message driven beans do not have remote or local interface.
EJB Container
Is the run-time container for beans deployed in the application server.
The container manages:
- Bean life cycle
- Persistence
- Transaction
- Security
- Concurrency
4.2. Describe the life cycle for stateless and stateful session beans
Stateless life cycle
Stateful life cycle
4.3. Implement session bean life cycle methods
import javax.annotation.PostConstruct
import javax.annotation.PreDestroy
@PostConstruct and @PreDestroy are callbacks when bean instances are created or destroyed.
import javax.ejb.PostActivate
import javax.ejb.PrePassivate
@PostActivate and @PrePassivate. To make the bean garbage collected in inactive periods, saving memory resources. The bean can be activated later and will maintain its conversational state.
4.4. Use a session bean to perform asynchronous communication
import javax.ejb.Asynchronous
@Asynchronous
You can use asynchronous invocation on stateless session bean or singleton session beans.
The return type of the method annotated with Asynchronous can be void or java.util.concurrent.Future<V> (java/util/concurrent/Future.html)
Example of session asynchronous bean implementation:
We need to define a stateless bean with an async method that is going to be invoked from a servlet. In the example we will have a page asking as for a user name and a button to submit.
After submission our servlet will get the request parameter "username", and will call the async method of the bean using the username as argument.
The bean async method will receive the username, will wait 5 seconds and will return after that time a greeting message: Hello + username.
Bean code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.util.concurrent.Future; | |
import javax.ejb.AsyncResult; | |
import javax.ejb.Asynchronous; | |
import javax.ejb.LocalBean; | |
import javax.ejb.Stateless; | |
/** | |
* Session Bean implementation class TestBean | |
*/ | |
@Stateless | |
@LocalBean | |
public class GreetingBean { | |
/** | |
* Default constructor. | |
*/ | |
public GreetingBean() { | |
// TODO Auto-generated constructor stub | |
} | |
@Asynchronous | |
public Future<String> asyncGreeting(String userName) { | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return new AsyncResult<String>("hello" + userName); | |
} | |
} |
Important:
- The bean is stateful session with an async method
- the async method returns a Future<T> object
Servlet code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.io.IOException; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.FutureTask; | |
import javax.ejb.EJB; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Servlet implementation class TestServlet | |
*/ | |
@WebServlet("/helloPage") | |
public class GreetingServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
@EJB | |
GreetingBean greetingBean; | |
/** | |
* Default constructor. | |
*/ | |
public GreetingServlet() { | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse | |
* response) | |
*/ | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
String userName = request.getParameter("userName"); | |
// call async method | |
if (userName != null) { | |
FutureTask<String> future = (FutureTask<String>) greetingBean | |
.asyncGreeting(userName); | |
while (!future.isDone()) { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
try { | |
request.setAttribute("message", future.get()); | |
} catch (InterruptedException | ExecutionException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
RequestDispatcher rd = request.getRequestDispatcher("/greeting.jsp"); | |
rd.forward(request, response); | |
} | |
/** | |
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse | |
* response) | |
*/ | |
protected void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
} | |
} |
Important:
- servlet with a get method, and the bean is injected with annotation @EJB
- Observe that we will return the message back to the JSP when Future.isDone() that returns true when the task is completed.
JSP code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
</head> | |
<body> | |
<form action="helloPage"> | |
<p> | |
name: <input type="text" name="userName" /> <br /> | |
</p> | |
<p> | |
<input type="submit" value="click me" /> | |
</p> | |
<p>${message}</p> | |
</form> | |
</body> | |
</html> |
The result when we executes the code above:
http://localhost:8080/TestWeb/helloPage
Remember:
- See details about how to configure the project in the previous post configuring-ejb-environment
- TestWeb is the name of our web project(context)
- helloPage is the name we have indicated in the annotation @WebServlet("/helloPage")
When the button is clicked after 5 seconds the greeting message is displayed in the page:
4.5. Have fine-grained control over packaging and deployment
Enterprise beans can be deployed using ejb-jar or war modules.
ejb-jar
- root
- classes
- META-INF
- ejb-jar.xml
- manifest
In a WAR file we can include the classes under classes folder or include the JAR file under lib.
- root
- WEB-INF
- ejb-jar.xml(optional)
- classes (*)--> ejb classes included in this folder
- lib
- ejb-jar(*) --> JAR file
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification