Saturday, June 19, 2010

ConcurrentHashMap :Be Careful while Iterating over

As Per Java Docs Iterator for ConcurrenthashMap does not guarantee to reflect concurrent changes in the map. It is due to the way thread safety for add/remove/update operations has been internally implemented in ConcurrentHashMap- ConcurrentHashMap internally divides the complete map in structures known as Buckets which can be accessed/Locked independently.

So, in order to avoid any NullPointerExceptions being thrown we need have a check for nulls being returned before making any calls on returned object. See snippet below to understand it further :-

Iterator it = testConcHashMap.keySet().iterator();
while(it.hasNext()) {
String key= (String)it.next();
String value= testConcHashMap.get(key);
//value.equals("anotherstring");//May throw a Null Pointer

if(! (value== null)){ //check for null
value.equals("anotherstring");
}
}


For further reading see java docs : http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html

Monday, June 14, 2010

Free e-book -Java Message Service

drop in your email ids as comments to get a very good free e-book by a renowned publisher on Java Message service.

CMMI- An overview

The CMMI is a framework for business process improvement. In other words, it is a model for building process improvement systems. In the same way that models are used to guide thinking and analysis on how to build other things (algorithms, buildings, molecules), CMMI is used to build process improvement systems.

CMMI (Capability Maturity Model Integration) was formed after merging multiple CMM models - Capability Maturity Model for Software(SW-CMM), Systems Engineering Capability Model(SECM), Integrated Product Development Capability Maturity Model (IPD-CMM). As managing multiple CMM models is difficult, SEI integrated all CMM models into one - CMM Integrated. The structure and essence of CMMI is same as that of CMM.

There are 5 levels of maturity in CMMi, each layer is the foundation for ongoing improvement, designated by numbers 1 through 5,befor getting itnto that we need to understand PA or Process Areas.

A process area is a cluster of related practices in an area that, when implemented collectively, satisfy a set of goals considered important for making improvement in that area.

Level 1 : Initial or Ad-hoc. There are no PAs (Process Areas) in this level
Level 2 : Managed. There are 7 PAs. PAs at this level look at project planning and execution (Basic project management)
Level 3 : Defined. There are 11 PAs here. Life cycle processes and Organizational processes are the focus areas here.
Level 4 : Quantitatively Managed. There are 2 PAs that deal with project management with quantitative data and statistical process control. (SPC)
Level 5 : Optimizing. There are 2 PAs. The focus is on continuous improvement.

There are 22 process areas in all.
As we move from Level 1 to 5, the project risk decreases & Quality and Productivity increases.

What are the characteristics of a mature organization ?


Organizations can achieve progressive improvements in their organizational maturity by achieving control first at the project level and continuing to the most advanced level—organization-wide continuous process improvement—using both quantitative and qualitative data to make decisions.

A mature software organization possesses an organization-wide ability for managing software development and maintenance processes. The software process is accurately communicated to both existing staff and new employees, and work activities are carried out according to the planned process. The processes mandated are fit for use and consistent with the way the work actually gets done. These defined processes are updated when necessary, and improvements are developed through controlled pilot-tests and/or cost benefit analyses. Roles and responsibilities within the defined process are clear throughout the project and across the organization.

The CMM / CMMI model was designed to guide software organizations in selecting process improvement strategies by determining current process maturity and identifying the few issues most critical to software quality and process improvement.

What's the difference between Maturity Level and Capability Level?


They are different ways of rating your processes.

A "Maturity Level" is what you can be appraised to and rated as when the organization uses the Staged Representation of the CMMI, and a "Capability Level" is what you can be appraised to and rated as when the organization uses the Continuous Representation of the CMMI.

WorkFlow Engine - JBoss jBPM

Basic Info on WorkFlow engine to start with,will add to it going further

What is a workflow engine?

- Workflow engine offers a programmatic structure for designing transactions and executing those using automated decisions, tasks and sequence flows.

- Workflow engine typically include three components:
- An engine that executes process definitions
- Services that allow the engine to interact with the outside world
- Tools that aid process development and monitoring.

- Workflow engine require participation of two responsibility levels:
-Business
-Technical.

jBoss jBPM

JBoss jBPM is an Open source, flexible and extensible workflow management system.
The business process can be represented graphically in terms of :-

a. Tasks.
b. wait states for asynchronous communication.
c. Timers,
d. Automated actions etc.

It has extensible control flow mechanism which binds all these operation together.

JBpm can be configured with any RDBMS database like Oracle, MySql, Hypersonic Sql etc and it can be deployed on any J2EE application server.

JBoss jBPM uses a unique process oriented programming model with its Process Definition Language (jPDL) which merges the best of java and declarative programming technique in order to create an easy to understand process graphs.

JMS-Messaging Models

Java Message Service (JMS) API allows Java applications to implement messaging using a standard API. The Java Message Service (JMS) has defined the standard for reliable Enterprise Messaging. JMS(API) is a part J2EE specifications and was developed by Sun Microsystems(Now Part of Oracle Corp.).

JMS API provides following advantages to enterprise applications:

a. Loose coupling
b. Scalability
c. Asynchronous messaging
d. Reliable message sending
e. Heterogeneity
f. Modularity

JMS is composed of following parts:

JMS provider:
it provides administrative and control features andimplements the JMS interfaces.
JMS clients: Components that produce and consume messages.
Messages: Object that carry information between JMS clients.
Administered objects: preconfigured JMS objects which are used by clients and are created by administrators.Eg. Destination and Connection Factories.
Native clients: Programs that uses some messaging product’s native client API instead of JMS API.

Messaging systems support following messaging models:

Publish-Subscribe Messaging
Point-to-Point Messaging

Publish-Subscribe Messaging

This model is used when several applications need to receive the same message. In Publish-Subscribe messaging system, multiple publisher can send messages to a Topic and all the Subscribers to that topic receive all the messages sent to that Topic. In a Publish Subscribe Messaging there may be multiple Senders and multiple Receivers. Publishers and subscribers are generally anonymous and may dynamically publish or subscribe to the content.


Point-to-Point Messaging


In this model, a producer sends messages to a particular queue and a consumer gets messages from the queue. Queues retain all messages sent to them until the messages are consumed or until the messages expire. Only one consumer will get the message and producer will know the consumer of the message.

Consumption of Messages

Messages sent using above two messaging models can be received /consumed in following two ways:

Synchronous: The message is fetched from destination by calling the receive method.
Asynchronous: By registering a message listener. When a message arrives at the destination, the JMS provider delivers the message by calling the listener's onMessage method.

Sunday, June 13, 2010

Understanding Object Oriented Programming Principles: Encapsulation, Inheritance, Polymorphism and Abstraction

Encapsulation:
Means putting the data and the function that operates on that data in a single unit(information hiding) .Encapsulation prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.
Encapsulation is the mechanism that binds together code and the data it manipulates,and keeps both safe from outside interference and misuse.in java encapsulation is achieved by making use of access modifiers( keeping variables as private and providing public getters and setters.

Eg:-
automatic transmission of an automobile.
It encapsulates lots of information about the engine, such acceleration, the pitch of the surface , and the position of the shift
lever. Being a user,we have only one method of affecting this complex encapsulation: moving the gear-shift lever.

Inheritance:
Inheritance is the process by which one object acquires the properties of another object.

Eg:-Vehicle->motorized->4wheeled.

Polymorphism:
Polymorphism (from the Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions.

Eg. Car's steering ->one interface many implementations(power or rack and pinion)

Abstraction:
Means hiding the internal details and just exposing the functionality.

Eg:-When we change the gear of a car, we know the gears will be changed without knowing how they are functioning internally.
Abstraction focuses on the outside view of an object (i.e. the interface).

HashMap Vs HashTable Vs ConcurrentHashmap

HashMap and HashTable both provide key-value access to data.

The Hashtable is among the original collection classes in Java.Hashtable extends the Dictionary class, which as the Javadocs state, is obsolete and has been replaced by the Map interface. HashMap is part of the new Collections Framework, added with Java 2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap is not synchronized.This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones

HashMap has a more complex hashing algorithm then Hashtable. It takes the hash value from the key and then hashes it again (double hashing). This can improve the distribution of the keys and hence the performance of the Map.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If we change the map while iterating, it will throw exception.

Third difference is that HashMap permits null values in it, while Hashtable doesn't.Also note that only one NULL value is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.

Using ConcurrentHashmap: for having a thread safe map we can use ConcurrenthashMap(from java5 onwards) as well instead of Hashtable which has become obsolete.

private Map myConcMap = new ConcurrentHashMap();

Now The question arises why ConcurrentHashMap and not HashTable or just have a synchronised access to HasMap.

So the major advantage of using ConcurrentHashMap is "performance" as the lock is not applied on wholeMap as is the case with a Synchronised access to hashmap or Hashtable.

As we know that hash maps store their data in a series of separate buckets, it is possible to lock only the portion of the map that is being accessed.ConcurrentHashMap uses this to provide us a highly optimized synchronised way of accessing HashMap.ConcurrentHash hash map follows following to provide a concurrent access:

1. Writing to a ConcurrentHashMap locks only a portion of the map
2. Reads can occur without locking.

Some disadvantges of ConcurrentHashMap:
ConcurrentHashMap will generally take up more memory.
it cannot take null as a key.