Monday, 18 January 2016

OCEJBCD -5. Singleton Session Bean

OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

5.1. Understand the advantages and disadvantages of using a singleton session bean

A singleton is a stateless bean whose instance is accessed by all request in the application. Singletons have to be thread safe in order to manage the concurrently access.

Advantages:
- Only one instance. The bean will be created when the application is started and will exist during all application life.

Disadvantages:
- Could cause bottle-neck and performance, if it is not handle accurately.

5.2. Create a singleton session bean
javax.ejb.Singleton 
@Singleton
public class SingletonBean { ... }



Other annotations that can be used with singleton are:


@Startup (For eager initialization; it means the instance will be created at deployment time)

@DependsOn (For adding dependencies)

5.3. Describe the life cycle of a singleton session bean


5.4. Implement singleton session bean life cycle methods

For a full example using singleton see last section of this post.

5.5. Describe singleton concurrency access

Singleton beans are designed for concurrency access. There are two ways to manage concurrency.
  • Container-managed concurrency (by default)
  • Bean-managed concurrency
Example:
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Singleton
public class ExampleSingletonBean {...}

For managing concurrrency, there are the following annotations to lock blocks of code:

@Lock(LockType.READ)
@Lock(LockType.WRITE)

5.6. Implement a concurrency management strategy

The following example describes the use of a singleton bean called by a servlet in order to get a random number in the page after clicking a button.

The method that gets the random number can be accessed concurrently and the value will be consistent from different client accesses.

Bean class:
package example;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.LocalBean;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
/**
* Session Bean implementation class RandomNumberBean
*/
@Singleton
@LocalBean
@Startup
@ConcurrencyManagement
@Lock(LockType.READ)
public class CounterNumberBean {
private int counter = 1;
/**
* Default constructor.
*/
public CounterNumberBean() {
// TODO Auto-generated constructor stub
}
@Lock(LockType.WRITE)
public int getCounter() {
return counter++;
}
public void setRandom(int counter) {
this.counter = counter;
}
}

Servlet class:
package example;
import java.io.IOException;
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 RandomNumberServlet
*/
@WebServlet("/CounterNumberPage")
public class CounterNumberServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
CounterNumberBean randomNumberBean;
/**
* @see HttpServlet#HttpServlet()
*/
public CounterNumberServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
RequestDispatcher rd = request.getRequestDispatcher("/counterNumber.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
int number = randomNumberBean.getCounter();
request.setAttribute("counter", number);
RequestDispatcher rd = request.getRequestDispatcher("/counterNumber.jsp");
rd.forward(request, response);
}
}
JSP:
<%@ 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> JavaWithBreakfast Blog </title>
</head>
<body>
<!-- Forward processing to a servlet -->
<form action="CounterNumberPage" method="post">
<input type="submit" value="click me"/>
<p>${counter}</p>
</form>
</body>
</html>
After running the application in the server, when the page is opened in a browser the first time:

If we open another browser (emulating another client), the request accesses the same value of the counter. The counter value is going to be shared by all the requests:
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

No comments:

Post a Comment