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

Sunday, 25 October 2015

OCEJBCD - 2- Implementing Session Beans

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

2.1. Examine session Beans

session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. 

What is a session bean?

2.2. Identify the three types of session beans
  • Stateless
    • they can support multiple clients
    • does not mantain conversational state with the client.
  • Stateful
    • Mantains conversational state with the client.
    • Relationship 1:1 with client.
  • Singleton 
    • new in javaEE6
    • is instantiated once per application and exists for the lifecycle of the application (one per application).
    • A single enterprise bean needs to be accessed by multiple threads concurrently

2.3. Choose the correct session bean type given a business constraint

  • Session stateless - to implement a web service. Transaction-processing applications, which are executed using a procedure call.
  • Session stateful - GUI client (filling fields in a GUI needs conversational state)
  • Singleton- to implement web service endpoints

2.4. Create session beans package and deploy session beans
The javabeans configuration can be defined with annotations or using an xml deployment descriptor located in :
META-INF/ejb-jar.xml

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

Saturday, 17 October 2015

OCEJBCD - 1- Introduction to JavaEE

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

1.1. Gain an understanding of the Java Platform, Enterprise Edition(JavaEE)


An enterprise application provides business logic modules. It uses architectures of distributed layers and it is executed by an applications server.

The features of JavaEE in every version are defined by several specifications (JSR- Java specification request). 

1.2. Examine the JavaEE application architecture

Summary of JavaEE6 APIs:
    • EJB : Enterprise Java Beans Technology
    • Servlets
    • JSF : Java server faces
    • JSP : Java server pages
    • JSP Tag libraries
    • JPA: Java Persistence API
    • JTA: Java Transaction API
    • JAX-RS Java API for RESTful Web Services
    • Managed Beans
    • CDI JSR 299- Contexts and dependency injection for JavaEE
    • JSR 330- Dependency Injection for Java
    • Bean Validation
    • JMS : Java Message API
    • JavaEE Connector Architecture
    • Java Mail API
    • Java Authorization Contract for Containers
    • JASPIC Java Authentication Service Provider for Containers

1.3. Examine JavaEE container services

Types of containers in JavaEE:

  • Client side
    • Application Web Container
    • Applet Container
  • Server side (JavaEE Container)
    • Web Container
    • EJB Container

Popular web containers: Apache Tomcat, Jetty,..
Popular full JavaEE Containers: Jboss(WildFly), Weblogic, Glassfish, ...

Services provided by a full JavaEE container:


  • Security
  • Transaction management
  • JNDI (Java naming directory interface)
  • Java EE remote connectivity (RMI)
  • Manage servlet and bean lifecycle
  • Database connection and persistence
  • Access to JavaEE APIs.

1.4. Examine the EJB component types
  • Enterprise bean class
    • Session bean -- associated with a client. They can be stateless or stateful.
    • Message-driven bean - allows to receive messages asynchronously (JMS)
  • Business interfaces
  • Helper classes (utilities, exceptions..)

1.5. Evaluate the EJB Lite Container

A lightweight subset of Enterprise JavaBeans(EJB 3.1) functionality.
http://www.oracle.com/technetwork/articles/javaee/javaee6overview-part3-139660.html#ejblite
Features included in EJB lite:
  • Stateless, stateful, and singleton session beans
  • Local EJB interfaces or no interfaces
  • Interceptors
  • Container-managed and bean-managed transactions
  • Declarative and programmatic security
  • Embeddable API
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification

Sunday, 27 September 2015

Producer Consumer Implementation - Blocking Queue

The Producer-Consumer design deals with the solution when different threads are involved in accessing the same shared resource to take or put elements from/to it.

- A consumer, thread class that removes one element from a resource.
- A producer, thread class that adds elements into a resource.
- A shared resource, called also monitor, will be a queue where the elements will be put and taken.

Originally this problem can be solved applying the methods wait() and notify(), and synchronizing the monitor.

The problem can be more simple using one of the collections of the concurrency package as blocking Queue (since 1.5).

BlockingQueue:
public interface BlockingQueue<E> extends Queue<E>

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.
Definition from: 
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html

This collection is part of java.util.concurrent package and its main feature is that provides internal locks and concurrency mechanism to be accessed atomically (thread-safe). The methods that can be used to implement the consumer-producer are: put / take.

Example:
Output:
Consumer takes0
Producer puts 0
Producer puts 1
Consumer takes1
Producer puts 2
Consumer takes2
Producer puts 3
Consumer takes3
Producer puts 4
Consumer takes4
Producer puts 5
Consumer takes5
Producer puts 6
Consumer takes6
Consumer takes7
Producer puts 7
Consumer takes8
Producer puts 8
Consumer takes9
Producer puts 9

Thursday, 10 September 2015

Java 7 - NIO - Watcher implementation

One of the new features of Java7 was NIO package. 
Java NIO also known as new input output library as is a set of classes defined under package java.nio.* to access files, file attributes and file systems.

The main classes from this package are:
  • Path - to manage file location and directory hierarchy
  • Files - to manage file operations
Some functionalities that can be implemented are:
  • Walking the file tree
  • Watchers - monitoring the creation/modification/deletion of files
  • Copy/Create/Delete files from a directory(atomic operations with synchronization)
  • Find files using matcher expressions
In the following example we are going to see how to implement a watcher. This is useful when we need a process to monitor when a file is created/modified or deleted in a directory.

Steps in this example:
1- watch for new files in a directory
2- evaluate the mime type of the file
3- if it is a text file it will be copied to other directory.


Saturday, 5 September 2015

OCEJBD - Oracle Certified Expert, Enterprise JavaBeans Developer - Intro

1Z0-895.- Oracle Certified Expert, Java Platform, Enterprise Edition 6 Enterprise JavaBeans Developer

The next certification I was planning to study is the certification in enterprise java beans Java EE6.
The requirement for this certification is to have passed the exam OCPJP (formely SCPJP).

These are a set of resources I am going to use for preparing the contents (at least initially)

Resources:
http://www.coderanch.com/forums/f-70/java-EJB-SCBCD
http://www.coderanch.com/how-to/java/ScbcdLinks#ejb6

Book:



Important Note: this book (Head First EJB) is based in EJB 2 (the exam is about EJB 3). It is a good book for general understanding of Java Beans, but the contents are not covering the current certification for Java EE6.   EJB 2 is very different from EJB 3.0, they are not related so it is recommended to go for the current version directly, so it is not necessary to learn EJB 2 to understand EJB 3.


You can find this recommendation in the following thread from JavaRanch.

In order to cover all the topics of the exam related with the current EJB3  version I am using this book : O'Reilly Enterprise JavaBeans 3.1



Mock Exams:
-Whizlabs
-Enthuware

Promo information:
There is a promotion this year. Introducing the code Java20 when you book an exam you can benefit of a discount of 20% in a java certification. So, I will try to book the exam before the end of this year.
https://blogs.oracle.com/certification/entry/1131_01

Topics

Introduction to JavaEE
  • Gain an understanding of the Java Platform, Enterprise Edition(JavaEE)
  • Examine the JavaEE application architecture
  • Examine JavaEE container services
  • Examine the EJB component types
  • Evaluate the EJB Lite Container
  • Compare JavaEE application development with traditional enterprise applicaton development.
Implementing Session Beans
  • Examine session Beans
  • Identify the three types of session beans
  • Choose the correct session bean type given a business constraint
  • Create session beans package and deploy session beans
Accesing Session Beans
  • Understand the purpose and role of JNDI in relation to EJB components
  • Configure JNDI environment properties
  • Use JNDI to look up a resource
  • Write code that receives a resource reference through injection
  • Create a session bean client
  • Create a session facade
  • Use dependency injection to locate an EJB
  • Understand the relationship between the EJB container and an EJB component.
  • Describe the life cycle for stateless and stateful session beans
  • Implement session bean life cycle methods
  • Use a session bean to perform asynchronous communication
  • Have fine-grained control over packaging and deployment
Singleton Session Bean
  • Understand the advantages and disadvantages of using a singletion session bean
  • Create a singleton session bean
  • Desribe the life cycle of a singleton session bean
  • Implement singleton session bean life cycle methods
  • Describe singleton concurrency access
  • Implement a concurrency management strategy
Developing Java EE Applications Using Messaging
  • Review JMS technology
  • Describe the roles of the participants in teh JMS API messaging system
  • Create a queue message producer
  • Create a synchronous message consumer
  • Understand the short-comings of using session beans as messaging consumers
  • Describe the properties and life cycle of message-driven beans
  • Create a JMS message-driven bean
  • Create life cycle event handlers for a JMS message-driven bean
  • Configure a JMS message-driven bean
  • Descrie timer services
  • Create a timer notification callback
  • Process a timer notification callback Manage timer objects
  • Describe interceptors and interceptor classes
  • Create a business interceptor method in the enterprise bean class
  • Create an interceptor class
  • Associate multiple business interceptor methods with an enterprise bean
  • Include life cycle callback interceptor methods in an interceptor class
Implementing transactions
  • Describe transaction demarcation management
  • Implement CMT
  • Interact programmatically with an ongoing CMT transaction
  • Implement BMT Apply transactions to messaging
Implementing Security
  • Understand the JavaEE security architecture
  • Authenticate the caller Examine JavaEE authorization strategies
  • Use declarative authorization
  • Use programmatic authorization Examine the responsibilities of the deployer
Using EJB Technology Best Practices
  • Define best practices and state the benefits of using EJB technology best practices
  • Select and apply known patterns to JavaEE application design
  • Incorporate effective exception handling into your JavaEE application design.
Package and Deploy EJB applications

Perform EJB Exception Handling

1Z0-895.- Oracle Certified Expert, Java Platform, Enterprise Edition 6 Enterprise JavaBeans Developer

Tuesday, 25 August 2015

Java 8 - Nashorn

One of the new features in Java8 is Nashorn (JSR 292)
It is basically the introduction of an engine to execute javascript in the server side over the JVM.

Over the last years there have been many solutions to execute javascript in the server side. Originally Javascript was used exclusively in the client, but with the emergence of new technologies (some as successful and trendy as node.js) javascript executed in the server is a real option.

My first question when I read about the idea of develop javascript in the server was ... why? what could be the advantage of using javascript in the server side?
If you have been working in web application probably the idea is in your mind...validation. Making validation simple and simplify code could be one of the advantages of this approach. Take into account that one of the principles of a secure development is to validate the data in both client and server.
Another advantage could be the idea of a unique programming language, not splitting anymore in front end/ server side expertise. The third advantage I could think is in re-usability of code routines.

Ok,...and how does it works? Let's see

Calling Javascript function from Java

In this example we will use a javaScript function that validates email format

Now using the js function from java:

We can use javascript in java using these three basic steps:
  • Defining ScriptEngine (see argument "nashorn")
  • Reference our javascript file using eval method.
  • Using invokeFunction or invokeMethod methods from Invocable class to call our javascript.