[Home] [Index] [Next]

Embedding Oddjob

Running Oddjob from Code.

A Simple Example

It's Hello World of course:

        
        String xml = 
            "<oddjob>" +
            " <job>" +
            "  <echo id='hello-job'>Hello World</echo>" +
            " </job>" +
            "</oddjob>";
        
        Oddjob oddjob = new Oddjob();
        oddjob.setConfiguration(new XMLConfiguration("EMBEDDED XML", xml));
    
        oddjob.run();
        

Which is:

  1. Create an instance of Oddjob.
  2. Give Oddjob some configuration. In this case it's a string of xml, but it could be a file, or a class path resource.
  3. Run Oddjob.

And that's it.

Looking Up Beans

With three lines of code it's possible to get at all Objects that have been registered within Oddjob with their id attribute. Because three lines of code seemed a bit much a utility class OddjobLookup was created.

We can now get there in one line thus:

        
        String greeting = new OddjobLookup(oddjob).lookup("hello-job.text", String.class);
        

By providing the property type, Oddjob will do the conversion for us. There is also a lookup method that doesn't take the second argument and returns the raw Object

Cleaning Up

And when you've finished with Oddjob we mustn't forget to clear those resources:

        
        oddjob.destroy();       
        

If you don't do this, it's possible that threads will be left running.


[Home] [Index] [Next]