Tuesday, November 04, 2008

Deleting SVN Directories from a Project

Sometimes you just need to get rid of those pesky subversion folders because you can’t export or something so here’s the quick and easy way to do it in windows:

  • Be sure you can see hidden files and folders (Advanced folder settings)
  • Open Windows Explorer in the root folder of your project
  • Explorer
  • In the search bar (or Ctrl+F for older versions of windows), type “.svn”
  • Sort by name
  • Delete all the ones that are folders (not the ones that are files)


Tuesday, October 07, 2008

Callable and the Case of the Disappearing Exceptions

If you have a class that implements Callable, but you don't plan on using the Future that is returned after you call Executor.submit(), you may lose any exceptions that are thrown. They literally disappear into the void. No log whatsoever.

So it is good practice to treat it a bit like you would with a Runnable and wrap the code in your call method with a try/catch to print the exception or log it somewhere.

Thursday, October 02, 2008

How To Give a User Root Access on Linux

NOTE: I take no responsibility for using the following and do not recommend using it.

But for those that want it:

usermod -G root username

This changes the user with ‘username’ to the root group and thus gives root access.

And remember, as Uncle Ben says, “with great power comes great responsibility”.

Wednesday, October 01, 2008

Using JAXB Without Annotations

The latest JAXB included with JDK 1.6 is really a great addition to the JDK making it easy to send objects around in XML format without requiring any third party libraries. All you have to do is put an @XmlRootElement annotation on a class and then call:

marshaller.marshal(Object, OutputStream);

Example class using @XmlRootElement:

class MyClass {
  String name;
  String content;

To read the object back in, you simply call:

MyClass object = unmarshaller.unmarshal(InputStream);

But what if you are building a library that doesn’t know what the class is and doesn’t have the @XmlRootElement annotation?  Well it turns out that it’s almost just as easy without it you just need to let JAXB know what class you expect.

First get the JAXBContext like this:

JAXBContext context = JAXBContext.newInstance(object.getClass());

To marshal:

marshaller.marshal(new JAXBElement(new QName(object.getClass().getSimpleName()), object.getClass(), object), out);

To unmarshal:

JAXBElement o = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
MyClass object = o.getValue();

That’s it.

Sunday, May 25, 2008

Warming Up the Hibernate Second Level Cache

I recently ran into a problem where Hibernate took forever to load up a result set because it was executing two additional queries for every object in the list to get it's @ManyToOne references. With thousands of objects returned, this meant thousands of individual sql select queries! I am using the JPA EntityManager throughout these examples.


public class MyEntity{
public MyEntity2 getMyEntity2(){

public MyEntity3 getMyEntity3(){


I had to use these many-to-one references so fiddling with lazy loading wouldn't help my situation. I was already caching the result collection after the first load, but it still was taking minutes to load up the first time and there was less than a thousand results, what gives? But 1000 * 2 extra sql select queries is 2000 individual queries which is just not good. Even the Hibernate second level cache didn't help because it still needs the first load.

There must be some way to make this startup faster right??

So I starting thinking, "how can I get these things to load up in the fewest number of sql queries?". Then it struck me, I'll warm up the cache by querying for all of the ManyToOne objects on startup, for instance:

em.createQuery("select me2 from MyEntity2").getResultList();


em.createQuery("select me2 from MyEntity3").getResultList();

Now when I run the original query for MyEntity, the result is almost immediate. I turned ~2000 database hits into 3!

More Reading:

Tuesday, May 20, 2008

Automated, Real-time Hard Drive Backup

As I've said before, I'm a big advocate of backup systems because I've gone through the hell of hard drive failures. A 2007 study by The Computer Science Department of Carnegie Mellon University states that:

We find that in the field, annual disk replacement rates typically exceed 1%, with 2-4% common and up to 13% observed on some systems.
With statistics like that, you better believe it will happen to you some day.

I currently use Mozy for remote backup in case I throw my computer out the window because Vista has me so frustrated. Remote backups are great and a must have in my eyes (they also give you that warm fuzzy feeling), but they are not perfect. For instance, it could take weeks to actually recover your 50 gigs of data from a remote service. You will eventually get it all, but it won't be a quick recovery.

Want zero down time? Enter RAID. Accordance Systems has a really cool backup device called the ARAID that automatically and transparently backs up your hard drive by mirroring it. Now mirroring hard drives isn't anything new, but the cool part about the ARAID device is that it acts like a normal hard drive on your computer and you can simply add it to your box without having to do anything crazy like reformatting your hard drive or installing raid controllers to get it running. And if a hard drive fails, you won't be waiting weeks to recover, in fact you won't even have to stop working because the mirrored drive takes over.

Here's the skinny on how it works:
  1. You shutdown your computer (or course)
  2. Replace your master hard drive with the ARAID unit
  3. Put your master hard drive in the top bay of the ARAID
  4. Now you start up your computer and it runs like normal as if the ARAID weren't there
  5. Now you insert a second drive into the bottom tray (yes, you can do this while the drive is
  6. The ARAID will now synchronize the master drive to the second drive on the fly
  7. Now you have a fully functioning mirrored RAID 1 array where your data is always backed up to the second drive so if one drive fails, the other takes over with no downtime.
This is pretty cool stuff, especially because they made it so easy to setup. I would definitely recommend using something like this along with a remote backup solution just in case your office burns to the ground.

Thursday, March 06, 2008

How To Set Up Dynamic DNS for your Amazon EC2 Instance

Here's a step by step to setting up dynamic dns for you EC2 instance. This is required because EC2 does not have static IPs (yet).

  1. First of all, you need a dynamic dns account. You can get one at www.dyndns.com or my personal favorite, www.zoneedit.com.
  2. After you sign up, be sure to add your domains to their system.
  3. Download dyndns client 
    1. run: wget http://cdn.dyndns.com/ddclient.tar.gz
    2. The download includes Installation Instructions in the README, but the step by step is below
  4. tar -zxvf ddclient.tar.gz
  5. cd ddclient-X.Y
  6. cp ddclient /usr/sbin/
  7. mkdir /etc/ddclient
  8. nano -w /etc/ddclient/ddclient.conf
    1. Then fill in config file:


# Basic configuration file for ddclient
# /etc/ddclient.conf
use=web, web=checkip.dyndns.org/, web-skip='IP Address'


# Basic configuration file for ddclient
# /etc/ddclient.conf
use=web, web=checkip.dyndns.com/, web-skip='IP Address'
custom=yes, example.com

More info on configuration

Continuing on:

  1. cp sample-etc_rc.d_init.d_ddclient /etc/rc.d/init.d/ddclient
  2. /sbin/chkconfig --add ddclient
    1. This makes it startup automatically next time you reboot
  3. /etc/rc.d/init.d/ddclient start
    1. This is to start it the first time by hand
    2. DON'T RUN THIS until your server is fully setup if you are moving servers because people will start hitting this server after you run this

That's it, not too difficult and gives you a real domain name to point at your EC2 instance.

Sunday, January 27, 2008

Transparent Persistence is Here

The idea of not having to deal with storing your application's data has long been a pipe dream, but it seems that the folks behind the db4o object database have made it a reality. I've written a couple of posts in the past about db4o and how easy it is to use and it appears to be getting easier and easier with Transparent Activation and Transparent Update.

Transparent activation is basically just lazy loading objects in the object graph on an as needed basis. Transparent Update is the opposite; storing objects when they get dirty without you having to explicitly say so.

You can check out the press release on Transparent Update here.

Thursday, January 10, 2008

Getting Started with JAX-RS and Jersey - Simple REST Web Services for Java

I came across the need once again to build a public facing web services API and we all know that to be in the cool gang these days, you have to do it REST style. Now I've been a big fan of REST over SOAP (or any other type) for a long time for one simple reason: it's simpler. And simple is king in my books. Anyways, I decided to see if anyone had come up with a good, simple REST framework for java since there hasn't really been anything that I've found over the years.

With a little digging, I found what I had been looking for and it goes by the name JAX-RS (jsr311). Now this is pretty bleeding edge, the buzz has just started in the past few months and there's not much around to help you out. But as you should see in the samples below, it's well worth checking out.

Now lets get into the nitty gritty:

1. Download Jersey and add required jars to classpath

Jersey is the reference implementation, get it here.

Now you only need three jars from the Jersey download:

  • asm-3.X.jar
  • jersey.jar
  • jsr311-api.jar

That's all you need. Compare that to Axis and this alone should make you want to switch.

2. Add the Jersey Servlet to web.xml

You can use a lightweight http server that comes with the JDK or Jersey, but who's gonna do that in the real world? So here is how to get it running in Tomcat.


3. Make a service bean

Now all you have to do is make service beans via JAX-RS annotations. This is remarkably easy. We'll start with the simplest Hello World example:

@Path("/helloworld") // sets the path for this service
public class HelloWorldBean {
@GET // This method will process GET
// requests to the @Path value set above
@ProduceMime("text/html") // content type to output
public String getClichedMessage() {
return "Hello World";

4. Run Tomcat and browse to /helloworld

Startup tomcat with your /WEB-INF/web.xml, the 3 jars in /WEB-INF/lib, and your compiled HelloWorldBean in /WEB-INF/classes. Now browse to http://localhost:8080/yourContext/rest/helloworld

You should see in your browser: Hello World.

That should give you all the basics you'll need to get started. Next time I'll post how to easily send XML responses directly from your objects.