W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在調(diào)用之前、調(diào)用之后、出現(xiàn)異常時(shí)的事件通知
在調(diào)用之前、調(diào)用之后、出現(xiàn)異常時(shí),會(huì)觸發(fā) oninvoke、onreturn、onthrow 三個(gè)事件,可以配置當(dāng)事件發(fā)生時(shí),通知哪個(gè)類的哪個(gè)方法。
提示
支持版本:?2.0.7
?之后
interface IDemoService { public Person get(int id); }
class NormalDemoService implements IDemoService { public Person get(int id) { return new Person(id, "charles`son", 4); } }
<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"/>
interface Notify { public void onreturn(Person msg, Integer id); public void onthrow(Throwable ex, Integer id); }
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); } }
<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 與 async 功能正交分解,async=true 表示結(jié)果是否馬上返回,onreturn 表示是否需要回調(diào)。
兩者疊加存在以下幾種組合情況:
提示
?async=false
? 默認(rèn)
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:只是用來說明callback正常被調(diào)用,業(yè)務(wù)具體實(shí)現(xiàn)自行決定. 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());
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: