|
| 1 | +/* |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +* contributor license agreements. See the NOTICE file distributed with |
| 4 | +* this work for additional information regarding copyright ownership. |
| 5 | +* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +* (the "License"); you may not use this file except in compliance with |
| 7 | +* the License. You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package org.apache.dubbo.springboot.demo.idl; |
| 19 | + |
| 20 | +import org.apache.dubbo.common.stream.StreamObserver; |
| 21 | +import org.apache.dubbo.common.URL; |
| 22 | +import org.apache.dubbo.rpc.Invoker; |
| 23 | +import org.apache.dubbo.rpc.PathResolver; |
| 24 | +import org.apache.dubbo.rpc.RpcException; |
| 25 | +import org.apache.dubbo.rpc.ServerService; |
| 26 | +import org.apache.dubbo.rpc.TriRpcStatus; |
| 27 | +import org.apache.dubbo.rpc.model.MethodDescriptor; |
| 28 | +import org.apache.dubbo.rpc.model.ServiceDescriptor; |
| 29 | +import org.apache.dubbo.rpc.model.StubMethodDescriptor; |
| 30 | +import org.apache.dubbo.rpc.model.StubServiceDescriptor; |
| 31 | +import org.apache.dubbo.rpc.service.Destroyable; |
| 32 | +import org.apache.dubbo.rpc.stub.BiStreamMethodHandler; |
| 33 | +import org.apache.dubbo.rpc.stub.ServerStreamMethodHandler; |
| 34 | +import org.apache.dubbo.rpc.stub.StubInvocationUtil; |
| 35 | +import org.apache.dubbo.rpc.stub.StubInvoker; |
| 36 | +import org.apache.dubbo.rpc.stub.StubMethodHandler; |
| 37 | +import org.apache.dubbo.rpc.stub.StubSuppliers; |
| 38 | +import org.apache.dubbo.rpc.stub.UnaryStubMethodHandler; |
| 39 | + |
| 40 | +import com.google.protobuf.Message; |
| 41 | + |
| 42 | +import java.util.HashMap; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.function.BiConsumer; |
| 45 | +import java.util.concurrent.CompletableFuture; |
| 46 | + |
| 47 | +public final class DubboGreeterTriple { |
| 48 | + |
| 49 | + public static final String SERVICE_NAME = Greeter.SERVICE_NAME; |
| 50 | + |
| 51 | + private static final StubServiceDescriptor serviceDescriptor = new StubServiceDescriptor(SERVICE_NAME,Greeter.class); |
| 52 | + |
| 53 | + static { |
| 54 | + org.apache.dubbo.rpc.protocol.tri.service.SchemaDescriptorRegistry.addSchemaDescriptor(SERVICE_NAME,GreeterOuterClass.getDescriptor()); |
| 55 | + StubSuppliers.addSupplier(SERVICE_NAME, DubboGreeterTriple::newStub); |
| 56 | + StubSuppliers.addSupplier(Greeter.JAVA_SERVICE_NAME, DubboGreeterTriple::newStub); |
| 57 | + StubSuppliers.addDescriptor(SERVICE_NAME, serviceDescriptor); |
| 58 | + StubSuppliers.addDescriptor(Greeter.JAVA_SERVICE_NAME, serviceDescriptor); |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings("all") |
| 62 | + public static Greeter newStub(Invoker<?> invoker) { |
| 63 | + return new GreeterStub((Invoker<Greeter>)invoker); |
| 64 | + } |
| 65 | + |
| 66 | + private static final StubMethodDescriptor greetMethod = new StubMethodDescriptor("greet", |
| 67 | + org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, org.apache.dubbo.springboot.demo.idl.GreeterReply.class, MethodDescriptor.RpcType.UNARY, |
| 68 | + obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom, |
| 69 | + org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom); |
| 70 | + |
| 71 | + private static final StubMethodDescriptor greetAsyncMethod = new StubMethodDescriptor("greet", |
| 72 | + org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, java.util.concurrent.CompletableFuture.class, MethodDescriptor.RpcType.UNARY, |
| 73 | + obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom, |
| 74 | + org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom); |
| 75 | + |
| 76 | + private static final StubMethodDescriptor greetProxyAsyncMethod = new StubMethodDescriptor("greetAsync", |
| 77 | + org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, org.apache.dubbo.springboot.demo.idl.GreeterReply.class, MethodDescriptor.RpcType.UNARY, |
| 78 | + obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom, |
| 79 | + org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom); |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + static{ |
| 85 | + serviceDescriptor.addMethod(greetMethod); |
| 86 | + serviceDescriptor.addMethod(greetProxyAsyncMethod); |
| 87 | + } |
| 88 | + |
| 89 | + public static class GreeterStub implements Greeter, Destroyable { |
| 90 | + private final Invoker<Greeter> invoker; |
| 91 | + |
| 92 | + public GreeterStub(Invoker<Greeter> invoker) { |
| 93 | + this.invoker = invoker; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void $destroy() { |
| 98 | + invoker.destroy(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public org.apache.dubbo.springboot.demo.idl.GreeterReply greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){ |
| 103 | + return StubInvocationUtil.unaryCall(invoker, greetMethod, request); |
| 104 | + } |
| 105 | + |
| 106 | + public CompletableFuture<org.apache.dubbo.springboot.demo.idl.GreeterReply> greetAsync(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){ |
| 107 | + return StubInvocationUtil.unaryCall(invoker, greetAsyncMethod, request); |
| 108 | + } |
| 109 | + |
| 110 | + public void greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply> responseObserver){ |
| 111 | + StubInvocationUtil.unaryCall(invoker, greetMethod , request, responseObserver); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public static abstract class GreeterImplBase implements Greeter, ServerService<Greeter> { |
| 119 | + |
| 120 | + private <T, R> BiConsumer<T, StreamObserver<R>> syncToAsync(java.util.function.Function<T, R> syncFun) { |
| 121 | + return new BiConsumer<T, StreamObserver<R>>() { |
| 122 | + @Override |
| 123 | + public void accept(T t, StreamObserver<R> observer) { |
| 124 | + try { |
| 125 | + R ret = syncFun.apply(t); |
| 126 | + observer.onNext(ret); |
| 127 | + observer.onCompleted(); |
| 128 | + } catch (Throwable e) { |
| 129 | + observer.onError(e); |
| 130 | + } |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public CompletableFuture<org.apache.dubbo.springboot.demo.idl.GreeterReply> greetAsync(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){ |
| 137 | + return CompletableFuture.completedFuture(greet(request)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * This server stream type unary method is <b>only</b> used for generated stub to support async unary method. |
| 142 | + * It will not be called if you are NOT using Dubbo3 generated triple stub and <b>DO NOT</b> implement this method. |
| 143 | + */ |
| 144 | + public void greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply> responseObserver){ |
| 145 | + greetAsync(request).whenComplete((r, t) -> { |
| 146 | + if (t != null) { |
| 147 | + responseObserver.onError(t); |
| 148 | + } else { |
| 149 | + responseObserver.onNext(r); |
| 150 | + responseObserver.onCompleted(); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public final Invoker<Greeter> getInvoker(URL url) { |
| 157 | + PathResolver pathResolver = url.getOrDefaultFrameworkModel() |
| 158 | + .getExtensionLoader(PathResolver.class) |
| 159 | + .getDefaultExtension(); |
| 160 | + Map<String,StubMethodHandler<?, ?>> handlers = new HashMap<>(); |
| 161 | + |
| 162 | + pathResolver.addNativeStub( "/" + SERVICE_NAME + "/greet"); |
| 163 | + pathResolver.addNativeStub( "/" + SERVICE_NAME + "/greetAsync"); |
| 164 | + // for compatibility |
| 165 | + pathResolver.addNativeStub( "/" + JAVA_SERVICE_NAME + "/greet"); |
| 166 | + pathResolver.addNativeStub( "/" + JAVA_SERVICE_NAME + "/greetAsync"); |
| 167 | + |
| 168 | + |
| 169 | + BiConsumer<org.apache.dubbo.springboot.demo.idl.GreeterRequest, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply>> greetFunc = this::greet; |
| 170 | + handlers.put(greetMethod.getMethodName(), new UnaryStubMethodHandler<>(greetFunc)); |
| 171 | + BiConsumer<org.apache.dubbo.springboot.demo.idl.GreeterRequest, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply>> greetAsyncFunc = syncToAsync(this::greet); |
| 172 | + handlers.put(greetProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(greetAsyncFunc)); |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + return new StubInvoker<>(this, url, Greeter.class, handlers); |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + @Override |
| 182 | + public org.apache.dubbo.springboot.demo.idl.GreeterReply greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){ |
| 183 | + throw unimplementedMethodException(greetMethod); |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + @Override |
| 191 | + public final ServiceDescriptor getServiceDescriptor() { |
| 192 | + return serviceDescriptor; |
| 193 | + } |
| 194 | + private RpcException unimplementedMethodException(StubMethodDescriptor methodDescriptor) { |
| 195 | + return TriRpcStatus.UNIMPLEMENTED.withDescription(String.format("Method %s is unimplemented", |
| 196 | + "/" + serviceDescriptor.getInterfaceName() + "/" + methodDescriptor.getMethodName())).asException(); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments