Google
 
Web unafbapune.blogspot.com

Sunday, November 30, 2008

 

Local vs Any address in Cajo

Looking into the cajo source code, I see the default server side host address used by cajo is the local host address. For example,
host = InetAddress.getLocalHost().getHostAddress();
This seems to work in most cases/platforms, except it breaks with the new Mac that I have. How does it break ? Well the server side binding works with the default (local) address, but the client would fail to connect to 127.0.0.1 or localhost. To make the client connect, I needed to figure out the specific local address (like 192.168.0.3) used for the server side binding, and explicitly specify that for the client.

When I tried to see if this was also the case outside cajo using the plain old ServerSocket(int), surprisingly it worked regardless of whether I specified locahost, 127.0.0.1, or 192.168.0.3.

Digging more into the JDK source code, ServerSocket uses an "any address" rather than a "local address" as the default:
InetAddress.anyLocalAddress();
Sadly, this static method is package private.

Anyhow, since the "any address" seems to work more so than the "local address", it would be nice if cajo can change the default server side address to the "any address", which would make cajo "just work" across more platforms.

Saturday, November 22, 2008

 

Warming up a JVM

If you knew the actual set of classes that will be lazily loaded into the JVM for execution, wouldn't it be nice if these classes can be eagerly loaded into the JVM upon start-up ? This particularly matters if you are writing web services, where the latency of the first few requests will be significantly impacted.

Pre-loading classes is easy. But how can we find out the precise set of classes to be preloaded ? It turns out there exists a very nice JVM option:
-verbose:class
Also found an interesting technical report: Eager Class Initialization For Java.

Tuesday, November 11, 2008

 

A little Java constructor pattern

Say we have a class Item:
public class Item {
final String name;
final float val;
final int size;

public Item(String name, int size, float val) {
this.name = name;
this.val = val;
this.size = size;
}
}
and we'd like to initialize an Item array with some values:
Item[] items = {
new Item("A", 3, 4),
new Item("B", 4, 5),
...
};
Wouldn't it be nice if we can simplify it to:
Item[] items = {
Item("A", 3, 4),
Item("B", 4, 5),
...
};
?

Couldn't this be easily done by defining a static factory method with the same name as Item, and then use it via static import ?
public class Item {
// ... same as above

// A static factory method for the syntactic sugar
public static Item Item(String name, int size, float val) {
return new Item(name, size, val);
}
}
Usage:
import static Item.Item;
...
Item[] items = {
Item("A", 3, 4),
Item("B", 4, 5),
...
};

This page is powered by Blogger. Isn't yours?