[Home] [Index] [Previous] [Next]

Custom Jobs

Writing an Oddjob job.

Background

Oddjob's philosophy is to be as unobtrusive as possible. A job in Oddjob is just an java.lang.Runnable or a java.util.concurrent.Callable. For all but the most advanced interaction with the framework, you don't need Oddjob on the classpath.

Hello World

Here's how easy it is to write a job:

package org.oddjob.devguide;

public class HelloWorldJob implements Runnable {

    public void run() {
        System.out.println("Hello World!");
    }
}

To get Oddjob to run our job we need to create a configuration file:

<oddjob>
    <job>
        <bean class="org.oddjob.devguide.HelloWorldJob"/>
    </job>
</oddjob>

And run it:

$ java -jar run-oddjob.jar -cp examples/classes -f examples/devguide/hello1.xml
Hello World!

Notice that the classpath option -cp is after the -jar. This is Oddjobs option, not Java's. This is because -jar ignores any existing classpath and any classpath option.

We can now also load it in Oddjob Explorer (but we need still need the classpath).

$ java -jar run-oddjob.jar -cp examples/classes examples/devguide/hello1.xml

We get:

Hello in Oddjob

Hello Rod, Jane, and Freddy

Here's a configurable job.

package org.oddjob.devguide;

public class HelloPeopleJob implements Runnable {

    private String[] who;
    
    public void setWho(String[] who) {
        this.who = who; 
    }
    
    public String[] getWho() {
        return who;
    }
        
    public void run() {
        for (int i = 0; i < who.length; ++i) {
            System.out.println("Hello " + who[i] + "!");            
        }
    }
    
    public String toString() {
        return "Hello People";
    }
}

With a configuration:

<oddjob>
    <job>
        <bean class="org.oddjob.devguide.HelloPeopleJob">
            <who>
                <list>
                    <values>
                        <value value="Rod"/>
                        <value value="Jane"/>
                        <value value="Freddy"/>
                    </values>
                </list>
            </who>
        </bean>
    </job>
</oddjob>

And fire it up as before:

Hello People in Oddjob

The job also has a nice name. We also gave our new job a toString() method, and that's what explorer uses to label the job.

Summary

So lets take a moment to reflect on what we've got:

Not bad for a dozen lines of code.


[Home] [Index] [Previous] [Next]