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