|
| 1 | +package com.baeldung.grpc.errorhandling; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.ThreadLocalRandom; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import com.google.protobuf.Any; |
| 13 | +import com.google.rpc.Code; |
| 14 | +import com.google.rpc.ErrorInfo; |
| 15 | + |
| 16 | +import io.grpc.Metadata; |
| 17 | +import io.grpc.Server; |
| 18 | +import io.grpc.ServerBuilder; |
| 19 | +import io.grpc.Status; |
| 20 | +import io.grpc.protobuf.ProtoUtils; |
| 21 | +import io.grpc.protobuf.StatusProto; |
| 22 | +import io.grpc.stub.StreamObserver; |
| 23 | + |
| 24 | +public class CommodityServer { |
| 25 | + |
| 26 | + private static final Logger logger = LoggerFactory.getLogger(CommodityServer.class.getName()); |
| 27 | + private final int port; |
| 28 | + private final Server server; |
| 29 | + private static Map<String, Double> commodityLookupBasePrice; |
| 30 | + static { |
| 31 | + commodityLookupBasePrice = new ConcurrentHashMap<>(); |
| 32 | + commodityLookupBasePrice.put("Commodity1", 5.0); |
| 33 | + commodityLookupBasePrice.put("Commodity2", 6.0); |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + |
| 38 | + CommodityServer commodityServer = new CommodityServer(8980); |
| 39 | + commodityServer.start(); |
| 40 | + if (commodityServer.server != null) { |
| 41 | + commodityServer.server.awaitTermination(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public CommodityServer(int port) throws IOException { |
| 46 | + this.port = port; |
| 47 | + server = ServerBuilder.forPort(port) |
| 48 | + .addService(new CommodityService()) |
| 49 | + .build(); |
| 50 | + } |
| 51 | + |
| 52 | + public void start() throws IOException { |
| 53 | + server.start(); |
| 54 | + logger.info("Server started, listening on {}", port); |
| 55 | + Runtime.getRuntime() |
| 56 | + .addShutdownHook(new Thread() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + System.err.println("shutting down server"); |
| 60 | + try { |
| 61 | + CommodityServer.this.stop(); |
| 62 | + } catch (InterruptedException e) { |
| 63 | + e.printStackTrace(System.err); |
| 64 | + } |
| 65 | + System.err.println("server shutted down"); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public void stop() throws InterruptedException { |
| 71 | + if (server != null) { |
| 72 | + server.shutdown() |
| 73 | + .awaitTermination(30, TimeUnit.SECONDS); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static class CommodityService extends CommodityPriceProviderGrpc.CommodityPriceProviderImplBase { |
| 78 | + |
| 79 | + @Override |
| 80 | + public void getBestCommodityPrice(Commodity request, StreamObserver<CommodityQuote> responseObserver) { |
| 81 | + |
| 82 | + if (commodityLookupBasePrice.get(request.getCommodityName()) == null) { |
| 83 | + |
| 84 | + Metadata.Key<ErrorResponse> errorResponseKey = ProtoUtils.keyForProto(ErrorResponse.getDefaultInstance()); |
| 85 | + ErrorResponse errorResponse = ErrorResponse.newBuilder() |
| 86 | + .setCommodityName(request.getCommodityName()) |
| 87 | + .setAccessToken(request.getAccessToken()) |
| 88 | + .setExpectedValue("Only Commodity1, Commodity2 are supported") |
| 89 | + .build(); |
| 90 | + Metadata metadata = new Metadata(); |
| 91 | + metadata.put(errorResponseKey, errorResponse); |
| 92 | + responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("The commodity is not supported") |
| 93 | + .asRuntimeException(metadata)); |
| 94 | + } else if (request.getAccessToken().equals("123validToken") == false) { |
| 95 | + |
| 96 | + com.google.rpc.Status status = com.google.rpc.Status.newBuilder() |
| 97 | + .setCode(Code.NOT_FOUND.getNumber()) |
| 98 | + .setMessage("The access token not found") |
| 99 | + .addDetails(Any.pack(ErrorInfo.newBuilder() |
| 100 | + .setReason("Invalid Token") |
| 101 | + .setDomain("com.baeldung.grpc.errorhandling") |
| 102 | + .putMetadata("insertToken", "123validToken") |
| 103 | + .build())) |
| 104 | + .build(); |
| 105 | + responseObserver.onError(StatusProto.toStatusRuntimeException(status)); |
| 106 | + } else { |
| 107 | + CommodityQuote commodityQuote = CommodityQuote.newBuilder() |
| 108 | + .setPrice(fetchBestPriceBid(request)) |
| 109 | + .setCommodityName(request.getCommodityName()) |
| 110 | + .setProducerName("Best Producer with best price") |
| 111 | + .build(); |
| 112 | + responseObserver.onNext(commodityQuote); |
| 113 | + responseObserver.onCompleted(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public StreamObserver<Commodity> bidirectionalListOfPrices(StreamObserver<StreamingCommodityQuote> responseObserver) { |
| 119 | + |
| 120 | + return new StreamObserver<Commodity>() { |
| 121 | + @Override |
| 122 | + public void onNext(Commodity request) { |
| 123 | + |
| 124 | + logger.info("Access token:{}", request.getAccessToken()); |
| 125 | + if (request.getAccessToken() |
| 126 | + .equals("123validToken") == false) { |
| 127 | + |
| 128 | + com.google.rpc.Status status = com.google.rpc.Status.newBuilder() |
| 129 | + .setCode(Code.NOT_FOUND.getNumber()) |
| 130 | + .setMessage("The access token not found") |
| 131 | + .addDetails(Any.pack(ErrorInfo.newBuilder() |
| 132 | + .setReason("Invalid Token") |
| 133 | + .setDomain("com.baeldung.grpc.errorhandling") |
| 134 | + .putMetadata("insertToken", "123validToken") |
| 135 | + .build())) |
| 136 | + .build(); |
| 137 | + StreamingCommodityQuote streamingCommodityQuote = StreamingCommodityQuote.newBuilder() |
| 138 | + .setStatus(status) |
| 139 | + .build(); |
| 140 | + responseObserver.onNext(streamingCommodityQuote); |
| 141 | + } else { |
| 142 | + |
| 143 | + for (int i = 1; i <= 5; i++) { |
| 144 | + CommodityQuote commodityQuote = CommodityQuote.newBuilder() |
| 145 | + .setPrice(fetchProviderPriceBid(request, "producer:" + i)) |
| 146 | + .setCommodityName(request.getCommodityName()) |
| 147 | + .setProducerName("producer:" + i) |
| 148 | + .build(); |
| 149 | + StreamingCommodityQuote streamingCommodityQuote = StreamingCommodityQuote.newBuilder() |
| 150 | + .setComodityQuote(commodityQuote) |
| 151 | + .build(); |
| 152 | + responseObserver.onNext(streamingCommodityQuote); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void onCompleted() { |
| 159 | + responseObserver.onCompleted(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onError(Throwable t) { |
| 164 | + logger.info("error:{}", t.getMessage()); |
| 165 | + } |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + private static double fetchBestPriceBid(Commodity commodity) { |
| 172 | + |
| 173 | + return commodityLookupBasePrice.get(commodity.getCommodityName()) + ThreadLocalRandom.current() |
| 174 | + .nextDouble(-0.2d, 0.2d); |
| 175 | + } |
| 176 | + |
| 177 | + private static double fetchProviderPriceBid(Commodity commodity, String providerName) { |
| 178 | + |
| 179 | + return commodityLookupBasePrice.get(commodity.getCommodityName()) + providerName.length() + ThreadLocalRandom.current() |
| 180 | + .nextDouble(-0.2d, 0.2d); |
| 181 | + } |
| 182 | +} |
0 commit comments