职责链实现的apache.chain使用
2015-06-19 17:34
573 查看
其实职责链在老早就使用过了,以前在HW给Vodafone做金融项目的时候,使用职责链完成交易步骤,那时觉得这东西真好用,可以直接通过配置决定业务流程,现在终于有机会实践一下。
项目地址:http://commons.apache.org/proper/commons-chain/
这种设计模式本身的实现是非常容易的,可以简单单做是一组IF条件的集合,符合条件的继续传递;不符合条件的终止运行。chain代表了一条运行逻辑,就如同一条项链,我们的业务逻辑就如同是珍珠,并且都实现了同样的compute接口。apache的实现,是通过将数据封装到上下文(context)中,而且该上下文就是串起这些珍珠的金线。
下面是自己写的一段例子:
链的组织,也可以通过配置xml文件来实现,用在spring框架中非常合适。
/**
*职责链的组织类,负责构造整个链
*/
publicclassRootCauseChainextendsChainBase
{
/**
*通过此方法增减生效的分析器
*/
publicRootCauseChain()
{
addCommand(newDataRootCauseAnalyzer());
//addCommand(newEnvRootCauseAnalyzer());
//addCommand(newPifRootCauseAnalyzer());
//addCommand(newTaskRootCauseAnalyzer());
}
}
具体的业务:
/**
*实现了command接口,数据均通过context组织
*/
publicclassDataRootCauseAnalyzerimplementsCommand
{
privateDataQueryServicedqService=newDataQueryService();
privatestaticfinalStringROOT_CAUSE_FORMAT="indicatorvalueisabnormal:check?formoreinformation";
@SuppressWarnings("unchecked")
@Override
publicbooleanexecute(Contextarg0)throwsException
{
booleanres=false;
Log.info(RootCauseConstant.MODULE_CODE,"0000",
"begintoexecuteDataRootCauseAnalyzer.execute");
List<DataPoint>exceptionDataPoints=(List<DataPoint>)arg0.get("expData");
ExceptionRuleexceptionRule=(ExceptionRule)arg0.get("rule");
List<RootCause>result=newArrayList<RootCause>();
if(exceptionDataPoints!=null&&!exceptionDataPoints.isEmpty())
{
for(DataPointdataPoint:exceptionDataPoints)
{
List<RootCause>rootCauseList=generateRootCause(dataPoint,exceptionRule);
result.addAll(rootCauseList);
}
}
//如果分析出了根因,则结束分析流程
if(result!=null&&!result.isEmpty())
{
arg0.put("result",result);
res=true;
}
//没有分析出根因,交到下一个分析器进行分析
returnres;
}
/**
*生成具体的异常信息
*
*@paramexceptionPoint
*异常数据点
*@paramrule
*异常规则
*@return查询上下级关系
*/
privateList<RootCause>generateRootCause(DataPointexceptionPoint,ExceptionRulerule)
{
List<RootCause>rclist=newArrayList<RootCause>();
returnrclist;
}
}
调用:
项目地址:
这种设计模式本身的实现是非常容易的,可以简单单做是一组IF条件的集合,符合条件的继续传递;不符合条件的终止运行。chain代表了一条运行逻辑,就如同一条项链,我们的业务逻辑就如同是珍珠,并且都实现了同样的compute接口。apache的实现,是通过将数据封装到上下文(context)中,而且该上下文就是串起这些珍珠的金线。
下面是自己写的一段例子:
链的组织,也可以通过配置xml文件来实现,用在spring框架中非常合适。
*职责链的组织类,负责构造整个链
*/
publicclassRootCauseChainextendsChainBase
{
/**
*通过此方法增减生效的分析器
*/
publicRootCauseChain()
{
addCommand(newDataRootCauseAnalyzer());
//addCommand(newEnvRootCauseAnalyzer());
//addCommand(newPifRootCauseAnalyzer());
//addCommand(newTaskRootCauseAnalyzer());
}
}
具体的业务:
*实现了command接口,数据均通过context组织
*/
publicclassDataRootCauseAnalyzerimplementsCommand
{
privateDataQueryServicedqService=newDataQueryService();
privatestaticfinalStringROOT_CAUSE_FORMAT="indicatorvalueisabnormal:check?formoreinformation";
@SuppressWarnings("unchecked")
@Override
publicbooleanexecute(Contextarg0)throwsException
{
booleanres=false;
Log.info(RootCauseConstant.MODULE_CODE,"0000",
"begintoexecuteDataRootCauseAnalyzer.execute");
List<DataPoint>exceptionDataPoints=(List<DataPoint>)arg0.get("expData");
ExceptionRuleexceptionRule=(ExceptionRule)arg0.get("rule");
List<RootCause>result=newArrayList<RootCause>();
if(exceptionDataPoints!=null&&!exceptionDataPoints.isEmpty())
{
for(DataPointdataPoint:exceptionDataPoints)
{
List<RootCause>rootCauseList=generateRootCause(dataPoint,exceptionRule);
result.addAll(rootCauseList);
}
}
//如果分析出了根因,则结束分析流程
if(result!=null&&!result.isEmpty())
{
arg0.put("result",result);
res=true;
}
//没有分析出根因,交到下一个分析器进行分析
returnres;
}
/**
*生成具体的异常信息
*
*@paramexceptionPoint
*异常数据点
*@paramrule
*异常规则
*@return查询上下级关系
*/
privateList<RootCause>generateRootCause(DataPointexceptionPoint,ExceptionRulerule)
{
List<RootCause>rclist=newArrayList<RootCause>();
returnrclist;
}
}
调用:
publicclassRootCauseService
{
/**
*分析异常点的根因
*
*@parampoints
*异常数据点
*@paramrule
*异常数据发现规则
*@return异常数据点及根因
*/
@SuppressWarnings("unchecked")
publicList<RootCause>getRootCause(List<DataPoint>points,ExceptionRulerule)
{
Log.info(RootCauseConstant.MODULE_CODE,"0000","begintoexecutegetRootCause,points="
+points+",rule="+rule);
List<RootCause>result=newArrayList<RootCause>();
try
{
Commandcommand=newRootCauseChain();
ContextBasectx=newContextBase();
ctx.put("expData",points);
ctx.put("rule",rule);
command.execute(ctx);
result=(List<RootCause>)ctx.get("result");
}
catch(Exceptione)
{
Log.error(RootCauseConstant.MODULE_CODE,"0000",
"executeanalysisRootCauseByCommandChainexception.",e);
}
Log.info(RootCauseConstant.MODULE_CODE,"0000","executegetRootCausefinished,result="
+result);
returnresult;
}
}
相关文章推荐
- windows下安装apache、php、sql server
- org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException:
- apache 相关的一些东西
- linux svn+apache 配置客户自助修改密码
- 如果apache安装成为linux的服务的话
- (7.2.7)Apache Lucene初探
- Apache Hama安装部署
- FTP -- 使用Apache FtpServer搭建FTP服务器
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'cla
- 令Apache中沉睡的无用进程自动退出的方法
- Apache 默认的根文档目录
- org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist:
- Fedora下apache php文件无法写入
- Apache Shiro 学习记录4
- debian-apache2配置多个站点
- Apache部署PHP常用配置
- apache kafka系列之kafka.common.ConsumerRebalanceFailedException异常解决办法
- Ubuntu Server 14.04 安装+安全Web服务器(linux+apache+mysql+php)搭建流程(更新)
- apachebench ab.exe压力测试
- Linux卸载自带的apache