@@ -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} }
0 commit comments