Thursday 26 February 2015

ORM using Morphia: Tip off

Morphia is a great ORM framework that maps your Java objects to mongo documents.

A general advice would be to refrain from using primitive types like int, long, double in the DO and instead substitute them with their wrapper class counterparts. This is because Morphia defaults values for the fields with primitive types in case no value has been entered for them. This is an undesirable behavior most of the times especially when the default values have other meaning in your domain context.


SpringBatch Chunking: Tip off

I have been working with Spring Batch framework for a while now. As much as I have found it comfortable to work with, there are instances when I get confounded by some interesting batch specific idea.

For instance, when you use chunking for data processing, it is a common thing to use a reader, processor and writer for processing the data chunks. 

Note here that the chunk size is not 1 unless it has been explicitly specified so in the batch description xml.

Remember that exclusivity of private data in the chunk processing pipeline is only between items of different chunks and not between items of the same chunk.

For example:

class MockProcessor implements ItemProcessor <K,V> {

private Map <K,V> cache;

public void Process () {

//use cache
cache.put (k,v);
}

}

Here, the cache contents are private and exclusive only for items belonging to different chunks and not for items in the same chunk. So, if you have any item specific processing to do, remember to set the chunk size to 1 or a better idea would be clear and initialize your cache upon each process invocation.

Wednesday 25 February 2015

Object pooling with valueOf()

We have all wrapped primitive int with wrapper Integer objects.

The regular way to achieve this would be to write:

Integer ONE = new Integer (1);

This would instantiate a new object each time as evidenced by the presence of the new operator. This repeated instantiation is wasteful most of the time.

We can do a one time instantiation of the object and pool the same object to re-use it when it is deemed required later. This may be done with the valueOf method.

Integer ONE = Integer.valueOf(1);

This may be compared to String instantiation by two different techniques:

String happy = "happy"; (pooling)
String happy = new String ("happy"); (non-pooling)