zoukankan      html  css  js  c++  java
  • Dubbo探索(五)

    事件通知

    在调用之前,调用之后,出现异常时,会触发oninvoke, onreturn, onthrow三个事件,可以配置当事件发生时,通知哪个类的哪个方法。

    消费方实现Notify接口,如下:

     1 /**
     2  * Version: 3.0
     3  * Author: pattywgm
     4  * Time: 17/6/29 下午11:01
     5  * Desc: 事件通知接口
     6  */
     7 public interface Notify {
     8     /**
     9      *
    10      * @param res 返回值
    11      * @param args 入参
    12      */
    13     public void onreturn(Object res, Object... args);
    14 
    15     /**
    16      *
    17      * @param ex 异常对象
    18      * @param args 入参
    19      */
    20     public void onthrow(Throwable ex, Object... args);
    21 
    22     /**
    23      *
    24      * @param args 入参
    25      */
    26     public void oninvoke(Object... args);
    27 }
    28 
    29 
    30 @Service("notifyImpl")
    31 public class NotifyImpl implements Notify {
    32     @Override
    33     public void onreturn(Object res, Object... args) {
    34         System.out.println("onreturn:" + res.toString());
    35 
    36     }
    37 
    38     @Override
    39     public void onthrow(Throwable ex, Object... args) {
    40 
    41         System.out.println("onthrow: " + ex.getMessage());
    42     }
    43 
    44     @Override
    45     public void oninvoke(Object... args) {
    46         System.out.println("oninvoke: " + args[0]);
    47     }

    配置UserService的findUserById()方法,添加事件通知,如下:

    1 <dubbo:reference id="userService" group="db" interface="com.patty.dubbo.api.service.UserService"
    2                      timeout="10000" retries="3" mock="true" check="false">
    3         <dubbo:method name="findAllUsers" merger="myMerger" cache="lru">
    4         </dubbo:method>
    5         <dubbo:method name="findUserById" async="false" onreturn="notifyImpl.onreturn" onthrow="notifyImpl.onthrow">
    6         </dubbo:method>
    7 </dubbo:reference>

    onreturn指定调用notifyImpl的onreturn方法, onthrow指定调用notifyImpl的onthrow方法。运行中打印结果信息为:

    onreturn: UserVo info: name-> 李丽, age -> 23, phoneNo -> 17709801256
    onthrow: null pointer happend (在UserService的实现中,模拟抛出一个空指针异常)

    代码参考:https://github.com/pattywgm/dubbo-demo.git

  • 相关阅读:
    Python的17种骚操作
    Python使用pip下载慢的原因
    Mysql数据库的安装
    Python中遇到的难解的提示:
    Linux使用SecureCRT远程终端工具的使用
    Linux下IP命令使用详解
    (未解决)jmeter报错之“请在微信客户端打开链接”
    Python学习笔记系列——九九乘法表&猜大小
    《Mysql必知必会》笔记
    (未解决)记录一次登录&jmeter,留下的一地鸡毛
  • 原文地址:https://www.cnblogs.com/java-wgm/p/7096947.html
Copyright © 2011-2022 走看看