消费方切面实例
<dubbo:reference id="demoService" interface="com.jgq.dubbo_Service.service.IProcessData" >
<!-- 切面监控 Callback 监控输入,与输出 必须与 对象方法一致-->
<dubbo:method name="deal" <strong>onreturn = "demoCallback.onreturn" onthrow="demoCallback.onthrow" </strong> />
</dubbo:reference>
<span style="white-space:pre"> </span><!-- 本地 bean -->
<strong><bean id ="demoCallback" class = "com.jgq.service.impl.NotifyImpl" /></strong>
切面被监控的提供方方法定义:
public <strong>String</strong> deal(<strong>String</strong> data);
针对方法 切面监控 接口定义:
package com.jgq.service;
public interface Notify {
public void onreturn(<strong>String deal, String instr</strong>);
public void onthrow(Throwable ex, <strong>String instr</strong>);
}
【 注意 : 上面监控接口 Notify 中 onreturn 的第一个参数为 被监控方法deal的返回类型, 后续参数为deal的输入参数类型保存一致 】
切面监控接口实现:
public class NotifyImpl implements Notify {
public Map<String, String> ret = new HashMap<String, String>();
public Map<String, Throwable> errors = new HashMap<String, Throwable>();
public void onreturn(String deal, String instr) {
System.out.println("切面测试: onreturn:" + deal);
ret.put(instr, deal);
}
public void onthrow(Throwable ex, String instr) {
System.out.println("切面测试: onthrow:" + ex.getMessage());
errors.put(instr, ex);
}
}
场景调用:
System.out.println( <span style="font-family: Arial, Helvetica, sans-serif;">demoService.deal("Hello[你好],Dubbo!") </span>);
场景结果数据:
切面测试: onreturn:Finished:Hello[你好],Dubbo!
Thread-0 demoService : Finished:Hello[你好],Dubbo!