nucleus.mockaop.net:opensource .net aop framework

本文介绍了Nucleus.MockAOP.Net,其基于配置文件动态载入Aspects,可对不同类和方法指定Aspects,还能指定执行顺序并提供相关框架和示例。不过存在性能受影响、配置耦合等限制。使用时,受管理类从特定类派生,可在配置文件指定Aspects。

Nucleus.MockAOP.Net简介

一、功能

  • 基于配置文件,动态载入Aspects

  • 对不同的类,指定不同的Aspects (基于正则表达式或者继承关系)

  • 对类的不同方法,指定不同的Aspects (基于正则表达式)

  • 指定Aspects的相对执行顺序

  • 提供了一种ChainOfResponsbility模式的RealProxy实现,并提供了该RealProxy所需的Aspect的框架,及三个示例Aspect,分别用来计算执行时间、打印输入参数和返回值、和捕捉所有异常

二、限制

  • 未找到好方法将Aspect变为无状态,导致每new一个正常对象,都会附带若干Aspect对象,性能受影响

  • 未找到好方法最灵活的配置类、方法与Aspects的关联,现在配置信息散落在各处,加大了对象之间的耦合,加重了Aspect对象的负担,如现在每个Aspect对象都要判断当前被调用的原始方法是否匹配在配置文件中的配置,若匹配才切入,否则不予理睬

  • 尚未实现RealProxy的动态配置(即不同的代理策略)

三、使用

  • 将希望被切入Aspect的类(即受AOP管理的类)从Nucleus.MockAOP.AspectManagedObject派生,则该类及其子类都将受AOP管理;如果某个个别子类希望脱离管理,则声明时将AspectManaged属性置为false即可

Nucleus.MockAOP.AspectManagedObject定义

namespace Nucleus.MockAOP {

     [AspectManaged]

     public class AspectManagedObject : ContextBoundObject {

     }

}

希望受AOP管理的类定义

public class SomeBusinessClass : AspectManagedObject {

     public string SomeProperty {

         get{return "pone";}

     }

}

父类受AOP管理,某个子类希望脱离AOP管理

[AspectManaged(false)]

public class SomeFreeClass : BusinessClass {

}

  • IAspect接口

IAspect定义

namespace Nucleus.MockAOP {

     public interface IAspect {

         IMethodReturnMessage SyncExecuteMethod(IMethodCallMessage msg);

         IConstructionReturnMessage SyncExecuteConstructor(IConstructionCallMessage msg);

     }

}

定义新的Aspect

public class TerminatorAspect : IAspect {

     #region IAspect 成员

public IMethodReturnMessage SyncExecuteMethod(IMethodCallMessage msg) {

          return xxx;

     }

     public IConstructionReturnMessage SyncExecuteConstructor(IConstructionCallMessage msg) {

          return xxx;

     }

     #endregion

}

定义与ChainOfResponsbility模式的RealProxy实现合作的Aspect

public class ExceptionAspect : BaseChainNodeAspect {

     protected override IMethodReturnMessage xxxxxx(IMethodCallMessage msg) {

         return xxx;

     }

}

  • 在配置文件中为类和方法指定Aspects和Aspects的相对执行顺序;使用正则表达式(type_name_regex)或者继承关系(instance_of),当在同一个type_name_pattern中同时使用时,以type_name_regex为准

  <?xml version="1.0" encoding="utf-8" ?>
- < configuration >
- < configSections >
  < section name =" aop_setting " type =" Nucleus.MockAOP.AspectConfigHandler, Nucleus.MockAOP " />
  </ configSections >
- < aop_setting >
- < type_name_pattern type_name_regex =" Nucleus.MockAOP.Test.BusinessClass ">
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.DurationAspect " method_name_regex =" (-)* " order =" 10 " />
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.MethodInfoAspect " method_name_regex =" GetString " order =" 20 " />
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.ExceptionAspect " order =" 30 " />
  </ type_name_pattern >
- < type_name_pattern instance_of =" Nucleus.MockAOP.Test.IBusiness, Nucleus.MockAOP.Test ">
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.DurationAspect " method_name_regex =" (-)* " order =" 10 " />
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.MethodInfoAspect " method_name_regex =" GetString " order =" 20 " />
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.ExceptionAspect " order =" 30 " />
  </ type_name_pattern >
- < type_name_pattern type_name_regex =" (Class/*Not/*Exist){1} ">
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.DurationAspect " order =" 10 " />
  < aspect type =" Nucleus.MockAOP.AspectsChainImpl.MethodInfoAspect " order =" 20 " />
  </ type_name_pattern >
  </ aop_setting >
  </ configuration >

四、下载交流

五、参考资料

hadoop@ubuntu:~$ schematool -dbType derby -initSchema SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/opt/hive/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/hadoop/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] Metastore connection URL: jdbc:derby:;databaseName=/tmp/metastore_db;create=true Metastore Connection Driver : org.apache.derby.jdbc.EmbeddedDriver Metastore connection User: APP Starting metastore schema initialization to 3.1.0 Initialization script hive-schema-3.1.0.derby.sql Error: FUNCTION 'NUCLEUS_ASCII' already exists. (state=X0Y68,code=30000) org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !! Underlying cause: java.io.IOException : Schema script failed, errorcode 2 Use --verbose for detailed stacktrace. *** schemaTool failed *** hadoop@ubuntu:~$ hive SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/opt/hive/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/hadoop/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] Hive Session ID = b232469a-bcec-406d-ac88-7511007cbf19 Logging initialized using configuration in jar:file:/opt/hive/apache-hive-3.1.3-bin/lib/hive-common-3.1.3.jar!/hive-log4j2.properties Async: true Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Call From ubuntu/127.0.1.1 to 192.168.229.132:9000 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:651) at org.apache.hadoop.hive.ql.session.SessionState.beginStart(SessionState.java:591) at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:747) at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:683) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.hadoop.util.RunJar.run(RunJar.java:328) at org.apache.hadoop.util.RunJar.main(RunJar.java:241) Caused by: java.net.ConnectException: Call From ubuntu/127.0.1.1 to 192.168.229.132:9000 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.apache.hadoop.net.NetUtils.wrapWithMessage(NetUtils.java:930) at org.apache.hadoop.net.NetUtils.wrapException(NetUtils.java:845) at org.apache.hadoop.ipc.Client.getRpcResponse(Client.java:1588) at org.apache.hadoop.ipc.Client.call(Client.java:1530) at org.apache.hadoop.ipc.Client.call(Client.java:1427) at org.apache.hadoop.ipc.ProtobufRpcEngine2$Invoker.invoke(ProtobufRpcEngine2.java:258) at org.apache.hadoop.ipc.ProtobufRpcEngine2$Invoker.invoke(ProtobufRpcEngine2.java:139) at com.sun.proxy.$Proxy28.getFileInfo(Unknown Source) at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB.getFileInfo(ClientNamenodeProtocolTranslatorPB.java:966) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocationHandler.java:433) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invokeMethod(RetryInvocationHandler.java:166) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invoke(RetryInvocationHandler.java:158) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invokeOnce(RetryInvocationHandler.java:96) at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:362) at com.sun.proxy.$Proxy29.getFileInfo(Unknown Source) at org.apache.hadoop.hdfs.DFSClient.getFileInfo(DFSClient.java:1739) at org.apache.hadoop.hdfs.DistributedFileSystem$29.doCall(DistributedFileSystem.java:1753) at org.apache.hadoop.hdfs.DistributedFileSystem$29.doCall(DistributedFileSystem.java:1750) at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81) at org.apache.hadoop.hdfs.DistributedFileSystem.getFileStatus(DistributedFileSystem.java:1765) at org.apache.hadoop.fs.FileSystem.exists(FileSystem.java:1829) at org.apache.hadoop.hive.ql.exec.Utilities.ensurePathIsWritable(Utilities.java:4486) at org.apache.hadoop.hive.ql.session.SessionState.createRootHDFSDir(SessionState.java:760) at org.apache.hadoop.hive.ql.session.SessionState.createSessionDirs(SessionState.java:701) at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:627) ... 9 more Caused by: java.net.ConnectException: Connection refused at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:715) at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:205) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:600) at org.apache.hadoop.ipc.Client$Connection.setupConnection(Client.java:668) at org.apache.hadoop.ipc.Client$Connection.setupIOstreams(Client.java:790) at org.apache.hadoop.ipc.Client$Connection.access$3800(Client.java:363) at org.apache.hadoop.ipc.Client.getConnection(Client.java:1649) at org.apache.hadoop.ipc.Client.call(Client.java:1474) ... 34 more
12-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值