RSS Feed for This PostCurrent Article

Java: Develop a Simple Workflow System

This is a simple workflow system I modified and adapted from Apache Commons Chain and few other sample implementations.

<workflow>
 
    <flow name="SampleFlow">
 
        <task id="ExceptionHandler" className="com.simple.workflow.ExceptionHandler"/>
 
        <task id="Step1"
              className="com.simple.workflow.Step1"/>
        <task id="Step2"
              className="com.simple.workflow.Step2"/>
        <task id="Step3"
              className="com.simple.workflow.Step3"/>
 
    </flow>
 
</workflow>

Each step implements the Task interface

package com.simple.workflow;
 
 
public class Step1 implements Task {
 
    public boolean execute(Context context) throws Exception {
        MyContext myContext = (MyContext)context;
        myContext.put("task_name", "Step 1");
        myContext.setTaskName("Step1");
        System.out.println("Step1");
        return false;  
    }
 
}

 

package com.simple.workflow;
 
 
public class Step2 implements Task {
 
    public boolean execute(Context context) throws Exception {
        System.out.println("Step 2");
        MyContext myContext = (MyContext) context;
        System.out.println("task name: " + myContext.getTaskName());
        return false;
    }
 
 
}

 

package com.simple.workflow;
 
 
public class Step3 implements Task {
 
    public boolean execute(Context context) throws Exception {
        System.out.println("Step 3");
        return false;
    }
}

ExceptionHandler implements Handler class, which in term implements Task class.

package com.simple.workflow;
 
public class ExceptionHandler implements Handler {
 
    public boolean postprocess(Context context, Exception exception) {
        MyContext myContext = (MyContext) context;
 
        if (exception != null) {
            System.out.println("task name: " + myContext.getTaskName() + " has error");
            System.out.println("exception message: " + exception.getMessage());
        }
 
        // Clean up
 
        // Put the elapsed time here
 
        return true;
    }
 
    public boolean execute(Context context) throws Exception {
        System.out.println("Exception handler execute");
        return false;
    }
}

It implements the postprocess method of Handler interface, which accept the Context and the Exception class. Exception class is not NULL if there is error.

The context is passed using the Context interface, which is actually a Map. You can create your own context by overriding ContextBase class, or use ContextBase class itself.

package com.simple.workflow;
 
import com.simple.workflow.impl.ContextBase;
 
 
public class MyContext extends ContextBase {
    private String taskName;
 
 
    public String getTaskName() {
        return taskName;
    }
 
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }
 
}

To test it,

package com.simple.workflow;
 
import com.simple.workflow.config.ConfigParser;
import com.simple.workflow.impl.*;
 
public class TestWorkflow {
    
      private static final String CONFIG_FILE = "/test-flow.xml";
        private ConfigParser parser;
        private Workflow workflow;
 
        public TestWorkflow() {
            parser = new ConfigParser();
        }
 
        public Workflow getWorkflow() throws Exception {
            if (workflow == null) {
                parser.parse(
                        this.getClass().getResource(CONFIG_FILE));
 
            }
            workflow = WorkflowFactoryBase.getInstance().getWorkflow();
            return workflow;
        }
 
        public static void main(String[] args) throws Exception {
            TestWorkflow test = new TestWorkflow();
            Workflow workflow = test.getWorkflow();
            Task task = workflow.getTask("SampleFlow");
            MyContext ctx = new MyContext();
            task.execute(ctx);
        }
 
}

The output,

Step1
Step 2
task name: Step1
Step 3

Source code can be downloaded here.


Trackback URL


Sorry, comments for this entry are closed at this time.