Xlaech
Xlaech

Reputation: 466

Apache Commons Chain Basic Example

i need to get into Apaches Common Chain for a Project. So i tried to get a basic example running after: http://www.onjava.com/pub/a/onjava/2005/03/02/commonchains.html

Commons Chain is installed via Maven.

I wrote the following Chain Base:

public class PFChain extends ChainBase {
    public PFChain() {
        super();
        addCommand(new CalcE());
        addCommand(new CalcDOEB());
        addCommand(new CalcG());
    }

    public static void executePFChain() {
        Command process = new PFChain();
        Context context = new ContextBase();
        try {
            process.execute(context);
        } catch (Exception e) {
            System.out.println("errortext");
            e.printStackTrace();
        }
    }
}

My three command Classes look like this:

public class CalcDOEB implements Command {
    @Override
    public boolean execute(Context context) throws Exception {
        System.out.println("Calculating DOEB...");
        return true;
    }
}

public class CalcE implements Command {
    @Override
    public boolean execute(Context context) throws Exception {
        System.out.println("Calculating E");
        return true;
    }
}

public class CalcG implements Command {
    @Override
    public boolean execute(Context context) throws Exception {
        System.out.println("Calculation G...");
        return true;
    }
}

Now what is strange, is that he only executes the First Command in the Chain. All of them work, but just if they at the head of the commands list.

When i turn on the debugger i see, that they all land in the list.

Where does this error come from and how do i fix it?

Greetings,

Nicolas

Upvotes: 2

Views: 4797

Answers (1)

Robbenu
Robbenu

Reputation: 413

Try changing all the return statements to false instead of true. When you return true, you end the chain. Because you don't want the chain to end, you need to return false instead.

To read more about this, check out the Javadoc: https://commons.apache.org/proper/commons-chain/apidocs/org/apache/commons/chain/Command.html#CONTINUE_PROCESSING

Upvotes: 6

Related Questions