Monday, February 27, 2012

MyLifeBits - An interesting project in Big Data

I was now a days reading on Big data and got to know about this interesting Microsoft's project. MylifeBits is a lifetime store of everything. It is the fulfillment of Vannevar Bush’s 1945 Memex vision including full-text search, text & audio annotations, and hyperlinks.

The software research:Jim Gemmell and Roger Lueder have developed the MyLifeBits software, which leverages SQL server to support: hyperlinks, annotations, reports, saved queries, pivoting, clustering, and fast search. MyLifeBits is designed to make annotation easy, including gang annotation on right click, voice annotation, and web browser integration. It includes tools to record web pages, IM transcripts, radio and television.

See the demos here:

Friday, February 10, 2012

Maven 3: How to Speed-up Build Process?

I was working on one of the project and one of my worry was that 'mvn clean install' was taking too long. I tried the following options which resulted in faster build. Here are my learnings:

  • Maven Commands: Maven 3 has come up with some advanced commands known as reactors. One of theeir experimental features is Parallel Builds. This build-mode analyzes your project's dependency graph and schedules modules that can be built in parallel according to the dependency graph of your project. The commands are as follows:

    mvn -T 4 clean install         # Builds with 4 threads
    mvn -T 2C clean install        # Runs 2 thread per cpu core

    Second command works better for multicore processors.
  • Using mvnsh: mvnsh(Maven Shell) allows developers to execute maven goals/phases in a pre-loaded shell environment. It is a CLI interface for Apache Maven that enabled faster turn-around. Maven Shell does not come with Maven 3 build. You can download it separately from Sonatype site.

    Following are the excellent article which provides details on mvnsh:

    How to use mvnsh?

    After downloading the mvnsh, you can use it in following manner:

    Run mvnsh.bat from /bin directory. This will take you to maven shell environment with mvnsh(/):bin> shell option.

    Now traverse to your project's POM.xml directory using:


    This will take you to mvnsh(/):<YOUR_POM_XML_DIR>> shell in mvnsh environment and now you can run your normal mvn commands.

Wednesday, February 8, 2012

What Do You Need to know while Moving Your Source Code to Java 7?

Are you shifting your project to Java 7? Almost all existing programs should run on Java SE 7 without modification. But still there are some minor potential source and binary incompatibilities in the JRE and JDK that involve rare circumstances and "corner cases".

Oracle documentation here explains in detail all the incompatibilities which an application developer needs to take care while moving the project to Java 7. I strongly recommend to read all the incompatibilities from the Oracle site. However, in order to save time, few of the more commonly used are described below:

Area: JSR 334
Synopsis: Improved Exception Handling May Cause source Incompatibility
Description: The following example shows two source incompatibilities: 
class Foo extends Exception {}
class SonOfFoo extends Foo {}
class DaughterOfFoo extends Foo {}
try {
   throw new DaughterOfFoo();
} catch (final Foo exception) {
   try {
      throw exception; // used to throw Foo, now throws DaughterOfFoo
   } catch (SonOfFoo anotherException) { // Reachable? }

 The first incompatibility is that the throw exception; statement throws a Foo exception in JDK 6, but throws a DaughterOfFoo exception in Java SE 7.
 The second incompatibility is that the catch (SonOfFoo ...) statement compiled under JDK 6 but, under Java SE 7, gets the following error: 
error: exception SonOfFoo is never thrown in body of corresponding try statement
 Such code is likely to be rare, but should be fixed by removing the non-reachable statements.
Nature of Incompatibility: source

Area: API: Language
Synopsis: Spec for java.lang.Float.parseFloat(String) and parseDouble(String) Updated to Document Exception
Description: The java.lang.Float.parseFloat(String) and java.lang.Float.parseDouble(String) methods have been throwing an undocument NullPointerException when passed a null argument since the methods were introduced in J2SE 1.2. The spec has been updated to document the NPEs.
Nature of Incompatibility: behavioral

Area: API: Language
Synopsis: java.lang.Character.isLowerCase/isUpperCase Methods Are Updated to Comply with the Specified Unicode Definition
Description: The specification and the implementation of the isLowerCase and isUpperCase methods have been updated to comply with the Unicode Standard definition to be GD=Lu/Ll + Other_UpperCase/LowerCase. Two new methods, java.lang.Character.isAlphabetic(int) and java.lang.Character.isIdeographic(int) have also been added.
Nature of Incompatibility: behavioral

Area: API: Utilities
Synopsis: Inserting an Invalid Element into a TreeMap Throws an NPE
Description: Due to an error in java.util.TreeMap, it was previously possible to insert invalid null elements and elements not implementing the Comparable interface into an empty TreeMap or TreeSet. Only a single invalid element could be inserted into an empty TreeMap or TreeSet; additional elements would cause the expected NullPointerException or ClassCastException. Most other operations upon the collection would also fail. As of Java SE 7, inserting an invalid null element or an element not implementing Comparable into an empty TreeMap or TreeSet throws a NullPointerException.
Nature of Incompatibility: behavioral

Area: API: Utilities
Synopsis: Formatter.format() Now Throws FormatFlagsConversionMismatchException
Description: The Formatter.format(String,Object...) method now throws a FormatFlagsConversionMismatchException exception when the "#" flag is specified for conversion "s" and the following argument is not a Formattable instance (including the special case "null").
Nature of Incompatibility: behavioral

Area: API: NIO
Synopsis: The Behavior for Several java.nio.channels.DatagramChannel Methods have Changed
Description: The DatagramChannel is the selectable channel for datagram-oriented sockets in the java.nio.channels package. The behavior for the send, receive, and connect methods have been modified. To restore the previous behavior, the property can be set to the value of "1.4", "1.5", or "1.6". See the DatagramChannel class specification for more information.
Nature of Incompatibility: behavioral

Area: API: Internationalization
Synopsis: Separation of User Locale and User Interface Locale
Description: The default locale can be independently set for two types of uses: the format setting is used for formatting resources, and the display setting is used in menus and dialogs. The new Locale.getDefault(Locale.Category) method takes a Locale.Category parameter. Previous behavior can be restored by setting the sun.locale.formatasdefault system property to true.
Nature of Incompatibility: behavioral

Synopsis: New RowSetFactory Interface to allow Creation of a RowSetFactory
Description: New API was introduced to support RowSet 1.1 and, specifically, the ability to write more portable code by creating a RowSetFactory. As part of this update, the definition of some constants has changed slightly, but should not affect most users.
Nature of Incompatibility: source

Synopsis: New JDBC Methods, Including new Methods in Interfaces
Description: For the Java SE 7 release, there are new methods to support JDBC 4.1. This includes methods added to the java.sql.Connection, java.sql.Driver, javax.sql.CommonDatasource, and java.sql.Statement interfaces. Because all methods of an interface must be implemented, previous code that uses these interfaces will not compile on Java SE 7 unless you add the new methods. See the JDBC documentation for more information.
Nature of Incompatibility: source

Area: API: Utilities
Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException
Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation.
 If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.
Nature of Incompatibility: behavioral

Area: API: IO
Synopsis: and setWriteable Methods Have New Behavior
Description: As of JDK 7, on Windows, the setReadOnly and setWritable methods no longer set the DOS readonly attribute on directories. This means that these methods will fail, by returning false, if the file is a directory. To preserve the relationship with canWrite, the canWrite method returns true if the file is a directory.
 Applications that want to set directories on Windows to be read only must use the new API. In particular, the Files.isWritable method takes into account the effective access (as determined by the file's discretionary access control list) and whether the file is located on a writable volume.
Nature of Incompatibility: behavioral

Tuesday, February 7, 2012

Java 7: Garbage First(G1) Collector

The Garbage-First Garbage Collector (or G1 GC for short) is a new GC that is being introduced in the Java HotSpot VM in JDK 7. In Java 7, Concurrent Mark-Sweep Collector (CMS) is replaced by G1 and enabled by default. It is a server-style collector, targeted at multi-processor machines with large amounts of memory that meets a soft real-time goal with high probability, while achieving high throughput.

This allow users to specify a soft real-time goal, stating their desire that collection consume no more than x ms of any y ms time slice. By making this goal explicit, the collector can try to keep collection pauses as small and infrequent as necessary for the application, but not so low as to decrease throughput or increase footprint unnecessarily.

There are two major differences between CMS and G1.

  • G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grain free lists for allocation, which considerably simplifies parts of the collector and mostly eliminates potential fragmentation issues.
  • G1 offers more predictable garbage collection pauses than the CMS collector and allows users to set their desired pause targets.

How Does G1 Work?

Please read Dr. Dobbs article which explains in detail the working of G1.

Further Reading about G1:

Monday, February 6, 2012

JDBC 4.1: 'try-with-resources' Statement Automatically Close Resources

Before Java 7, the biggest worry while working with JDBC was to close the resources. If we don’t close them, we can be left with unclosed resources to the database. This means that we could eventually run out of resources.

Java 7 solves this problem by introducing a 'try-with-resources' statement which automatically close resources of type java.sql.Connection, java.sql.Statement, and java.sql.ResultSet objects, regardless of whether a SQLException or any other exception has been thrown. A try-with-resources statement consists of a try statement and one or more declared resources. This statement ensures that any resources declared in the try statement will be closed automatically.

NOTE: If multiple resources variables are declared in try statement, they are closed in reverse order in which they are declared.


public static void showEmployees(Connection con) throws SQLException 
    String query = "select EMP_NAME, EMP_ID, EMP_ADDRESS, 
                     EMP_DESIGNATION from EMPLOYEES";

    try (Statement stmt = con.createStatement()) 
        ResultSet rs = stmt.executeQuery(query);

        while ( 
            String empName = rs.getString("EMP_NAME");
            int empID = rs.getInt("EMP_ID");
            String empAddress = rs.getFloat("EMP_ADDRESS");
            String empDesignation = rs.getInt("EMP_DESIGNATION");
            System.out.println(empName + ", " + empID + ", " + 
                      empAddress + ", " + empDesignation);

The above highlighted statement is a try-with-resources statement declares one resource stmt which will be automatically closed as soon as try block terminates.

Wednesday, February 1, 2012

Java SE 6 End of Life (EOL)

Java SE 6 will no longer be publicly available after July 2012. Customers interested in continued access to critical bug fixes and security fixes as well as general maintenance for JDK 6 have the option to purchase Oracle Lifetime Support.

Further details are available on Oracle site:

Is it the time to think about shifting to Java 7?