SyntaxHighlighter

Tuesday, July 5, 2011

Java 7 New Features

Following are few new features of Java 7

1. Introduction of WatchService? (NIO)

A watch service that watches registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.

2. Strings in Java Switch construct
Will be able to String case based switch constructt; prior to java 7 switch was only allowed with int & enums

3. Introduction of autoclosable interface and try with resource construct
Developers will get rid of closing resources in finally block. JDBC 4.1 has accomodated these constructs in their classes.
4. Underscores in Numbers
Increasing readability of numbers.
int phoneNumber = 555_555_1212;
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumbers = 999_99_9999L;
float monetaryAmount = 12_345_132.12;
long hexBytes = 0xFF_EC_DE_5E;

5. Introduction of Closures
Much awaited & requested closures are part of JDK7 on experimental basis ,RI for it will be included in JDK8 TCK as per JSR


6 Addition of forkJoin for parallel processing
Combined JSR 166 into JDK

Monday, July 4, 2011

Spring - MongoDB (Document Oriented Database)

We often hear following terms viz high scalability, elasticity , high availability..
In addition to this, we do face performance issues with humongous data , which brings our critical applications to deadly halt.

I have experienced such performance problems numerous times in my professional carrier (almost 6 years til now), we usually solved these problems with trade offs like minimizing data fetched / even gone ahead and moved on to more cores and disks etc or tuning and other stuff.

Having such a experience , I was always looking to try out thing called NOSQL databases, recently I got chance & time

My first reaction to Mongo was ..."Its Kool !!".

I tried MongoDB along with spring , (spring-data mongodb), as always spring provides nice abstraction and spring style templates to interact with mongo.

 Setting up MongoDB is very simple,
1. Download zip from http://www.mongodb.org/downloads appropriate for your platform.For me its mongodb-win32-i386-1.8.1.zip
2. Unzip the zip the downloaded zip to a directory, it will have a bin directory with mongo executables.
3. To get mongo working create "C:\data\db\"  ,as mongo uses this location as default to store file system.
4. You are almost done, now go to bin directory and run mongod.exe , this will start up database server.
5. "help' command will help you after that, to explore more functions like creating database, switching, adding users, adding updating/ removing collections.

Now, will move to spring -mongo part,

Spring comes with MongoTemplate, 
sample configurations , that I tried are as follows



The class MongoRepository is a implementation of below interface with mongotemplate instance injected in it



Constructor of MongoRepository initializes a mongoTemplate instance as follows



 It works very smoothly..

 @Override
    public void saveEntity(String collectionName, Entity object,com.dhananjay.ithinker.persister.Repository.Mode mode) {
        log.info("Saving record for in Collection Name "+collectionName);
        switch(mode){
            case INSERT:
                this.mongoTemplate.insert(collectionName,object);
                break;
            case UPDATE:
            { 
                Map objectMap;
                try {
                    objectMap = org.apache.commons.beanutils.BeanUtils.describe(object);
                    Update update = new Update();
                    if(objectMap != null && !objectMap.isEmpty()){
                      
                        for(Map.Entry entry : objectMap.entrySet()){
                            if(entry.getValue() != null)
                                //update.addToSet(entry.getKey(), entry.getValue());
                                update = update.set(entry.getKey(), entry.getValue());
                            this.mongoTemplate.updateFirst(collectionName,new Query(where("_id").is(object.getId())), update);
                        }
                    }
                  
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Error while update ",e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Error while update ",e);
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException("Error while update ",e);
                }
                break;
            }
            case DELETE:
                this.mongoTemplate.remove(collectionName,new Query(where("_id").is(object.getId())));
                break;
            default :
                throw new UnsupportedOperationException("This mode for DB operation is not yet supported "+mode);
      
        }
      
        log.info("Saving record for in Collection Name is completed"+collectionName);
    }


@Override
    public T getEntityByColumn(String column, String value,String collection, Class clazz) {
        return this.mongoTemplate.findOne(collection, new Query(where(column).is(value)),clazz);
    }

    @Override
    public T getEntityById(String collectionName, long id,Class clazz) {
        log.info("Fetching single record for id "+id +" Collection Name "+collectionName);
        return this.mongoTemplate.findOne(collectionName, new Query(where("_id").is(id)),clazz);
    }


MongoRepository

 HUMONGOUS  :)
Thanks,
Dhananjay