職責鏈實現的apache.chain使用


    其實職責鏈在老早就使用過了,以前在HW給Vodafone做金融項目的時候,使用職責鏈完成交易步驟,那時覺得這東西真好用,可以直接通過配置決定業務流程,現在終於有機會實踐一下。

 
    
    這種設計模式本身的實現是非常容易的,可以簡單單做是一組IF條件的集合,符合條件的繼續傳遞;不符合條件的終止運行。chain代表了一條運行邏輯,就如同一條項鏈,我們的業務邏輯就如同是珍珠,並且都實現了同樣的compute接口。apache的實現,是通過將數據封裝到上下文(context)中,而且該上下文就是串起這些珍珠的金線。
 
    下面是自己寫的一段例子:
 
 鏈的組織,也可以通過配置xml文件來實現,用在spring框架中非常合適。
 
/**
 * 職責鏈的組織類,負責構造整個鏈
 */
public class RootCauseChain extends ChainBase
{
	/**
	 * 通過此方法增減生效的分析器
	 */
	public RootCauseChain()
	{
		addCommand(new DataRootCauseAnalyzer());
		//addCommand(new EnvRootCauseAnalyzer());
		//addCommand(new PifRootCauseAnalyzer());
		//addCommand(new TaskRootCauseAnalyzer());
	}
}

  具體的業務:  

/**
 * 實現了command接口,數據均通過context組織
 */
public class DataRootCauseAnalyzer implements Command
{
	private DataQueryService dqService = new DataQueryService();

	private static final String ROOT_CAUSE_FORMAT = "indicator value is abnormal: check ? for more information";

	@SuppressWarnings("unchecked")
	@Override
	public boolean execute(Context arg0) throws Exception
	{
		boolean res = false;

		Log.info(RootCauseConstant.MODULE_CODE, "0000",
				"begin to execute DataRootCauseAnalyzer.execute");

		List<DataPoint> exceptionDataPoints = (List<DataPoint>) arg0.get("expData");
		ExceptionRule exceptionRule = (ExceptionRule) arg0.get("rule");

		List<RootCause> result = new ArrayList<RootCause>();

		if (exceptionDataPoints != null && !exceptionDataPoints.isEmpty())
		{
			for (DataPoint dataPoint : exceptionDataPoints)
			{
				List<RootCause> rootCauseList = generateRootCause(dataPoint, exceptionRule);

				result.addAll(rootCauseList);
			}
		}

		// 如果分析出了根因,則結束分析流程
		if (result != null && !result.isEmpty())
		{
			arg0.put("result", result);

			res = true;
		}

		// 沒有分析出根因,交到下一個分析器進行分析
		return res;
	}

	/**
	 * 生成具體的異常信息
	 * 
	 * @param exceptionPoint
	 *            異常數據點
	 * @param rule
	 *            異常規則
	 * @return 查詢上下級關系
	 */
	private List<RootCause> generateRootCause(DataPoint exceptionPoint, ExceptionRule rule)
	{
		List<RootCause> rclist = new ArrayList<RootCause>();

    
		return rclist;
	}
}

  調用:

public class RootCauseService
{
    /**
     * 分析異常點的根因
     * 
     * @param points
     *            異常數據點
     * @param rule
     *            異常數據發現規則
     * @return 異常數據點及根因
     */
    @SuppressWarnings("unchecked")
    public List<RootCause> getRootCause(List<DataPoint> points, ExceptionRule rule)
    {
        Log.info(RootCauseConstant.MODULE_CODE, "0000", "begin to execute getRootCause, points="
                + points + ", rule=" + rule);

        List<RootCause> result = new ArrayList<RootCause>();

        try
        {
            Command command = new RootCauseChain();
            ContextBase ctx = new ContextBase();

            ctx.put("expData", points);
            ctx.put("rule", rule);

            command.execute(ctx);
            result = (List<RootCause>) ctx.get("result");
        }
        catch (Exception e)
        {
            Log.error(RootCauseConstant.MODULE_CODE, "0000",
                    "execute analysisRootCauseByCommandChain exception.", e);
        }

        Log.info(RootCauseConstant.MODULE_CODE, "0000", "execute getRootCause finished, result="
                + result);

        return result;
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM