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

Annotations

Overview

Oddjob provides some Annotations for interaction with the Framework. Doesn't this introduce a dependency which is against the principle of an unobtrusive framework? No - because annotations can be specified in a descriptor that we will get to later.

Supported Annotations

Configuration:
@ArooaAttribute
A property that is normally configured as an element is to be configured as an attribute.
@ArooaComponent
The property is a component, this is a child job in Oddjob.
@ArooaText
The property is CDATA in the XML (such as Echo's text property).
@ArooaHidden
Hide the property from the designer.
Configuration Lifecycle:
@Initialised
Callback for when the bean has been initialised. All static properties have been set.
@Configured
Callback for when the bean has been configured. All dynamic properties have been set.
@Destroy
Callback to clear up resources.
Job Lifecycle:
@Run
Method to run a job if the job isn't Runnable or Callable
@Run
Method to run to stop a job.
@SoftReset
Callback to reset state in a job after an Exception or if it is Incomplete.
@HardReset
Callback to reset a job if it is in any Finished stated.
Other
@Start
Like run, but for a Service.
@AcceptExceptionListener
For a FallibleComponent
@AcceptCompletionHandle
For an AsyncJob
@NoDescribe
Do not show the property in the properties tab of the UI.
@Destination
Property to set as a destination in a Bean Bus.
@Inject
The standard Java annotation for dependency injection is described later

Example

Here's a Job with lots of Annotations:

package org.oddjob.devguide;

import org.oddjob.arooa.deploy.annotations.ArooaAttribute;
import org.oddjob.arooa.deploy.annotations.ArooaComponent;
import org.oddjob.arooa.life.Configured;
import org.oddjob.arooa.life.Destroy;
import org.oddjob.arooa.life.Initialised;
import org.oddjob.framework.adapt.HardReset;
import org.oddjob.framework.adapt.Run;
import org.oddjob.framework.adapt.SoftReset;
import org.oddjob.framework.adapt.Stop;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AnnotationsInJob {

    @ArooaAttribute
    public File file;

    public final List<Object> children = new ArrayList<>();

    @Run
    public void go() {
        System.out.println("Running");
        for (Object child : children) {
            if (child instanceof Runnable) {
                ((Runnable) child).run();
            }
        }
    }

    @Stop
    public void halt() {
        System.out.println("Stopping");
    }

    @SoftReset
    @HardReset
    public void reset() {
        System.out.println("Resetting");
    }

    @Initialised
    public void init() {
        System.out.println("Initialising we know we have children " + children );
    }

    @Configured
    public void configure() {
        System.out.println("Configured - we now know that file is " + file);
    }

    @Destroy
    public void destroy() {
        System.out.println("Destroying - we still have " + file + " and " + children + " but not for long.");
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    @ArooaComponent
    public void setChild(int index, Object child) {
        if (child == null) {
            this.children.remove(index);
        }
        else {
            this.children.add(index, child);
        }
    }

    @Override
    public String toString() {
        return "AnnotationsInJob";
    }
}

And its configuration:

<oddjob id="oddjob">
  <job>
    <bean class="org.oddjob.devguide.AnnotationsInJob" file="${oddjob.file}">
      <child>
        <echo>I'm a child job</echo>
      </child>
    </bean>
  </job>
</oddjob>

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