Tuesday, February 27, 2007
[quick tips] To Speedup your junit testing in Ant
Now, before u look into your unit testing code to improve the performance, why don't have a check on your Ant Junit Target (I discovered this accidentally yesterday, as I am frustrated by the slow build process). If u have setup ur Ant to fork a new JVM for unit testing target, by default, Ant will fork a new VM per Test Class, which is slow, and expansive (imagine forking 1000+ jvm for your project). To correct this, we just needs to set correct forkmode="perBatch".
i.e
junit fork="true" forkmode="perBatch"
My test result, before forkmode="perBatch", my test process take ~= 16 minutes
after, my test process take < 2 minutes
Cheers, and happy coding
Monday, February 12, 2007
[Tech Tips] Implementing Command Design Pattern via Spring Framework - Part II
The RemoteControl class needs to hold list of supported commands, and needs to delegate request to respective command class base on the pass in command string. Here is the RemoteControl source:
1The RemoteControl will transfer a list of supported commands to internal HashMap using commandString as object key, thus, removing the needs of iterating commands list for each new command execution.
2 package blog.coolboy.springexample.cp;
3
4 import java.util.List;
5 import java.util.HashMap;
6 import blog.coolboy.springexample.cp.command.CommandType;
7 import blog.coolboy.springexample.cp.command.Command;
8
9
10 public class RemoteControl {
11
12 private HashMap<String, Command> supportedCommands = null;
13
14 public RemoteControl() { }
15
16
17 public void setListOfSupportedCommands(List<Command> commandList) {
18 supportedCommands = new HashMap<String, Command>();
19 for(Command cmd : commandList) {
20 supportedCommands.put(
cmd.getCommandType().getCommandString(),
cmd );
21 }
22
23 }
24
25 public void execute(String cmdString) {
26 Command cmd = supportedCommands.get(cmdString);
27 if(cmd != null) {
28 cmd.execute();
29 } else {
30 System.out.println("The cmd->" +
cmdString + " is not supported!");
31 }
32 }
33
34 }
Here's the fun part, we will inject list of supported commands to the RemoteControl above using Spring XML configuration, as shown in code below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- listing of supported Commands here -->
<bean id="tvOnCommand"
class="blog.coolboy.springexample.cp.command.impl.TVOnCommand"/>
<bean id="tvOffCommand"
class="blog.coolboy.springexample.cp.command.impl.TVOffCommand"/>
<bean id="lightOnCommand"
class="blog.coolboy.springexample.cp.command.impl.LightOnCommand"/>
<bean id="lightOffCommand"
class="blog.coolboy.springexample.cp.command.impl.LightOffCommand"/>
<bean id="remoteControl"
class="blog.coolboy.springexample.cp.RemoteControl">
<property name="listOfSupportedCommands">
<list>
<ref bean="tvOnCommand"/>
<ref bean="tvOffCommand"/>
<ref bean="lightOnCommand"/>
<ref bean="lightOffCommand"/>
</list>
</property>
</bean>
</beans>
That's all, our remote control class is finished. To roll out new Command, developer just needs to
1. declare the new command type, and it's command string at the CommandType enum
2. implements the command, and
3. inject new command via Spring Configuration.
Here are remoteControlService class to test the remoteControl
1 package blog.coolboy.springexample.cp;Here are the try run:
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7 import org.springframework.context.ApplicationContext;
8 import org.springframework.jms.listener.DefaultMessageListenerContainer;
9
10 public class RemoteControlService {
11 public static void main(String [] args) {
12 ApplicationContext context =
new ClassPathXmlApplicationContext("/conf/remoteControlConfig.xml");
13 RemoteControl remoteControl = (RemoteControl) context.getBean("remoteControl");
14 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
15 System.out.println("Remote Control Ready, enter 'Quit' to terminate the program");
16 String txtCommand;
17 try {
18 do {
19 System.out.print("Enter your command->");
20 txtCommand = reader.readLine();
21 if(txtCommand.equalsIgnoreCase("quit")) {
22 break;
23 } else {
24 remoteControl.execute(txtCommand);
25 }
26 System.out.println("");
27 } while(true);
28 } catch (IOException ex) {
29 ex.printStackTrace();
30 }
31
32 }
33 }
------------------
Remote Control Ready, enter 'Quit' to terminate the program
Enter your command->tv.on
Switching On TV
Enter your command->tv.off
Switching off TV
Enter your command->light.on
Turning on Light
Enter your command->light.off
Turning off Light
Enter your command->quit
----------------
Isn't this fun? In the next article, I will explain how I would extends the app to support JMS and SOAP command request, using Spring, stay tune.
Friday, February 09, 2007
[Tech Tips] Implementing Command Design Pattern via Spring Framework - Part 1
I been using Spring Framework for pass few months, and I am big fans of the Framework. The framework help a lot, as we don't have to write lengthly, repetitive boring code, most application code are wire via Spring XML configuration, the framework also make our code cleaner, as it provide out of box AOP engine, developer could focus on implementing module core function, and “AOPing” other programming aspects such as transaction control, logging, security, and etc when require.
Thus, to say a big thank u on providing such great framework, and contribute back to the communities, starting from today, I will use this blog to share my experience, tips, and journal of using Spring here. Most of the write up will assume u have some basic idea of using Spring (Just do a google search on Spring tutorial).
So, my very first 2007 technical topic is “Implementing Command Design Pattern using Spring”. I will use the remote control example from very popular design pattern book, “Head First Design Patterns” from OREILLY.
The original requirement of the remote control are
1 . The remote control will have multiple buttons,
2. Each button could be program to turn on/off a household device.
I change the requirement a little, the remote control will not have any buttons, but:
1. It will accept command as a string from a console
2. Each device's command will have unique command String, such as
The very first thing we need to do is to roll out the command interface
1 package blog.coolboy.springexample.cp.command;
2
3 public interface Command {
4 public CommandType getCommandType();
5 public void execute();
6 }
Next, we needs to define an enum to hold all possible supported CommandType, each CommandType holds it's command string, which is unique. That's a helper method to return CommandType base on passing command string.
1 package blog.coolboy.springexample.cp.command;
2
3 public enum CommandType {
4 LIGHT_ON("light.on"),
5 LIGHT_OFF("light.off"),
6 TV_ON("tv.on"),
7 TV_OFF("tv.off");
8
9 private String commandString;
10
11 // return CommandType base on passing
12 // commandString
13 public static CommandType valueOfCommandString(String cmdString) {
14 CommandType theCommandType = null;
15 for(CommandType cmdType : CommandType.values()) {
16 if (cmdType.getCommandString().equals(cmdString)) {
17 theCommandType = cmdType;
18 break;
19 }
20
21 }
22 return theCommandType;
23 }
24
25 CommandType(String cmdString) {
26 commandString = cmdString;
27 }
28
29 public String getCommandString() {
30 return commandString;
31 }
32
33 public void setCommandString(String commandString) {
34 this.commandString = commandString;
35 }
36
37 }
38
Now, implements all supported commands, below is sample code TV ON Command;
1 package blog.coolboy.springexample.cp.command.impl;
2
3 import blog.coolboy.springexample.cp.command.Command;
4 import blog.coolboy.springexample.cp.command.CommandType;
5
6
7 public class TVOnCommand implements Command {
8
9 private final static CommandType cmdType = CommandType.TV_ON;
10
11 public TVOnCommand() {
12 // U suppose to pass in a TV instance here..
13 }
14
15 public CommandType getCommandType() {
16 return this.cmdType;
17 }
18
19 public void execute() {
20 System.out.println("Switching On TV");
21 }
22
23 }
The rest of commands (TV.Off, Light On, Light.Off) follows the same structure..
In part II, we will roll out the RemoteControl class, and use Spring to inject supported commands to the Remotecontrol, and provide remoteControlService as an interactive shell for user to test out the remote control, stay tune.
Sunday, January 21, 2007
Monday, December 04, 2006
Friday, October 27, 2006
Latest US TV Series that I enjoyed
Listed below are few TV series that I am really enjoyed:
1. Heroes (From ABC network) - A series about a group of people all over the world on discovering their special power (e.g. reading people mind, stopping the time, paint the future, and others), and how the discovering process affect the surrounding world, This series is far more better then the "Lost" in terms of character building, plot, and the story. Highly recommended.
2. Justice - "CSI + LA Law", and the formula works..
3. House - Still the best TV series on Medical/Hospital, like the character House, very cool and cruel..
and finally
4. Desperate House Wives, yeh yeh, call me sissy if u like, but I still like the series...So what..
So, whcih shows u will recommends?
Monday, June 19, 2006
New Blog for Donut !
After months of preparation, we finally set up a blog called http://pugdiary.blogspot.com, a blog just for Donut! You can access all Donut's latest photos, adventures, videos and also links to other pug blogs. Check it out, enjoy and please do leave a comment or two !
Cheers,
Coolboy
Monday, May 29, 2006
Playing NDS with bunch of Kids.
On 27 May 2006. Out of curiosity and ahem .... just to feel young again, I purposely dropped by a local NDS group gathering at KLCC yesterday and to check out what's all the hype about NDS wireless game. The NDS group gathers once a month in various locations in KL city.
Here are a couple of photos taken at the gathering:
Me playing wireless "Tetrises" with all the Kids
Me trying to act cool but blur blur ...
Members are engrossed in playing animal crossing (a boring game in my opinion)Wednesday, May 03, 2006
Donut Latest Video Update, May 2006
I am having a lot of fun using Google Picasa Tools to compose and edit all my photos. One of the coolest feature of the tool is it allows me to create video by simply selecting a set of photos from my photo folders. Below is a video created using Donut's latest photos .... hope u'll like it.
Monday, April 03, 2006
[Donut's Diary] - How to resist tempation ...
Most of the time, my father treat me like his precious. He will hug me, kiss me and treat me like his son. Sometimes , he “tortures” me by putting my favorite food beside or on my paws and see how long I can resist the temptation. I think it's my Father's dream for me to break the World Guinness Record and make him famous ... ha ha, in his dream lah! OK! I hate this training so much. Everybody please see the video below and u will know why.
Resist temptation Take I – Which I failed ....
Resist temptation Take II : I break the 25 seconds record ...
Now u see. My father is very mean, isn't he? bow, wow,wow!!!
Friday, March 24, 2006
Looking for Java Developers !!
Wednesday, March 22, 2006
Project All About Sudoku!
I admit that I am very addicted to the game. I must play at least one Sudoku puzzle a day or else I feel that something is missing from me. And it's a very good brain exercise for everyone, is highly recommended for anyone who's older then 30+ and people will start calling u "uncle", like me :-(
I love the game so much that I was actually thinking of putting one Sudoku puzzle as part of the interview questions for a Java developer position which I'm hiring for my present Company. I think that if one can't even solve a simple Sudoku puzzle within a given time frame, their problem solving skill is pretty much limited. Somehow, my Manager rejected this idea.
My "Geek" friend Paul Ho and I had several interesting discussions about the game ... about algorithms on generating a unique Sudoku, what is the best program logic to solve a given puzzle ... so on. As a 120% "geek" than me and a very good Java developer, Paul was able to come out with a Java base Sudoku game under 4K and completed within 1 week time frame. Well done Paul! Bravo !
Paul 4K Sudoku game (download from here) actually triggered me to start my very first open source project called "Project all about Sudoku" hosted at sourceforge.com. The aim of this project is to provide a platform (A Java Swing Application) for developers around the world to participate by delivering various kind of plugins to make this addictive game more fun, entertaining and most importantly ... Free For All!.
Well, this project just went "live" yesterday and u can download a "SQLPlus" version of the game. Appreciate if u could check it out and do tell me what do u think. I could use all your support to boost the project ranking.
Monday, March 06, 2006
Coolboy at Bristol
BLIZZARD !!!!
On this day, a huge blizzard of snow came down out of nowhere as I was walking to the Office. The snow melted away during lunchtime.

Eversince I touched down in Bristol, UK the weather here has been between 0 - 5 degrees celcius. At night, the temperature can go down to -8 degrees. It's so cold that I have not travelled anywhere, not even to the City. I prefer to stay in my warm heated room.
After our morning breakfast, me and my colleague walk to our Office from The Rookery 227 B&B Inn, work from 9 till 5:30pm, walk back and eat microwave foods, do some surfing, watch TV and go to sleep.
During weekends, we go to a nearby supermarket, get more microwave foods, juices and some fruits. Why buy microwave foods, you may ask? Well, because it’s too bloody cold to go outside for proper dining. So there isn’t much I can write about. Especially when u feel very moody and miserable … the only things to get me going is the yummy breakfast … which is the same old, same old (see above pic) each morning.
Whoopee ! The sun is out today and weather is actually warm enough for me to travel into the city. I took a bus from Bristol (Paid 3.50 pounds - so expensive for half hour bus ride !!!!) to the famous Bristol Suspension Bridge.
The picture is actually made up by two pictures. The bridge is too long to fit in one camera shoot.
Taken from other side of the bridge. (risked my life to take this picture) I had to stand near to a cliff in order to show my readers how far above ground the bridge is build upon.
This is a picture of me on the bridge ... just to prove that I was actually there !
Tuesday, February 14, 2006
Donut Diary: Bapa, don't go.
I found out today that my Baba have to travel to Bristol, UK, for job training, and he will be there for a month long..my god...who's going to sleep with me? who's going to clean my cage? who's going to give me daily training, and most importantly, who's going to bath me..and feed me, and “sayang” me, and clean my wrinkle face, sing night night song for me...
I don't want u to go!!! I don't care.....srr...
So the moment he bring up the big suitcase for packing, I straight away jump into and “kacau” him...
Please dont't go!
Friday, February 10, 2006
I am old today!
I needs some ideas to feels young again, the concept of “middle age man” still very far from me..thus please help me if u have any idea to make one feels young again. Listed here are my so call new year resolutions to avoid middle age man crises:
1. To feels young, one needs to be around with young peoples –one way to achieve this is to hire more young people (Java peoples) as my team members, then I will understand their “culture”, talk their language, play their games, and most importantly, feels young again.
So, if u have more then 3 years development with Java, knows J2EE, Hibernate, Spring, and other open source frameworks, and most importantly, you are young (I mean you feels young everyday, physical age is not important here), and you are fun and outgoing people, like to work with a “ahem” easy going and fun, and not so old manager...do help me by dropping me your resume at my email at khoo dot james at gmail dot com (Will let u know my new company later)
2. I needs to go to Pulau Perhentian often, that's place is a “energy recharge” place for me. Clear water, clean air, and most importantly, good looking young people, like young Japanese girl likes to go there. Guarantee will feels young there, don't believer me, here a picture of Pulau Perhentian taken from CNY break:
3. To get Diving license by this year.. Needs to have one dive at Malaysia Famous diving spot, at Pulua Simpadan before I reach 35...very very important.
Well, that's all folks...Feels young! and act young! Here a latest Donut's picture wishing u all...”Gong Xi Fai Chai”










