A recent attempt at creating a unique identifier at work was shot down in flames by Findbugs. My naïve attempt wasn’t very thoughtful but seemed to work.

Integer.toString(Math.abs(random.nextInt()))

Findbugs indicated there was a possibility that I might end up with a negative value even though I “cleverly” used Math.abs().

RV: Bad attempt to compute absolute value of signed random integer (RV_ABSOLUTE_VALUE_OF_RANDOM_INT)

If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well

Even though I didn’t need this random id to be perfect — the unintended side-effect wasn’t a technical problem since I used it as a String — I still wanted to fix it. Stackoverflow to the rescue.

A question-and-answer from Stackoverflow pointed the way to the solution: java.util.UUID. Somehow, I missed that Sun had added Java’s own universally unique identifier (UUID) generator in Java 1.5.

In the end, I happily replaced my homemade id generator with Java’s.

UUID.randomUUID().toString()

The output of which is something like 1c312843-8903-411f-88b2-ff1b92ca80ba.

Cross-posted at http://java.dzone.com/articles/global-unique-identifiers-java.

 

Check out Julien Ponge’s article on Fork and Join in Java 7.

 

Every so often I get myself confused about Java’s often-obtuse handling of dates and timezones. So, for my future self’s benefit, here are some reminder notes… Continue reading »

 

Post updated 2011 Jan 19: Given the opportunity to install 10.10, I have added different instructions for Ubuntu 10.10 (Maverick Meerkat).

Much to my surprise, but probably not those of the Open Source Ruling Class, Sun’s Java 6 has been removed from the Ubuntu Multiverse. Apparently the Ubuntu folks have started putting some weight behind their recommendations for switching to the “OpenJDK.” Fortunately, the official, “proprietary” Java is still available through another Ubuntu repository.

Ubuntu 10.10

To install Sun’s Java 6 JDK on Ubuntu 10.10, add the Sun Java6 Community PPA and install:

add-apt-repository ppa:sun-java-community-team/sun-java6
apt-get update
apt-get install sun-java6-jdk
update-java-alternatives -s java-6-sun

Ubuntu 10.04

To make Sun’s Java 6 JDK available on Ubuntu 10.04 add the new repository like so:

add-apt-repository "deb http://archive.canonical.com/ lucid partner"
aptitude update
aptitude install sun-java6-jdk
update-java-alternatives -s java-6-sun
 

If you’re using ehcache’s disk persistence feature, which allows the cache to survive across JVM restarts, be sure to shut down ehcache properly.

To do so when using ehcache within a webapp, simply add its ShutdownListener as a listener in web.xml.

<listener>
    <listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener>

Alternatively, or when not using ehcache inside a webapp, instruct ehcache to register its own shutdown hook by setting a system property.

net.sf.ehcache.enableShutdownHook=true

If you forget one of the above, your persisted cache may not be up-to-date, or worse, not persisted at all.

© 2011 And now here's something… Suffusion theme by Sayontan Sinha