RSS Feed for This PostCurrent Article

Design Pattern: Design a Simple Workflow using Chain of Responsibility Pattern

Download Source Code

I was trying Apache Commons Chain when I was trying to design a simple workflow system for my back-end application using the Chain of Responsibility design pattern.

Chain of Responsibility is a popular technique for organizing the execution of complex processing flows. It is not difficult if you want to write it yourself but Commons Chain has already implemented this pattern for you.

Commons Chain models a computation as a series of "commands" that can be combined into a "chain". The API for a command consists of a single method (execute()), which is passed a "context" parameter containing the dynamic state of the computation, and whose return value is a boolean that determines whether or not processing for the current chain has been completed (true), or whether processing should be delegated to the next command in the chain (false).

E.g., first I create my custom context to be passed around my flows.

import org.apache.commons.chain.impl.ContextBase;

public class MyContext extends ContextBase {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Then I created my processes, each one as a command.

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

public class Command1 implements Command {

    public boolean execute(Context context) throws Exception {
        System.out.println("First command");
        MyContext myContext = (MyContext)context;
        myContext.setName("twit88");
        return false;
    }
}



import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

public class Command2 implements Command {

    public boolean execute(Context context) throws Exception {
        System.out.println("Second command");
        MyContext myContext = (MyContext)context;
        myContext.setEmail("[email protected]");
        return false;
    }
}



import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

public class Command3 implements Command {

    public boolean execute(Context context) throws Exception {
        System.out.println("Third command");
        MyContext myContext = (MyContext)context;
        System.out.println("name: " + myContext.getName());
        System.out.println("email: " + myContext.getEmail());
        return false;
    }
}

I defined the sequence of processing in a configuration file.

<catalog>

  <!-- Single command "chains" 
         from CatalogBaseTestCase -->
  <command   name="MyFirstCommand"
        className="Command1"/>
  <command   name="MySecondCommand"
        className="Command2"/>
  <command   name="MySecondCommand"
        className="Command3"/>


  <!-- Chains with nested commands -->
  <chain     name="MyFlow">
    <command   id="1"
        className="Command1"/>
    <command   id="2"
        className="Command2"/>
    <command   id="3"
        className="Command3"/>
  </chain>


</catalog>

Then I can test it.

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.config.ConfigParser;
import org.apache.commons.chain.impl.CatalogFactoryBase;

public class TestChain {

    private static final String CONFIG_FILE = "/chain.xml";
    private ConfigParser parser;
    private Catalog catalog;

    public TestChain() {
        parser = new ConfigParser();
    }

    public Catalog getCatalog() throws Exception {
        if (catalog == null) {
            parser.parse(
              this.getClass().getResource(CONFIG_FILE));

        }
        catalog = CatalogFactoryBase.getInstance().getCatalog();
        return catalog;
    }

    public static void main(String[] args) throws Exception {
        TestChain chain = new TestChain();
        Catalog catalog = chain.getCatalog();
        Command command = catalog.getCommand("MyFlow");
        Context ctx = new MyContext();
        command.execute(ctx);
    }

}

The output

First command
Second command
Third command
name: twit88
email: [email protected]

Another interesting article for reading will be using Spring to create the workflow.


Trackback URL


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