The road so far….

November 12, 2010

Use java.util.prefs.Preferences instead of java.util.Properties

Filed under: java — Tags: , — Rahul Sharma @ 10:07 pm

A typical installer for an application needs to ask the user a couple of options and some of these are configuration questions e.g. the port on which the application should run, how it should run etc. The application has to remember these options and use them in every run. Standard manner of solving such a problem is to write these options in a properties file which can be loaded at the start-up of the application. But then again the problem shifts to some other area i.e.  remember the install path and then load the required properties file from the installed path.

Remembering installed path can be solved by setting an environment variable e.g. MYAPP_HOME. The variable can be initialized with the required value  while installing  so that every time the application gets loaded the variable will be set. This is a typical solution that is employed in most of the projects. (more…)

Advertisements

September 21, 2010

TIP! : use ScheduledExecutor instead of Thread for polling or infinite loops

Filed under: java — Tags: — Rahul Sharma @ 10:34 pm

Often we have the requirement of polling/ long or infinite  running loops that should continually run and then break on a certain condition. These loops are often constructed using the Runnable interface and are scheduled as separated threads using the  Thread class.

A typical code looks some thing like this: (more…)

July 5, 2010

Maps can avoid java.lang.OutOfMemoryError

Filed under: java — Tags: — Rahul Sharma @ 9:19 pm

Often we try to use java Maps for some kind of caching purpose.  Usually we keep adding data to Map either in form of new entries or keep updating old entries with new data. In any case Map starts to bloat up,  keeping data in it infinitely or until we remove it. In such a case objects keep piling up on the Java Heep space and we eventually get an java.lang.OutOfMemoryError: Java heap space exception.

But we can avoid such a situation and create an extensible Map that can be used to hold information without Out-Of-Memory exception,  let me show you how. (more…)

June 27, 2010

Enums are Not Singleton !!

Filed under: java — Tags: , — Rahul Sharma @ 9:57 pm

After going through  couple of  blogs on Enums and associated design patterns, I started testing Enums and their behaviour especially in relation to Singleton pattern.  So I implemented a simple Enum and started to test it.

public enum MyApplicationFactory {
   ModuleOne;
   void operate() {
     System.out.println("Working On it!! ");
   }
}

public class MyAppFactoryTests {
  @Test
   public void testOne() {
     System.out.println(MyApplicationFactory.ModuleOne);
     MyApplicationFactory.ModuleOne.operate();
  }
}

This test went fine, so I added one more constant “ModuleTwo ” to Enum and that also went fine. But the problem came when I implemented a constructor in the Enum.  So now my Enum looks like this : (more…)