首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Jakarta Commons Chain 使用指南:責任鏈模式實現業務流程編排

     編輯:Java 相關知識

適用場景

需按固定順序執行多個命令、命令間共享上下文數據、支援動態配置流程步驟時,Commons Chain 提供標準化實現。

核心概念

  • Command:單個處理單元,實現 execute(Context) 返回 boolean (true 終止鏈)
  • Context:鏈間共享數據容器,Map 風格 API
  • Chain:命令有序集合,本身也是 Command,支援巢狀
  • Catalog:命名 Chain 註冊表,支援 XML 配置

程式化定義流程 (購車業務範例)

1. 定義五個 Command

public class GetCustomerInfo implements Command {
    public boolean execute(Context ctx) {
        System.out.println("Get customer info");
        ctx.put("customerName", "George Burdell");
        return false; // 繼續
    }
}

// TestDriveVehicle, NegotiateSale, ArrangeFinancing 結構類似,僅輸出日誌

public class CloseSale implements Command {
    public boolean execute(Context ctx) {
        System.out.println("Congratulations " + ctx.get("customerName") + ", you bought a new car!");
        return false;
    }
}

2. 組裝 Chain

public class SellVehicleChain extends ChainBase {
    public SellVehicleChain() {
        addCommand(new GetCustomerInfo());
        addCommand(new TestDriveVehicle());
        addCommand(new NegotiateSale());
        addCommand(new ArrangeFinancing());
        addCommand(new CloseSale());
    }

    public static void main(String[] args) throws Exception {
        Command chain = new SellVehicleChain();
        Context ctx = new ContextBase();
        chain.execute(ctx);
    }
}

XML 配置方式 (解耦流程定義)

1. chain-config.xml

<catalog>
  <chain name="sell-vehicle">
    <command id="GetCustomerInfo" className="demo.chain.GetCustomerInfo"/>
    <command id="TestDriveVehicle" className="demo.chain.TestDriveVehicle"/>
    <command id="NegotiateSale" className="demo.chain.NegotiateSale"/>
    <command id="ArrangeFinancing" className="demo.chain.ArrangeFinancing"/>
    <command id="CloseSale" className="demo.chain.CloseSale"/>
  </chain>
</catalog>

2. 自定義 Context (類型安全)

public class SellVehicleContext extends ContextBase {
    private String customerName;
    public String getCustomerName() { return customerName; }
    public void setCustomerName(String name) { this.customerName = name; }
}

3. 載入 Catalog 執行

public class CatalogLoader {
    private static final String CONFIG_FILE = "/demo/chain-config.xml";
    private ConfigParser parser = new ConfigParser();
    private Catalog catalog;

    public Catalog getCatalog() throws Exception {
        if (catalog == null) {
            parser.parse(getClass().getResource(CONFIG_FILE));
            catalog = CatalogFactoryBase.getInstance().getCatalog();
        }
        return catalog;
    }

    public static void main(String[] args) throws Exception {
        CatalogLoader loader = new CatalogLoader();
        Command chain = loader.getCatalog().getCommand("sell-vehicle");
        Context ctx = new SellVehicleContext();
        chain.execute(ctx);
    }
}

關鍵特性

  • 命令返回 true 可提前終止鏈
  • Context 傳遞數據,SellVehicleContext 提供類型安全存取
  • XML 配置支援熱部署、非技術人員維護流程
  • Chain 可巢狀組合,構建複雜工作流
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved