当前位置首页 > Apache知识

职责链实现的apache.chain使用

阅读次数:197 次  来源:admin  发布时间:

其实职责链在老早就使用过了,以前在HW给Vodafone做金融项目的时候,使用职责链完成交易步骤,那时觉得这东西真好用,可以直接通过配置决定业务流程,现在终于有机会实践一下。

项目地址:http://commons.apache.org/proper/commons-chain/

这种设计模式本身的实现是非常容易的,可以简单单做是一组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;
    }
}
上一篇:Debian系统移植--持续更新
下一篇:要成为linux网站运维工程师必须要掌握的技能