Skip to content

Commit c0b257a

Browse files
authored
issue #11458:Triple stub support async mode (#11464)
* issue #11458:Triple stub support async mode * issue 11458:Triple stub support async mode * issue 11458:Triple stub support async mode
1 parent 34b92b0 commit c0b257a

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

dubbo-compiler/src/main/resources/Dubbo3TripleStub.mustache

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public final class {{className}} {
7272
{{inputType}}.class, {{outputType}}.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
7373
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
7474
{{outputType}}::parseFrom);
75+
76+
private static final StubMethodDescriptor {{methodName}}AsyncMethod = new StubMethodDescriptor("{{originMethodName}}",
77+
{{inputType}}.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
78+
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
79+
{{outputType}}::parseFrom);
80+
81+
private static final StubMethodDescriptor {{methodName}}ProxyAsyncMethod = new StubMethodDescriptor("{{originMethodName}}Async",
82+
{{inputType}}.class, {{outputType}}.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
83+
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
84+
{{outputType}}::parseFrom);
85+
7586
{{/unaryMethods}}
7687

7788
{{#serverStreamingMethods}}
@@ -121,6 +132,10 @@ public final class {{className}} {
121132
return StubInvocationUtil.unaryCall(invoker, {{methodName}}Method, request);
122133
}
123134

135+
public CompletableFuture<{{outputType}}> {{methodName}}Async({{inputType}} request){
136+
return StubInvocationUtil.unaryCall(invoker, {{methodName}}AsyncMethod, request);
137+
}
138+
124139
{{#javaDoc}}
125140
{{{javaDoc}}}
126141
{{/javaDoc}}
@@ -163,6 +178,21 @@ public final class {{className}} {
163178

164179
public static abstract class {{interfaceClassName}}ImplBase implements {{interfaceClassName}}, ServerService<{{interfaceClassName}}> {
165180
181+
private <T, R> BiConsumer<T, StreamObserver<R>> syncToAsync(java.util.function.Function<T, R> syncFun) {
182+
return new BiConsumer<T, StreamObserver<R>>() {
183+
@Override
184+
public void accept(T t, StreamObserver<R> observer) {
185+
try {
186+
R ret = syncFun.apply(t);
187+
observer.onNext(ret);
188+
observer.onCompleted();
189+
} catch (Throwable e) {
190+
observer.onError(e);
191+
}
192+
}
193+
};
194+
}
195+
166196
@Override
167197
public final Invoker<{{interfaceClassName}}> getInvoker(URL url) {
168198
PathResolver pathResolver = url.getOrDefaultFrameworkModel()
@@ -172,11 +202,14 @@ public final class {{className}} {
172202
173203
{{#methods}}
174204
pathResolver.addNativeStub( "/" + SERVICE_NAME + "/{{originMethodName}}" );
205+
pathResolver.addNativeStub( "/" + SERVICE_NAME + "/{{originMethodName}}Async" );
175206
{{/methods}}
176207

177208
{{#unaryMethods}}
178209
BiConsumer<{{inputType}}, StreamObserver<{{outputType}}>> {{methodName}}Func = this::{{methodName}};
179210
handlers.put({{methodName}}Method.getMethodName(), new UnaryStubMethodHandler<>({{methodName}}Func));
211+
BiConsumer<{{inputType}}, StreamObserver<{{outputType}}>> {{methodName}}AsyncFunc = syncToAsync(this::{{methodName}});
212+
handlers.put({{methodName}}ProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>({{methodName}}AsyncFunc));
180213
{{/unaryMethods}}
181214

182215
{{#serverStreamingMethods}}

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/stub/StubInvocationUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ private static Object call(Invoker<?> invoker, MethodDescriptor methodDescriptor
5858
methodDescriptor.getMethodName(), invoker.getInterface().getName(),
5959
invoker.getUrl().getProtocolServiceKey(), methodDescriptor.getParameterClasses(),
6060
arguments);
61+
//When there are multiple MethodDescriptors with the same method name, the return type will be wrong
62+
rpcInvocation.setReturnType(methodDescriptor.getReturnClass());
6163
try {
6264
return InvocationUtil.invoke(invoker, rpcInvocation);
6365
} catch (Throwable e) {

0 commit comments

Comments
 (0)