Event Notifications Triggered by Calls

Event notifications when exceptions occur before and after calling

Feature Description

The events oninvoke, onreturn, and onthrow will be triggered before a call, after a call, and when an exception occurs, respectively. You can configure which class and method to notify when these events happen.

Use Cases

Before calling the service method, we can log the start time, after the call, calculate the total time consumed, and in case of exceptions, we can trigger alerts or print error logs, or log request and response logs before and after the service call.

Reference Case dubbo-samples-notify

Usage

Shared Service Interface Between Provider and Consumer

  1. interface IDemoService {
  2. public Person get(int id);
  3. }

Service Provider Implementation

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

Service Provider Configuration

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

Service Consumer Callback Interface

  1. interface Notify {
  2. public void onreturn(Person msg, Integer id);
  3. public void onthrow(Throwable ex, Integer id);
  4. }

Service Consumer Callback Implementation

  1. class NotifyImpl implements Notify {
  2. public Map<Integer, Person> ret = new HashMap<Integer, Person>();
  3. public Map<Integer, Throwable> errors = new HashMap<Integer, Throwable>();
  4. public void onreturn(Person msg, Integer id) {
  5. System.out.println("onreturn:" + msg);
  6. ret.put(id, msg);
  7. }
  8. public void onthrow(Throwable ex, Integer id) {
  9. errors.put(id, ex);
  10. }
  11. }

Service Consumer Callback Configuration

The following combinations exist:

  • Asynchronous Callback Mode: async=true onreturn="xxx"
  • Synchronous Callback Mode: async=false onreturn="xxx"
  • Asynchronous No Callback: async=true
  • Synchronous No Callback: async=false

callback and async functionalities are orthogonally decomposed, where async=true indicates whether the result returns immediately, and async=false is default, while onreturn indicates whether a callback is needed.

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

Test Code

  1. IDemoService demoService = (IDemoService) context.getBean("demoService");
  2. NotifyImpl notify = (NotifyImpl) context.getBean("demoCallback");
  3. int requestId = 2;
  4. Person ret = demoService.get(requestId);
  5. Assert.assertEquals(null, ret);
  6. //for Test:used to indicate that callback is called normally, the specific implementation is left to the business.
  7. for (int i = 0; i < 10; i++) {
  8. if (!notify.ret.containsKey(requestId)) {
  9. Thread.sleep(200);
  10. } else {
  11. break;
  12. }
  13. }
  14. Assert.assertEquals(requestId, notify.ret.get(requestId).getId());

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)