Sunday 13 December 2015

OCEJBCD -4. Advanced Session Bean concepts

OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification
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

Note: when there is a timeout or a system exception the bean will be back to does not exist state.

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: 

Important:

  1. The bean is stateful session with an async method
  2. the async method returns a Future<T> object


Servlet code:

Important:

  1. servlet with a get method, and the bean is injected with annotation @EJB
  2. Observe that we will return the message back to the JSP when Future.isDone() that returns true when the task is completed.


JSP code:


The result when we executes the code above:
http://localhost:8080/TestWeb/helloPage


Remember:


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