圖片來源:點(diǎn)擊打開鏈接
整個(gè)異步過程圖片描述的很清楚,下面來看看代碼:
一、服務(wù)提供者
1、服務(wù)提供者接口
[java]view plaincopyprint?
packagecom.test.dubboser;
publicinterfaceServiceDemo2?{
publicPerson?getPerson(String?str,intage);
}
package com.test.dubboser;
public interface ServiceDemo2 {
public Person getPerson(String str,int age);
}
2、Person 類
[java]view plaincopyprint?
packagecom.test.dubboser;
importjava.io.Serializable;
publicclassPersonimplementsSerializable?{
/**
*
*/
privatestaticfinallongserialVersionUID?=?8661104133888956335L;
privateintage;
privateString?name;
publicPerson(){}
publicPerson(intage?,String?name){
this.age=?age;
this.name=name;
}
publicintgetAge()?{
returnage;
}
publicvoidsetAge(intage)?{
this.age?=?age;
}
publicString?getName()?{
returnname;
}
publicvoidsetName(String?name)?{
this.name?=?name;
}
@Override
publicString??toString(){
StringBuffer??buffer=newStringBuffer();
buffer.append("name:"+name+"\t");
buffer.append("age:"+age);
returnbuffer.toString();
}
}
package com.test.dubboser;
import java.io.Serializable;
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8661104133888956335L;
private int age;
private String name;
public Person(){}
public Person(int age ,String name){
this.age= age;
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String? toString(){
StringBuffer? buffer= new StringBuffer();
buffer.append("name:"+name+"\t");
buffer.append("age:"+age);
return buffer.toString();
}
}
3、服務(wù)提供者接口實(shí)現(xiàn)類
[java]view plaincopyprint?
packagecom.test.dubboser;
publicclassServiceImp2implementsServiceDemo2{
publicPerson?getPerson(String?str,intage)?{
Person?person=newPerson();
person.setName(str);
person.setAge(age);
returnperson;
}
}
package com.test.dubboser;
public class ServiceImp2 implements ServiceDemo2{
public Person getPerson(String str,int age) {
Person person=new Person();
person.setName(str);
person.setAge(age);
return person;
}
}
4、配置文件
[html]view plaincopyprint?
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
ref="demoService"/>
ref="demoService2"/>
ref="cacheService"/>
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
ref="demoService"/>
ref="demoService2"/>
ref="cacheService"/>
二、服務(wù)消費(fèi)者
1、配置文件
[html]view plaincopyprint?
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
注意這里的這一行,實(shí)現(xiàn)異步配置
[html]view plaincopyprint?
2、消費(fèi)者代碼
[java]view plaincopyprint?
packagecom.test.dubbocli;
importjava.util.concurrent.ExecutionException;
importjava.util.concurrent.Future;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.alibaba.dubbo.rpc.RpcContext;
importcom.test.dubboser.CacheService;
importcom.test.dubboser.Person;
importcom.test.dubboser.ServiceDemo;
importcom.test.dubboser.ServiceDemo2;
publicclassMain?{
publicstaticvoidmain(String[]?args)throwsInterruptedException,?ExecutionException?{
run();
}
publicstaticvoidrun()throwsInterruptedException,?ExecutionException{
ClassPathXmlApplicationContext?context?=newClassPathXmlApplicationContext(newString[]?{"applicationConsumer.xml"});
context.start();
//ServiceDemo?demoServer?=?(ServiceDemo)?context.getBean("demoServicemy");
ServiceDemo2?demoServer2?=?(ServiceDemo2)?context.getBean("demoServicemy2");
/*ServiceDemo?demoServer3?=?(ServiceDemo)?context.getBean("demoServicemy3");*/
/*String?str=demoServer.say("java?---->>>");*/
//調(diào)用后立即返回null
Person?person=demoServer2.getPerson("www",13);
System.err.println("立即返回的為null:"+person);
//拿到調(diào)用的Future引用,當(dāng)結(jié)果返回后,會(huì)被通知和設(shè)置到此Future。
Future?pFuture?=?RpcContext.getContext().getFuture();
//如果Person已返回,直接拿到返回值,否則線程wait,等待Person返回后,線程會(huì)被notify喚醒。
person?=?pFuture.get();
System.out.println("返回的有值"+person);
System.out.println(person);
}
}
package com.test.dubbocli;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.rpc.RpcContext;
import com.test.dubboser.CacheService;
import com.test.dubboser.Person;
import com.test.dubboser.ServiceDemo;
import com.test.dubboser.ServiceDemo2;
public class Main {
public static void main(String[] args) throws InterruptedException, ExecutionException {
run();
}
public static void run() throws InterruptedException, ExecutionException{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
context.start();
//ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");
ServiceDemo2 demoServer2 = (ServiceDemo2) context.getBean("demoServicemy2");
/*ServiceDemo demoServer3 = (ServiceDemo) context.getBean("demoServicemy3");*/
/*String str=demoServer.say("java ---->>>");*/
//調(diào)用后立即返回null
Person person=demoServer2.getPerson("www", 13);
System.err.println("立即返回的為null:"+person);
//拿到調(diào)用的Future引用,當(dāng)結(jié)果返回后,會(huì)被通知和設(shè)置到此Future。
Future pFuture = RpcContext.getContext().getFuture();
//如果Person已返回,直接拿到返回值,否則線程wait,等待Person返回后,線程會(huì)被notify喚醒。
person = pFuture.get();
System.out.println("返回的有值"+person);
System.out.println(person);
}
}
3、運(yùn)行結(jié)果
[html]view plaincopyprint?
立即返回的為null:null
future中獲取值:name:www?age:13
立即返回的為null:null
future中獲取值:name:www age:13
三、異步返回值,和異步無返回值
你也可以設(shè)置是否等待消息發(fā)出:(異步總是不等待返回)
1、sent="true" 等待消息發(fā)出,消息發(fā)送失敗將拋出異常。
2、sent="false" 不等待消息發(fā)出,將消息放入IO隊(duì)列,即刻返回。
[html]view plaincopyprint?
3、如果你只是想異步,完全忽略返回值,可以配置return="false",以減少Future對(duì)象的創(chuàng)建和管理成本:
[html]view plaincopyprint?
設(shè)置了return =“false”后我們就獲取不到Future對(duì)象,當(dāng)然就獲取不到返回值,這樣就只有異步調(diào)用了服務(wù)端方法而沒有返回值,執(zhí)行的流程也就是最開始圖形中的1和2 這兩步,沒有了其他的步驟所以速度也就比較快……