简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM