Skip to content
Maurice Zeijen edited this page Mar 17, 2016 · 2 revisions

If none of the available tasks fits into your needs then you can define your own custom tasks and let them be created by SelfDiagnose or you can inject it into SelfDiagnose.

Creating custom DiagnosticTask

You can create a custom task by writing a class that extends 'com.philemonworks.selfdiagnose.DiagnosticTask':

import com.philemonworks.selfdiagnose.DiagnosticTask;

public MyTask extends DiagnosticTask {

    private String myAttribute;

    public String getDescription() {
        return "purpose of this task";
    }

    public void initializeFromAttributes(Attributes attributes) {
        super.initializeFromAttributes(attributes);

        // Map your custom attribute values to fields here
        this.myAttribute = attributes.getValue("myAttribute");
    }

    public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
        // perform your check
    }
}

Configuring task

SelfDiagnose managed task creation

You can then configure a custom task in the selfdiagnose.xml file by using the '<task>' element and providing the class name of the task. SelfDiagnose will create the instantiate the task object for you. This means the task needs to have a public constructor without arguments.

<task
    class="mypackage.MyTask"
    comment="my task comment"
    myAttribute="some value"
/>

Manual managed task creation

If you want to create the task manually and inject it into SelfDiagnose then you can do that using the SelfDiagnose object:

MyTask myTask = new MyTask();
SelfDiagnose.register(myTask);

The 'initializeFromAttributes' will not be executed in this case so you must make sure that the initialisation is done before you register the task with SelfDiagnose.

Spring managed task creation

SelfDiagnose can inject custom tasks that are created within a Spring Context.

You can reference to such a task by using the 'ref' attribute with the bean id as it’s value:

<task comment="My Task" ref="myTask"/>

In Spring you can create this Task in any way you like, for instance with 'old school' Spring xml:

<bean id="myTask" class="mypackage.MyTask" />

The 'initializeFromAttributes' will not be executed in this case so you must make sure that the initialisation is done before you register the task with SelfDiagnose.