Event Notification

Event notification in dubbo

Before calling, after calling, when an exception occurs,will trigger oninvoke, onreturn, onthrow events.You can configure which method to notify when an event occurs.

Service Interface

interface IDemoService {
    public Person get(int id);
}

Service provider implement the service.

class NormalDemoService implements IDemoService {
    public Person get(int id) {
        return new Person(id, "charles`son", 4);
    }
}

Service provider configure the service which it provided.

<dubbo:application name="rpc-callback-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<bean id="demoService" class="org.apache.dubbo.callback.implicit.NormalDemoService" />
<dubbo:service interface="org.apache.dubbo.callback.implicit.IDemoService" ref="demoService" version="1.0.0" group="cn"/>

Declare the Callback interface at service consumer-side.

interface Notify {
    public void onreturn(Person msg, Integer id);
    public void onthrow(Throwable ex, Integer id);
}

Implement the Callback at service consumer-side.

class NotifyImpl implements Notify {
    public Map<Integer, Person>    ret    = new HashMap<Integer, Person>();
    public Map<Integer, Throwable> errors = new HashMap<Integer, Throwable>();

    public void onreturn(Person msg, Integer id) {
        System.out.println("onreturn:" + msg);
        ret.put(id, msg);
    }

    public void onthrow(Throwable ex, Integer id) {
        errors.put(id, ex);
    }
}

Configure the Callback at service consumer-side.

<bean id ="demoCallback" class = "org.apache.dubbo.callback.implicit.NotifyImpl" />
<dubbo:reference id="demoService" interface="org.apache.dubbo.callback.implicit.IDemoService" version="1.0.0" group="cn" >
      <dubbo:method name="get" async="true" onreturn = "demoCallback.onreturn" onthrow="demoCallback.onthrow" />
</dubbo:reference>

callback and async functions are orthogonally decomposed. async = true means that the result is returned immediately. onreturn means that a callback is required.

There are several situations with the tow attributes[^2].

  • Asynchronous callback mode:async=true onreturn="xxx"
  • Synchronous callback mode:async=false onreturn="xxx"
  • Asynchronous no callback:async=true
  • Synchronous no callback:async=true

Testing code

IDemoService demoService = (IDemoService) context.getBean("demoService");
NotifyImpl notify = (NotifyImpl) context.getBean("demoCallback");
int requestId = 2;
Person ret = demoService.get(requestId);
Assert.assertEquals(null, ret);
//for Test:Just used to illustrate the normal callback callback, the specific business decisions.
for (int i = 0; i < 10; i++) {
    if (!notify.ret.containsKey(requestId)) {
        Thread.sleep(200);
    } else {
        break;
    }
}
Assert.assertEquals(requestId, notify.ret.get(requestId).getId());

Last modified January 28, 2021: fix typo in events-notify.md (#720) (6d945d77a2)