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

异步调用

上级 21d8378c
/**
* Created by shenhongxi on 2017/6/21.
*/
package org.hongxi.whatsmars.dubbo.demo.api;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
public interface BarService {
Bar findBar(String barId);
}
\ No newline at end of file
package org.hongxi.whatsmars.dubbo.demo.api.vo;
import java.io.Serializable;
public class Bar implements Serializable {
private static final long serialVersionUID = -5809782578272943999L;
private String barId;
private String name;
private String address;
public Bar() {}
public Bar(String barId, String name, String address) {
this.barId = barId;
this.name = name;
this.address = address;
}
public String getBarId() {
return barId;
}
public void setBarId(String barId) {
this.barId = barId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "barId:" + barId + ",name:" + name + ",address" + address;
}
}
...@@ -3,17 +3,25 @@ ...@@ -3,17 +3,25 @@
*/ */
package org.hongxi.whatsmars.dubbo.demo.consumer; package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.service.EchoService; 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.DemoService;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.Future;
public class DemoConsumer { public class DemoConsumer {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
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"); // 执行远程方法
...@@ -39,6 +47,24 @@ public class DemoConsumer { ...@@ -39,6 +47,24 @@ public class DemoConsumer {
Object status = echoService.$echo("OK"); Object status = echoService.$echo("OK");
System.out.println("回声测试:" + status.equals("OK")); System.out.println("回声测试:" + status.equals("OK"));
System.out.println("#######################ALL SUCCESSFUL##########################");
} }
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,4 +22,12 @@ ...@@ -22,4 +22,12 @@
<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
/**
* Created by shenhongxi on 2017/6/21.
*/
package org.hongxi.whatsmars.dubbo.demo.provider;
import org.hongxi.whatsmars.dubbo.demo.api.BarService;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
import org.springframework.stereotype.Service;
@Service("barService")
public class BarServiceImpl implements BarService {
@Override
public Bar findBar(String barId) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
return new Bar(barId, "芝根芝底", "酒仙桥302号");
}
}
\ No newline at end of file
...@@ -29,4 +29,6 @@ ...@@ -29,4 +29,6 @@
<!-- 类似group的还有version="2.0.0" --> <!-- 类似group的还有version="2.0.0" -->
<dubbo:service group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" ref="demoService2" /> <dubbo:service group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" ref="demoService2" />
<dubbo:service interface="org.hongxi.whatsmars.dubbo.demo.api.BarService" ref="barService" />
</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.
先完成此消息的编辑!
想要评论请 注册