提交 b2ff2a82 编写于 作者: 武汉红喜's avatar 武汉红喜

test

上级 8e1f9ca1
/**
* Created by shenhongxi on 2017/6/21.
*/
package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.service.EchoService;
import org.hongxi.whatsmars.dubbo.demo.api.BarService;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.Future;
public class AsyncConsumer {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-async-consumer.xml"});
context.start();
// 异步调用
async(context);
}
private static void async(ClassPathXmlApplicationContext context) throws Exception {
// 异步调用
DemoService demoService5 = (DemoService) context.getBean("demoService5");
demoService5.sayHello("aysc");
Future<String> helloFuture = RpcContext.getContext().getFuture();
BarService barService = (BarService) context.getBean("barService");
barService.findBar("m123456");
Future<Bar> barFuture = RpcContext.getContext().getFuture();
String hello5 = helloFuture.get();
Bar bar = barFuture.get();
System.out.println(hello5);
System.out.println(bar);
}
}
\ No newline at end of file
...@@ -22,9 +22,6 @@ public class DemoConsumer { ...@@ -22,9 +22,6 @@ public class DemoConsumer {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start(); context.start();
// 异步调用
// async(context);
// dubbo protocol // dubbo protocol
DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理 DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("dubbo"); // 执行远程方法 String hello = demoService.sayHello("dubbo"); // 执行远程方法
...@@ -54,20 +51,4 @@ public class DemoConsumer { ...@@ -54,20 +51,4 @@ public class DemoConsumer {
} }
private static void async(ClassPathXmlApplicationContext context) throws Exception {
// 异步调用
DemoService demoService5 = (DemoService) context.getBean("demoService5");
demoService5.sayHello("aysc");
Future<String> helloFuture = RpcContext.getContext().getFuture();
BarService barService = (BarService) context.getBean("barService");
barService.findBar("m123456");
Future<Bar> barFuture = RpcContext.getContext().getFuture();
String hello5 = helloFuture.get();
Bar bar = barFuture.get();
System.out.println(hello5);
System.out.println(bar);
}
} }
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:reference id="demoService5" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService">
<dubbo:method name="sayHello" async="true" />
</dubbo:reference>
<dubbo:reference id="barService" interface="org.hongxi.whatsmars.dubbo.demo.api.BarService">
<dubbo:method name="findBar" async="true" />
</dubbo:reference>
</beans>
\ No newline at end of file
...@@ -22,12 +22,4 @@ ...@@ -22,12 +22,4 @@
<dubbo:reference id="demoService4" group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" /> <dubbo:reference id="demoService4" group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" />
<dubbo:reference id="demoService5" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService">
<dubbo:method name="sayHello" async="true" />
</dubbo:reference>
<dubbo:reference id="barService" interface="org.hongxi.whatsmars.dubbo.demo.api.BarService">
<dubbo:method name="findBar" async="true" />
</dubbo:reference>
</beans> </beans>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册