相关资料

官方文档

项目结构

代码示例

  • [EchoTestApp]
@RestController
@SpringBootApplication
@ImportResource("classpath:/consumer.xml")
public class EchoTestApp {
@Autowired
private ClientService clientService; @GetMapping("/hi/{name}")
public String hello(@PathVariable(name = "name") String name) throws InterruptedException, ExecutionException, TimeoutException {
return clientService.echo(name); } public static void main(String[] args) {
System.getProperties().put("server.port", 7070);
SpringApplication.run(EchoTestApp.class, args);
} @Configuration
@EnableDubbo(scanBasePackages = "consumer")
@PropertySource("classpath:/dubbo-consumer.properties")
static public class ConsumerConfiguration { }
}

和之前的区别在于 @ImportResource("classpath:/consumer.xml") 引入dubbo的xml配置

至于为什么用xml呢?因为没有找到 dubbo 事件通知 api 的参考示例

  • [consumer.xml]
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="annotation-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="echoService" check="false" interface="com.xh.dubbo.learn.lesson2.api.IEchoService">
<dubbo:method name="echo" async="true" onreturn="notify.onReturn" onthrow="notify.onThrow"/>
</dubbo:reference> <bean class="com.xh.dubbo.learn.lesson2.service.ClientService" id="clientService">
<property name="echoService" ref="echoService"></property>
</bean>
</beans>
  • [dubbo-consumer.properties] 注释掉里面的所有配置,应为不能和上面的重复

  • [ClientService] 注释掉里面的重复配置

package com.xh.dubbo.learn.lesson2.service;

import com.xh.dubbo.learn.lesson2.api.IEchoService;
import org.apache.dubbo.rpc.RpcContext; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; /*@Service*/
public class ClientService {
/**
* xml和注解不能同时定义
*/
/* @Reference*/
private IEchoService echoService; public String echo(String msg) throws ExecutionException, InterruptedException, TimeoutException { String result = echoService.echo(msg);// 这里的返回值为空,请不要使用
Future<String> future = RpcContext.getContext().getFuture();
// 业务线程可以开始做其他事情
System.out.println("start do other thing...");
//Thread.sleep(100);
System.out.println("print result:" + result);
System.out.println("other thing is done"); result = future.get(3000, TimeUnit.MILLISECONDS); // 阻塞需要获取异步结果时,也可以使用 get(timeout, unit) 设置超时时间
return result == null ? "error result" : result;
} public IEchoService getEchoService() {
return echoService;
} public void setEchoService(IEchoService echoService) {
this.echoService = echoService;
}
}
  • [INotify]
public interface INotify {
void onReturn(String returnStr, String arg); void onThrow(Throwable ex, String arg);
}
  • [NotifyImpl]
@Component("notify")
public class NotifyImpl implements INotify {
public void onReturn(String returnStr, String arg) {
System.out.println("do something onReturn");
System.out.println(returnStr);
System.out.println(arg); } public void onThrow(Throwable ex, String arg) {
System.out.println("do something onThrow");
System.out.println(ex.getMessage());
System.out.println(arg); }
}

需要注意的是以上方法的参数的类型和个数需要和配置文件中的比如 notify.onReturn 一致,只是前面多了返回值或者异常

输出

控制台

start do other thing...

print result:null

other thing is done

do something onReturn

echo: 哈哈哈

哈哈哈

浏览器

dubbo学习笔记四(异步调用)的更多相关文章

  1. MongoDB 学习笔记四 C#调用MongoDB

    驱动 下载 https://github.com/mongodb/mongo-csharp-driver/downloads 项目地址: https://github.com/mongodb/mong ...

  2. go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)

    目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...

  3. java之jvm学习笔记四(安全管理器)

    java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...

  4. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  5. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  6. python3.4学习笔记(四) 3.x和2.x的区别,持续更新

    python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...

  7. 零拷贝详解 Java NIO学习笔记四(零拷贝详解)

    转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...

  8. kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置

    KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...

  9. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

随机推荐

  1. Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name) 中文显示乱码

    如果file.Name为中文则乱码.解决办法是方法1:response.setHeader("Content-Disposition", "attachment; fil ...

  2. LC 656. Coin Path 【lock, Hard】

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

  3. js window事件解析(转载)

    js-window对象的方法和属性资料 hxpd 发表于 2007-05-08 21:58:18 熟练window对象的open.close.alert.confirm.prompt.setTimeo ...

  4. WCF绑定(Binding)

    一个Binding由一个有序的binding元素栈所组成,其中的每一个元素都指定了连接到ServiceEndpoint的一个方面.在这个栈中的最底两层都是必须要有的.最底下的一层是传输binding元 ...

  5. UML学习笔记_02_UML初识(简单的流程)

    UML建模简单流程: 分析->定义用例->定义领域模型->定义交互图->定义设计类图 1.分析: 分析需求,对项目的结构有一个大致的定义 2.定义用例: 用例是需求分析的一种工 ...

  6. Java异常超详细总结

    1.1,什么是异常:   异常就是Java程序在运行过程中出现的错误. 骚话: 世界上最真情的相依就是你在try我在catch,无论你发什么脾气,我都静静接受,默默处理(这个可以不记) 1.2,异常继 ...

  7. python 连接mysql数据库:pymysql

    示例:import pymysql conn=pymysql.connect( host="127.0.0.1", #数据库IP port=3306, #数据库端口 user=&q ...

  8. java源码-ReentrantLock源码分析-2

    继续上篇ReentrantLock分析如何唤醒线程: 当调用lock.unlock()方法最终调用AQS类中的release方法,开始释放锁 tryRelease(1)方法在Sync对象中实现,首先会 ...

  9. debain安装文泉驿字体

    sudo apt-get install ttf-wqy-microhei sudo apt-get install ttf-wqy-zenhei

  10. windows客户端如果通过cmd窗口连接到远程linux服务器,可以使用telnet;

     linux系统打开telnet端口的方法 2016-03-11 16:02:25 标签:linux telnet 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. ...