职责链实现的apache.chain使用
其实职责链在老早就使用过了,以前在HW给Vodafone做金融项目的时候,使用职责链完成交易步骤,那时觉得这东西真好用,可以直接通过配置决定业务流程,现在终于有机会实践一下。
/**
* 职责链的组织类,负责构造整个链
*/
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;
}
}
职责链实现的apache.chain使用的更多相关文章
- OOP设计模式[JAVA]——03职责链模式
职责链模式 Responsibility of Chain 在职责链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求 ...
- 18职责链模式CoR
一.什么是职责链模式 Chain of Responsibility(CoR)模式也叫职 责链模式或者职责连锁模式,是行为模式之一, 该模式构造一系列分别担当不同的职责的类的对 象来共同完成一个任务, ...
- js职责链模式
职责链模式(Chain of Responsiblity),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为 ...
- C#设计模式系列:职责链模式(Chain of Responsibility)
1.职责链模式简介 1.1>.定义 职责链模式是一种行为模式,为解除请求的发送者和接收者之间的耦合,而使多个对象都有机会处理这个请求.将这些对象连接成一条链,并沿着这条链传递该请求,直到有一个对 ...
- 职责链(Chain of Responsibility)模式在航空货运中的运用实例
设计模式这东西,基本上属于“看懂一瞬间,用会好几年”.只有实际开发中,当某一模式很好的满足了业务需求时,才会有真切的感觉.借用一句<闪电侠>中,绿箭侠教导闪电侠的台词:“不是你碰巧遇到了它 ...
- 深入浅出设计模式——职责链模式(Chain of Responsibility Pattern)
模式动机 职责链可以是一条直线.一个环或者一个树形结构,最常见的职责链是直线型,即沿着一条单向的链来传递请求.链上的每一个对象都是请求处理者,职责链模式可以将请求的处理者组织成一条链,并使请求沿着链传 ...
- atitit.设计模式(1)--—职责链模式(chain of responsibility)最佳实践O7 日期转换
atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 可以选择的模式: 表格模式,责任链模式 1 3 ...
- 设计模式:职责链模式(Chain Of Responsibility)
定 义:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 结构图: 处理请求类: //抽象处理类 abs ...
- 设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型)
设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型) 1.概述 你去政府部门求人办事过吗?有时候你会遇到过官员踢球推责,你的问题在我这里能解决就解决,不能解决就 ...
随机推荐
- phpstorm 自定义函数配置
phpstorm 自定义函数配置 打开设置->活动模板->
- js 根据当前星期做跳转(代码段)
var week = [6,0,1,2,3,4,5]; $('.HotShop_head .HotShop_tab:eq('+week[new Date().getDay()]+')').click( ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- The method of type must override a superclass method
导入android项目时,报The method of type must override asuperclass method 一堆错误, 解决方法: 将编译的jdk与使用的jdk版本一致即可.
- php生成xml的四种方法(转)
<?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...
- 判断webpart类型 How can I tell what type a web part is?
using(new SPSite("http://mysite/myweb").OpenWeb()){ //give relative path of the webpartpag ...
- 在百万数据中找出重复的数据sql
select * from (select count(name) as isone, name from tbl_org_departments group by name) t where t.i ...
- hdu 4706 Children's Day(模拟)
http://acm.hdu.edu.cn/showproblem.php?pid=4706 [题目大意]: 用a-z排出N的形状,输出大小为3-10的N,如果超过z之后,重新从a开始 下面是大小为3 ...
- 第一章 Spring整体框架和环境搭建
1.Spring 的整体架构 Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其他模块组合使用,灵活且方便的部署可以使开发的程序更加简洁灵活 ...
- js验证码倒计时
var wait=59; function time(){ if(wait >= 0){ $("#buttons").val("" + wait + &q ...