Sunday 28 August 2016

OCEJBCD -9.Implementing Interceptor Classes and Methods

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

9.1. Describe interceptors and interceptor classes

An interceptor is a class used to intercept calls to EJB.

They are usually used for logging, authentication or validation purposes.

A typical example can be logging at the beginning and end of a method. This can be a tedious task, imagine to create logging entries for a big application. With interceptors it is avoided this duplicity of code, only specifying this behaviour in an interceptor class and used by annotations in the classes of your application.

The interceptor can be used by all methods of the class if we annotate our bean with :
@Interceptors(MyInterceptor.class)
We can apply more than one interceptor separating list with comma:
@Interceptors(MyInterceptor1.class, MyInterceptor2.class)

Or we can define which methods will be intercepted with annotations at method level with: 
@Interceptors(MyInterceptor.class)

Also we can exclude class interceptors to be executed in some methods with:
@ExcludeClassInterceptors


9.2. Create a business interceptor method in the enterprise bean class

Also we can define an interceptor method inside the bean class. 
This interceptor method will be the last one to be invoked before the actual bean method:



2016-08-28T10:50:05.833+0100|Info: Entering method:public void example.TestBean.firstMethod()
2016-08-28T10:50:05.833+0100|Info: Leaving method:public void example.TestBean.firstMethod()

9.3. Create an interceptor class


There are three kinds of interceptor method:
  • method interceptor methods for methods of a target class.
    • @AroundInvoke
  • timeout method interceptor methods for timeout methods of a target class 
    • @AroundTimeout
  • lifecycle callback interceptor methods : 
    • @PostConstruct and @PreDestroy 
    • (and for @PrePassivateand @PostActivate methods of EJB stateful session beans).



9.5. Associate multiple business interceptor methods with an enterprise bean


Applying the interceptor class defined in the previous section to our test bean. In the output log we can verify that two interceptors method are being invoked for firstMethod: first the interceptor at class level, and second the one at method level.
The second method will be called only by the interceptor at bean method due to annotation of excluding class interceptors.




2016-08-28T11:07:55.689+0100|Info: **Begin public void example.TestBean.firstMethod()
2016-08-28T11:07:55.689+0100|Info: Entering method:public void example.TestBean.firstMethod()
2016-08-28T11:07:55.689+0100|Info: Leaving method:public void example.TestBean.firstMethod()
2016-08-28T11:07:55.689+0100|Info: **End public void example.TestBean.firstMethod()
2016-08-28T11:07:55.689+0100|Info: Entering method:public void example.TestBean.secondMethod()

2016-08-28T11:07:55.689+0100|Info: Leaving method:public void example.TestBean.secondMethod()

9.6. Include life cycle callback interceptor methods in an interceptor class
The lifecycle of an interceptor instance is the same as that of the target class instance with which it is associated.

@PostConstruct @PreDestroy
public void interceptLifeCycle(InvocationContext ctx){...

This callbacks can be used to initialize the state of the bean.
Because callbacks have not return value, the return type must be void.


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


No comments:

Post a Comment