Saturday 21 November 2015

OCEJBCD - 3- Accesing Session Beans

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

3.1. Understand the purpose and role of JNDI in relation to EJB components

JNDI : Java naming directory
Binding - assigning a name to an EJB object which can be used later.
Lookup - looking up and getting an object of EJB.



3.2. Configure JNDI environment properties

http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html#girgn

global
java:global[/application name]/module name/enterprise bean name[/interface name]
module
java:module/enterprise bean name/[interface name]
app
java:app[/module name]/enterprise bean name[/interface name]

3.3. Use JNDI to look up a resource

Accessing an enterprise bean can be done by dependency injection or JNDI lookup.

Example using JNDI lookup:
ExampleRemote example = (ExmapleRemote) InitialContext.lookup("java:global/myApp/ExampleRemote");

3.4. Write code that receives a resource reference through injection

The annotations for inject a resource are:
@EJB - used to inject other EJB reference (on fields or on methods).
@Resource - used to inject datasource or singleton services like sessionContext, timerService etc.

We will see a complete code example in the followings sections (3.5- 3.7) and step by step we will create a javaEE application. This simple application is compound by:
  • Web client project : with a servlet and JSP
  • EJB server project :with sessionEJB and its interface.

The purpose will be create an application that allows a user selecting one color from a dropdown list. When submitting the selection a servlet will be executed and will call an EJB bean method. The bean method will return the color that will be displayed in the screen.

3.5. Create a session bean client


We are going to define an stateless session bean with a method that will evaluate a number of options and it will return the hex code corresponding to one color:

If input is 1 -- > color blue
If input is 2 -- > color red
If input is 3 -- > color yellow


3.6. Create a session facade

http://www.oracle.com/technetwork/java/sessionfacade-141285.html

Session facade provides a level of abstraction between the client and the business object. It manages the interactions between client and business services.

For the session bean defined in the previous section we will define an interface, that will be used by the client to access to our bean.


3.7. Use dependency injection to locate an EJB


Now, we will have to define the client that will call the bean (Servlet and JSP).
JSP

Servlet class
The servlet class will have a method doGet, that will be called everytime the button submit is clicked.
The dependency injection to locate the bean is applied by annotation:
  @EJB
  TestBeanLocal colorBean;


After deploying our application (web and EJB) in the server. When entering this url in the browser: http://localhost:8080/ColorWeb/myColor, the page is open:

 When a color is selected the corresponding hex code and color are shown in the screen
Note: The details about environment configuration are described in this post : http://javawithbreakfast.blogspot.com/2015/11/configuring-ejb-environment.html
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

No comments:

Post a Comment