├── grpc-example ├── README.md ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── order-service │ ├── src │ │ └── main │ │ │ ├── proto │ │ │ ├── service.proto │ │ │ └── resources.proto │ │ │ ├── resources │ │ │ └── log4j2.properties │ │ │ └── java │ │ │ └── dev │ │ │ └── techdozo │ │ │ └── order │ │ │ └── client │ │ │ └── ProductClient.java │ └── build.gradle ├── product-service │ ├── src │ │ └── main │ │ │ ├── proto │ │ │ ├── service.proto │ │ │ └── resources.proto │ │ │ ├── java │ │ │ └── dev │ │ │ │ └── techdozo │ │ │ │ └── product │ │ │ │ ├── appliction │ │ │ │ ├── ProductInfo.java │ │ │ │ └── repository │ │ │ │ │ └── ProductRepository.java │ │ │ │ ├── api │ │ │ │ └── ProductService.java │ │ │ │ └── ProductServer.java │ │ │ └── resources │ │ │ └── log4j2.properties │ └── build.gradle ├── build.gradle ├── gradlew.bat └── gradlew ├── grpc-server-streaming-rpc ├── README.md ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── grpc-server │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── dev │ │ │ │ └── techdozo │ │ │ │ └── product │ │ │ │ ├── appliction │ │ │ │ ├── Product.java │ │ │ │ └── repository │ │ │ │ │ └── ProductRepository.java │ │ │ │ ├── GrpcServer.java │ │ │ │ └── api │ │ │ │ └── ProductService.java │ │ │ ├── resources │ │ │ └── log4j2.properties │ │ │ └── proto │ │ │ └── service.proto │ └── build.gradle ├── grpc-client │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── log4j2.properties │ │ │ ├── proto │ │ │ └── service.proto │ │ │ └── java │ │ │ └── dev │ │ │ └── techdozo │ │ │ └── order │ │ │ └── client │ │ │ ├── UnaryGrpcBlockingClient.java │ │ │ ├── UnaryGrpcAsynClient.java │ │ │ └── UnaryGrpcBlockingClientErrorResponse.java │ └── build.gradle ├── build.gradle ├── gradlew.bat └── gradlew ├── grpc-unary-rpc ├── settings.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── README.md ├── grpc-client │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── dev │ │ │ │ └── techdozo │ │ │ │ └── order │ │ │ │ ├── context │ │ │ │ ├── UserInfo.java │ │ │ │ └── UserContext.java │ │ │ │ └── client │ │ │ │ ├── interceptor │ │ │ │ ├── GrpcClientRequestInterceptor.java │ │ │ │ └── GrpcClientResponseInterceptor.java │ │ │ │ ├── UnaryGrpcBlockingClient.java │ │ │ │ ├── UnaryGrpcAsynClient.java │ │ │ │ └── UnaryGrpcFutureClient.java │ │ │ ├── resources │ │ │ └── log4j2.properties │ │ │ └── proto │ │ │ └── service.proto │ └── build.gradle ├── grpc-server │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── dev │ │ │ │ └── techdozo │ │ │ │ └── product │ │ │ │ ├── appliction │ │ │ │ ├── Product.java │ │ │ │ └── repository │ │ │ │ │ └── ProductRepository.java │ │ │ │ ├── interceptor │ │ │ │ ├── GrpcServerResponseInterceptor.java │ │ │ │ ├── GrpcServerRequestInterceptor.java │ │ │ │ └── GrpcMDCInterceptor.java │ │ │ │ ├── api │ │ │ │ └── ProductService.java │ │ │ │ └── GrpcServer.java │ │ │ ├── resources │ │ │ └── log4j2.properties │ │ │ └── proto │ │ │ └── service.proto │ └── build.gradle ├── build.gradle ├── gradlew.bat └── gradlew ├── grpc-spring-boot ├── product-api-gateway │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ ├── application.properties │ │ │ └── log4j2.properties │ │ │ ├── java │ │ │ └── dev │ │ │ │ └── techdozo │ │ │ │ └── api │ │ │ │ └── product │ │ │ │ ├── resource │ │ │ │ ├── ProductResponse.java │ │ │ │ └── ProductGatewayController.java │ │ │ │ ├── application │ │ │ │ ├── service │ │ │ │ │ ├── ProductService.java │ │ │ │ │ └── impl │ │ │ │ │ │ └── ProductServiceImpl.java │ │ │ │ ├── model │ │ │ │ │ └── Product.java │ │ │ │ ├── config │ │ │ │ │ ├── ApplicationProperties.java │ │ │ │ │ └── AppConfig.java │ │ │ │ ├── mapper │ │ │ │ │ └── ProductMapper.java │ │ │ │ └── error │ │ │ │ │ └── ServiceExceptionMapper.java │ │ │ │ └── ProductGatewayApplication.java │ │ │ └── proto │ │ │ ├── service.proto │ │ │ └── resources.proto │ └── build.gradle ├── settings.gradle ├── product-service │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ ├── application.properties │ │ │ └── log4j2.properties │ │ │ ├── proto │ │ │ ├── service.proto │ │ │ └── resources.proto │ │ │ └── java │ │ │ └── dev │ │ │ └── techdozo │ │ │ └── product │ │ │ ├── appliction │ │ │ ├── Product.java │ │ │ └── repository │ │ │ │ ├── ProductRepository.java │ │ │ │ └── impl │ │ │ │ └── ProductRepositoryImpl.java │ │ │ ├── ProductApplication.java │ │ │ ├── api │ │ │ ├── mapper │ │ │ │ └── ProductMapper.java │ │ │ ├── interceptor │ │ │ │ ├── LogInterceptor.java │ │ │ │ ├── ExceptionHandler.java │ │ │ │ └── GlobalExceptionHandlerInterceptor.java │ │ │ └── ProductApiService.java │ │ │ └── config │ │ │ └── AppConfig.java │ └── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── commons │ ├── build.gradle │ └── src │ │ └── main │ │ └── java │ │ └── dev │ │ └── techdozo │ │ └── commons │ │ └── error │ │ ├── ErrorDetails.java │ │ ├── RepositoryException.java │ │ ├── ApplicationException.java │ │ ├── ServiceException.java │ │ ├── InvalidOperationException.java │ │ ├── ErrorResponse.java │ │ ├── BaseException.java │ │ ├── ResourceNotFoundException.java │ │ ├── GlobalExceptionHandler.java │ │ └── ErrorCode.java ├── build.gradle ├── gradlew.bat └── gradlew └── .gitignore /grpc-example/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /grpc-unary-rpc/settings.gradle: -------------------------------------------------------------------------------- 1 | include 'grpc-server' 2 | include 'grpc-client' 3 | 4 | -------------------------------------------------------------------------------- /grpc-example/settings.gradle: -------------------------------------------------------------------------------- 1 | include 'order-service' 2 | include 'product-service' 3 | 4 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/settings.gradle: -------------------------------------------------------------------------------- 1 | include 'grpc-server' 2 | include 'grpc-client' 3 | 4 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | product.host=localhost 2 | product.port=8001 -------------------------------------------------------------------------------- /grpc-spring-boot/settings.gradle: -------------------------------------------------------------------------------- 1 | include 'product-api-gateway' 2 | include 'product-service' 3 | include 'commons' 4 | 5 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | grpc.server.port=8001 2 | spring.main.web-application-type=none -------------------------------------------------------------------------------- /grpc-example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techdozo/grpc/HEAD/grpc-example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /grpc-unary-rpc/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techdozo/grpc/HEAD/grpc-unary-rpc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /grpc-spring-boot/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techdozo/grpc/HEAD/grpc-spring-boot/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techdozo/grpc/HEAD/grpc-server-streaming-rpc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /grpc-spring-boot/commons/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | dependencies { 6 | implementation 'org.springframework.boot:spring-boot-starter-web' 7 | } 8 | -------------------------------------------------------------------------------- /grpc-unary-rpc/README.md: -------------------------------------------------------------------------------- 1 | ## gRPC unary RPC code example 2 | Code example related to gRPC unary RPC and interceptor as mentioned in the blog - 3 | - https://techdozo.dev/grpc-synchronous-and-asynchronous-unary-rpc-in-java/ -------------------------------------------------------------------------------- /grpc-example/order-service/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | import "resources.proto"; 4 | 5 | service ProductService { 6 | rpc getProduct(GetProductRequest) returns (GetProductResponse); 7 | } -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | import "resources.proto"; 4 | 5 | service ProductService { 6 | rpc getProduct(GetProductRequest) returns (GetProductResponse); 7 | } -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/context/UserInfo.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.context; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | 6 | @Builder 7 | @Getter 8 | public class UserInfo { 9 | private String userToken; 10 | } 11 | -------------------------------------------------------------------------------- /grpc-example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jul 03 08:25:01 IST 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /grpc-spring-boot/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jul 03 08:25:01 IST 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /grpc-unary-rpc/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jul 03 08:25:01 IST 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jul 03 08:25:01 IST 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /grpc-example/order-service/src/main/proto/resources.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | 4 | 5 | message GetProductRequest { 6 | string productId = 1; 7 | 8 | } 9 | 10 | message GetProductResponse { 11 | string name = 1; 12 | string description = 2; 13 | double price = 3; 14 | } -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/proto/resources.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | 4 | 5 | message GetProductRequest { 6 | string productId = 1; 7 | 8 | } 9 | 10 | message GetProductResponse { 11 | string name = 1; 12 | string description = 2; 13 | double price = 3; 14 | } -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/resource/ProductResponse.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.resource; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Setter 8 | @Getter 9 | @AllArgsConstructor 10 | public class ProductResponse { 11 | private String productId; 12 | } 13 | -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/java/dev/techdozo/product/appliction/ProductInfo.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | 6 | @Getter 7 | @Builder 8 | public class ProductInfo { 9 | private String sku; 10 | private String name; 11 | private String description; 12 | private double price; 13 | } 14 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/appliction/Product.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | 6 | @Getter 7 | @Builder 8 | public class Product { 9 | private String productId; 10 | private String name; 11 | private String description; 12 | private double price; 13 | } 14 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.service; 2 | 3 | 4 | import dev.techdozo.api.product.application.model.Product; 5 | 6 | public interface ProductService { 7 | String createNewProduct(Product product); 8 | Product getProduct(String productId); 9 | } 10 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/java/dev/techdozo/product/appliction/Product.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | 6 | @Getter 7 | @Builder 8 | public class Product { 9 | private String productId; 10 | private String name; 11 | private String description; 12 | private double price; 13 | } 14 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ErrorDetails.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import lombok.Getter; 4 | 5 | public class ErrorDetails { 6 | @Getter 7 | public enum Code { 8 | RESOURCE_ID("err_resource_id"), 9 | MESSAGE("err_message"); 10 | 11 | private final String key; 12 | 13 | Code(String key) { 14 | this.key = key; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product; 3 | 4 | import "resources.proto"; 5 | 6 | option java_package = "dev.techdozo.product.resource"; 7 | option java_multiple_files = true; 8 | 9 | 10 | service ProductService { 11 | rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse); 12 | rpc GetProduct(GetProductRequest) returns (GetProductResponse); 13 | } -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product; 3 | 4 | import "resources.proto"; 5 | 6 | option java_package = "dev.techdozo.product.resource"; 7 | option java_multiple_files = true; 8 | 9 | 10 | service ProductService { 11 | rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse); 12 | rpc GetProduct(GetProductRequest) returns (GetProductResponse); 13 | } -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/model/Product.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import lombok.ToString; 6 | 7 | @Setter 8 | @Getter 9 | @ToString 10 | public class Product { 11 | private String name; 12 | private String description; 13 | private double price; 14 | private String userId; 15 | } 16 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/appliction/Product.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction; 2 | 3 | import lombok.EqualsAndHashCode; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | @EqualsAndHashCode 10 | public class Product { 11 | private String productId; 12 | private String name; 13 | private String description; 14 | private double price; 15 | private String userId; 16 | } 17 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/config/ApplicationProperties.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.config; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | 7 | @ConfigurationProperties("product") 8 | @Setter 9 | @Getter 10 | public class ApplicationProperties { 11 | private String host; 12 | private int port; 13 | } 14 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/ProductApplication.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @Slf4j 8 | @SpringBootApplication 9 | public class ProductApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ProductApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /grpc-example/order-service/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/resources/log4j2.properties: -------------------------------------------------------------------------------- 1 | status=warn 2 | appender.console.type=Console 3 | appender.console.name=console 4 | appender.console.layout.type=PatternLayout 5 | appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n 6 | # Log to console and rolling file 7 | logger.app.name=dev.techdozo 8 | logger.app.level=debug 9 | logger.app.additivity=false 10 | logger.app.appenderRef.console.ref=console 11 | rootLogger.level=info 12 | rootLogger.appenderRef.stdout.ref=console -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | 4 | service ProductService { 5 | 6 | // A simple RPC. 7 | // returns Product for a given id 8 | rpc GetProduct(GetProductRequest) returns (GetProductResponse); 9 | } 10 | 11 | message GetProductRequest { 12 | string product_id = 1; 13 | } 14 | 15 | message GetProductResponse { 16 | Product product = 1; 17 | } 18 | 19 | message Product { 20 | string name = 1; 21 | string description = 2; 22 | double price = 3; 23 | } -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | 4 | service ProductService { 5 | 6 | // A simple RPC. 7 | // returns Product for a given id 8 | rpc GetProduct(GetProductRequest) returns (GetProductResponse); 9 | } 10 | 11 | message GetProductRequest { 12 | string product_id = 1; 13 | } 14 | 15 | message GetProductResponse { 16 | Product product = 1; 17 | } 18 | 19 | message Product { 20 | string name = 1; 21 | string description = 2; 22 | double price = 3; 23 | } -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/ProductGatewayApplication.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = {"dev.techdozo.api.product", "dev.techdozo.commons"}) 7 | public class ProductGatewayApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ProductGatewayApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/appliction/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction.repository; 2 | 3 | import dev.techdozo.product.appliction.Product; 4 | 5 | public interface ProductRepository { 6 | 7 | /** 8 | * Get product based on the productId. Throws ResourceNotFoundException if product id is not found. 9 | * 10 | * @param productId id of the product. 11 | * @return Product 12 | */ 13 | Product get(String productId) ; 14 | 15 | String save(Product product); 16 | } 17 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/RepositoryException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import java.util.Map; 4 | 5 | public class RepositoryException extends BaseException { 6 | 7 | public RepositoryException(ErrorCode errorCode,String message, Map errorMetaData) { 8 | super(errorCode,message, errorMetaData); 9 | } 10 | 11 | public RepositoryException(ErrorCode errorCode,String message, Map errorMetaData, Throwable cause) { 12 | super(errorCode,message, errorMetaData, cause); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ApplicationException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import java.util.Map; 4 | 5 | public class ApplicationException extends BaseException { 6 | 7 | public ApplicationException(ErrorCode errorCode,String message, Map errorMetaData) { 8 | super(errorCode,message, errorMetaData); 9 | } 10 | 11 | public ApplicationException(ErrorCode errorCode,String message, Map errorMetaData, Throwable cause) { 12 | super(errorCode,message, errorMetaData, cause); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ServiceException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import java.util.Map; 4 | 5 | public class ServiceException extends BaseException { 6 | 7 | public ServiceException(ErrorCode errorCode, String message, Map errorMetaData) { 8 | super(errorCode, message, errorMetaData); 9 | } 10 | 11 | public ServiceException( 12 | ErrorCode errorCode, String message, Map errorMetaData, Throwable cause) { 13 | super(errorCode, message, errorMetaData, cause); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/mapper/ProductMapper.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.mapper; 2 | 3 | import dev.techdozo.api.product.application.model.Product; 4 | import dev.techdozo.product.Resources; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.factory.Mappers; 7 | 8 | @Mapper 9 | public abstract class ProductMapper { 10 | 11 | public static final ProductMapper MAPPER = 12 | Mappers.getMapper(ProductMapper.class); 13 | 14 | public abstract Product map(Resources.GetProductResponse getProductResponse); 15 | } 16 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/InvalidOperationException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import java.util.Map; 4 | 5 | public class InvalidOperationException extends ServiceException { 6 | 7 | public InvalidOperationException(ErrorCode errorCode,String message, Map errorMetaData) { 8 | super(errorCode,message, errorMetaData); 9 | } 10 | 11 | public InvalidOperationException(ErrorCode errorCode, 12 | String message, Map errorMetaData, Throwable cause) { 13 | super(errorCode,message, errorMetaData, cause); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | .class 4 | /build/ 5 | build 6 | !gradle/wrapper/gradle-wrapper.jar 7 | out 8 | 9 | ### STS ### 10 | .apt_generated 11 | .classpath 12 | .factorypath 13 | .project 14 | .settings 15 | .springBeans 16 | .sts4-cache 17 | 18 | ### IntelliJ IDEA ### 19 | .idea 20 | *.iws 21 | *.iml 22 | *.ipr 23 | /out/ 24 | 25 | ### NetBeans ### 26 | /nbproject/private/ 27 | /nbbuild/ 28 | /dist/ 29 | /nbdist/ 30 | /.nb-gradle/ 31 | ##gradlew 32 | ##gradlew.bat 33 | 34 | 35 | ### VS Code ### 36 | .vscode/ 37 | 38 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 39 | hs_err_pid* 40 | /src/main/generated/ 41 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/api/mapper/ProductMapper.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api.mapper; 2 | 3 | import dev.techdozo.product.Resources; 4 | import dev.techdozo.product.appliction.Product; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.ReportingPolicy; 7 | import org.mapstruct.factory.Mappers; 8 | 9 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 10 | public abstract class ProductMapper { 11 | 12 | public static final ProductMapper MAPPER = 13 | Mappers.getMapper(ProductMapper.class); 14 | 15 | public abstract Product map(Resources.CreateProductRequest createProductRequest); 16 | } 17 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/api/interceptor/LogInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api.interceptor; 2 | 3 | import io.grpc.Metadata; 4 | import io.grpc.ServerCall; 5 | import io.grpc.ServerCallHandler; 6 | import io.grpc.ServerInterceptor; 7 | import lombok.extern.slf4j.Slf4j; 8 | 9 | @Slf4j 10 | public class LogInterceptor implements ServerInterceptor { 11 | @Override 12 | public ServerCall.Listener interceptCall( 13 | ServerCall call, Metadata headers, ServerCallHandler next) { 14 | log.info(call.getMethodDescriptor().getFullMethodName()); 15 | return next.startCall(call, headers); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | import "google/rpc/status.proto"; 4 | 5 | service ProductService { 6 | 7 | // A server side streaming RPC 8 | // streams product information for a given productId 9 | rpc ListProduct(ListProductRequest) returns (stream GetProductResponse); 10 | } 11 | 12 | message GetProductResponse { 13 | oneof product_response { 14 | Product product = 1; 15 | google.rpc.Status error = 2; 16 | } 17 | } 18 | 19 | message ListProductRequest { 20 | repeated string productId = 1; 21 | } 22 | 23 | message Product { 24 | string name = 1; 25 | string description = 2; 26 | double price = 3; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/proto/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product.api; 3 | import "google/rpc/status.proto"; 4 | 5 | service ProductService { 6 | 7 | // A server side streaming RPC 8 | // streams product information for a given productId 9 | rpc ListProduct(ListProductRequest) returns (stream GetProductResponse); 10 | } 11 | 12 | message GetProductResponse { 13 | oneof product_response { 14 | Product product = 1; 15 | google.rpc.Status error = 2; 16 | } 17 | } 18 | 19 | message ListProductRequest { 20 | repeated string productId = 1; 21 | } 22 | 23 | message Product { 24 | string name = 1; 25 | string description = 2; 26 | double price = 3; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ErrorResponse.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 4 | import com.fasterxml.jackson.annotation.JsonTypeName; 5 | import lombok.Data; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | import java.util.Map; 10 | 11 | import static com.fasterxml.jackson.annotation.JsonTypeInfo.As; 12 | import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id; 13 | 14 | @Setter 15 | @Getter 16 | @Data 17 | @JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME, visible = true) 18 | @JsonTypeName("error") 19 | public class ErrorResponse { 20 | private ErrorCode errorCode; 21 | private String message; 22 | private Map details; 23 | } 24 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/proto/resources.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product; 3 | 4 | message CreateProductRequest { 5 | string name = 1; 6 | string description = 2; 7 | double price = 3; 8 | string userId = 4; 9 | } 10 | 11 | message CreateProductResponse { 12 | string productId = 1; 13 | } 14 | 15 | message GetProductRequest { 16 | string productId = 1; 17 | } 18 | 19 | message GetProductResponse { 20 | string name = 1; 21 | string description = 2; 22 | double price = 3; 23 | string userId = 4; 24 | } 25 | 26 | message ErrorDetail { 27 | // Error code 28 | string errorCode = 1; 29 | //Error message 30 | string message = 2; 31 | // Additional metadata associated with the Error 32 | map metadata = 3; 33 | } -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/BaseException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import lombok.Getter; 4 | 5 | import java.util.Map; 6 | 7 | @Getter 8 | public class BaseException extends RuntimeException { 9 | 10 | private Map errorMetaData; 11 | private ErrorCode errorCode; 12 | 13 | public BaseException(ErrorCode errorCode,String message, Map errorMetaData) { 14 | super(message); 15 | this.errorCode = errorCode; 16 | this.errorMetaData = errorMetaData; 17 | } 18 | 19 | public BaseException(ErrorCode errorCode,String message, Map errorMetaData, Throwable cause) { 20 | super(message, cause); 21 | this.errorCode = errorCode; 22 | this.errorMetaData = errorMetaData; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/proto/resources.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package dev.techdozo.product; 3 | import "google/protobuf/any.proto"; 4 | 5 | message CreateProductRequest { 6 | string name = 1; 7 | string description = 2; 8 | double price = 3; 9 | string userId = 4; 10 | } 11 | 12 | message CreateProductResponse { 13 | string productId = 1; 14 | } 15 | 16 | message GetProductRequest { 17 | string productId = 1; 18 | } 19 | 20 | message GetProductResponse { 21 | string name = 1; 22 | string description = 2; 23 | double price = 3; 24 | string userId = 4; 25 | } 26 | 27 | message ErrorDetail { 28 | // Error code 29 | string errorCode = 1; 30 | //Error message 31 | string message = 2; 32 | // Additional metadata associated with the Error 33 | map metadata = 3; 34 | } -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/context/UserContext.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.context; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | @Slf4j 6 | public final class UserContext { 7 | 8 | private UserContext() {} 9 | 10 | private static InheritableThreadLocal currentUserContext = 11 | new InheritableThreadLocal<>(); 12 | 13 | public static void setUserContext(String userId) { 14 | log.info("Setting userId to {} ", userId); 15 | currentUserContext.set(UserInfo.builder().userToken(userId).build()); 16 | } 17 | 18 | public static UserInfo getUserContext() { 19 | return currentUserContext.get(); 20 | } 21 | 22 | public static void clear() { 23 | log.info("Removing user context, current context {}", currentUserContext.get()); 24 | currentUserContext.remove(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/interceptor/GrpcServerResponseInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.interceptor; 2 | 3 | import io.grpc.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | 6 | @Slf4j 7 | public class GrpcServerResponseInterceptor implements ServerInterceptor { 8 | 9 | @Override 10 | public ServerCall.Listener interceptCall( 11 | ServerCall serverCall, Metadata metadata, ServerCallHandler next) { 12 | 13 | return next.startCall( 14 | new ForwardingServerCall.SimpleForwardingServerCall<>(serverCall) { 15 | @Override 16 | public void sendMessage(RespT message) { 17 | log.info("Message being sent to client : " + message); 18 | super.sendMessage(message); 19 | } 20 | }, 21 | metadata); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /grpc-example/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "io.spring.dependency-management" version "1.0.8.RELEASE" 4 | } 5 | 6 | group = 'dev.techdozo.grpc' 7 | sourceCompatibility = '11' 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | subprojects { 13 | 14 | group = 'dev.techdozo.grpc' 15 | version = '0.0.1' 16 | apply plugin: 'java' 17 | apply plugin: 'io.spring.dependency-management' 18 | apply plugin: 'java-library' 19 | 20 | 21 | repositories { 22 | maven { url 'https://repo.osgeo.org/repository/release/' } 23 | mavenCentral() 24 | mavenLocal() 25 | maven { url 'https://repo.spring.io/snapshot' } 26 | maven { url 'https://repo.spring.io/milestone' } 27 | } 28 | 29 | dependencyManagement { 30 | imports { 31 | mavenBom("org.springframework.boot:spring-boot-dependencies:2.4.5") 32 | } 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /grpc-unary-rpc/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "io.spring.dependency-management" version "1.0.8.RELEASE" 4 | } 5 | 6 | group = 'dev.techdozo.grpc' 7 | sourceCompatibility = '11' 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | subprojects { 13 | 14 | group = 'dev.techdozo.grpc' 15 | version = '0.0.1' 16 | apply plugin: 'java' 17 | apply plugin: 'io.spring.dependency-management' 18 | apply plugin: 'java-library' 19 | 20 | 21 | repositories { 22 | maven { url 'https://repo.osgeo.org/repository/release/' } 23 | mavenCentral() 24 | mavenLocal() 25 | maven { url 'https://repo.spring.io/snapshot' } 26 | maven { url 'https://repo.spring.io/milestone' } 27 | } 28 | 29 | dependencyManagement { 30 | imports { 31 | mavenBom("org.springframework.boot:spring-boot-dependencies:2.4.5") 32 | } 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "io.spring.dependency-management" version "1.0.8.RELEASE" 4 | } 5 | 6 | group = 'dev.techdozo.grpc' 7 | sourceCompatibility = '11' 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | subprojects { 13 | 14 | group = 'dev.techdozo.grpc' 15 | version = '0.0.1' 16 | apply plugin: 'java' 17 | apply plugin: 'io.spring.dependency-management' 18 | apply plugin: 'java-library' 19 | 20 | 21 | repositories { 22 | maven { url 'https://repo.osgeo.org/repository/release/' } 23 | mavenCentral() 24 | mavenLocal() 25 | maven { url 'https://repo.spring.io/snapshot' } 26 | maven { url 'https://repo.spring.io/milestone' } 27 | } 28 | 29 | dependencyManagement { 30 | imports { 31 | mavenBom("org.springframework.boot:spring-boot-dependencies:2.4.5") 32 | } 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/interceptor/GrpcServerRequestInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.interceptor; 2 | 3 | import io.grpc.Metadata; 4 | import io.grpc.ServerCall; 5 | import io.grpc.ServerCallHandler; 6 | import io.grpc.ServerInterceptor; 7 | import lombok.extern.slf4j.Slf4j; 8 | 9 | @Slf4j 10 | public class GrpcServerRequestInterceptor implements ServerInterceptor { 11 | 12 | @Override 13 | public ServerCall.Listener interceptCall( 14 | ServerCall serverCall, Metadata metadata, ServerCallHandler next) { 15 | 16 | log.info("Validating user token"); 17 | var userToken = metadata.get(Metadata.Key.of("JWT", Metadata.ASCII_STRING_MARSHALLER)); 18 | validateUserToken(userToken); 19 | return next.startCall(serverCall, metadata); 20 | } 21 | 22 | private void validateUserToken(String userToken) { 23 | // Logic to validate token 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import java.util.Map; 4 | 5 | public class ResourceNotFoundException extends RepositoryException { 6 | 7 | public ResourceNotFoundException(String message, Map errorMetaData) { 8 | this(ErrorCode.RESOURCE_NOT_FOUND,message, errorMetaData); 9 | } 10 | 11 | public ResourceNotFoundException(ErrorCode errorCode, String message, Map errorMetaData) { 12 | super(errorCode,message, errorMetaData); 13 | } 14 | 15 | public ResourceNotFoundException(String message, Map errorMetaData, Throwable cause) { 16 | this(ErrorCode.RESOURCE_NOT_FOUND,message, errorMetaData, cause); 17 | } 18 | 19 | public ResourceNotFoundException(ErrorCode errorCode, 20 | String message, Map errorMetaData, Throwable cause) { 21 | super(errorCode,message, errorMetaData, cause); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.config; 2 | 3 | import dev.techdozo.product.api.interceptor.ExceptionHandler; 4 | import dev.techdozo.product.api.interceptor.GlobalExceptionHandlerInterceptor; 5 | import dev.techdozo.product.api.interceptor.LogInterceptor; 6 | import net.devh.boot.grpc.server.advice.GrpcAdvice; 7 | import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration(proxyBeanMethods = false) 11 | public class AppConfig { 12 | 13 | @GrpcGlobalServerInterceptor 14 | public LogInterceptor logServerInterceptor() { 15 | return new LogInterceptor(); 16 | } 17 | 18 | @GrpcAdvice 19 | public ExceptionHandler exceptionHandler() { 20 | return new ExceptionHandler(); 21 | } 22 | 23 | // @GrpcGlobalServerInterceptor 24 | // public GlobalExceptionHandlerInterceptor errorHandler() { 25 | // return new GlobalExceptionHandlerInterceptor(); 26 | // } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.config; 2 | 3 | import dev.techdozo.api.product.application.service.ProductService; 4 | import dev.techdozo.api.product.application.service.impl.ProductServiceImpl; 5 | import io.grpc.ManagedChannel; 6 | import io.grpc.ManagedChannelBuilder; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | public class AppConfig { 12 | 13 | @Bean 14 | public ManagedChannel managedChannel() { 15 | return ManagedChannelBuilder.forAddress( 16 | applicationProperties().getHost(), applicationProperties().getPort()) 17 | .usePlaintext() 18 | .build(); 19 | } 20 | 21 | @Bean 22 | public ProductService productService() { 23 | return new ProductServiceImpl(managedChannel()); 24 | } 25 | 26 | @Bean 27 | public ApplicationProperties applicationProperties() { 28 | return new ApplicationProperties(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/client/interceptor/GrpcClientRequestInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client.interceptor; 2 | 3 | import dev.techdozo.order.context.UserContext; 4 | import io.grpc.*; 5 | import lombok.extern.slf4j.Slf4j; 6 | 7 | @Slf4j 8 | public class GrpcClientRequestInterceptor implements ClientInterceptor { 9 | 10 | public ClientCall interceptCall( 11 | final MethodDescriptor methodDescriptor, 12 | final CallOptions callOptions, 13 | final Channel channel) { 14 | 15 | return new ForwardingClientCall.SimpleForwardingClientCall( 16 | channel.newCall(methodDescriptor, callOptions)) { 17 | 18 | @Override 19 | public void start(ClientCall.Listener responseListener, Metadata headers) { 20 | var userToken = UserContext.getUserContext().getUserToken(); 21 | log.info("Setting userToken {} in header", userToken); 22 | headers.put(Metadata.Key.of("JWT", Metadata.ASCII_STRING_MARSHALLER), userToken); 23 | super.start(responseListener, headers); 24 | } 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /grpc-example/order-service/src/main/java/dev/techdozo/order/client/ProductClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.product.api.ProductServiceGrpc; 4 | import dev.techdozo.product.api.Resources.GetProductRequest; 5 | import io.grpc.ManagedChannelBuilder; 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | @Slf4j 9 | public class ProductClient { 10 | 11 | private final ProductServiceGrpc.ProductServiceBlockingStub productServiceBlockingStub; 12 | 13 | public ProductClient(String host, int port) { 14 | var managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build(); 15 | this.productServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 16 | } 17 | 18 | public void call() { 19 | 20 | log.info("Calling Server.."); 21 | 22 | var productRequest = GetProductRequest.newBuilder().setProductId("apple-123").build(); 23 | 24 | var productResponse = productServiceBlockingStub.getProduct(productRequest); 25 | 26 | log.info("Received Product from server, info {}", productResponse); 27 | } 28 | 29 | public static void main(String[] args) { 30 | var client = new ProductClient("0.0.0.0", 8081); 31 | client.call(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/client/interceptor/GrpcClientResponseInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client.interceptor; 2 | 3 | import io.grpc.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | 6 | @Slf4j 7 | public class GrpcClientResponseInterceptor implements ClientInterceptor { 8 | 9 | public ClientCall interceptCall( 10 | final MethodDescriptor methodDescriptor, 11 | final CallOptions callOptions, 12 | final Channel channel) { 13 | 14 | return new ForwardingClientCall.SimpleForwardingClientCall( 15 | channel.newCall(methodDescriptor, callOptions)) { 16 | 17 | @Override 18 | public void start(Listener responseListener, Metadata headers) { 19 | super.start( 20 | new ForwardingClientCallListener.SimpleForwardingClientCallListener( 21 | responseListener) { 22 | 23 | @Override 24 | public void onMessage(RespT message) { 25 | log.debug("Received response from Server: {}", message); 26 | super.onMessage(message); 27 | } 28 | }, 29 | headers); 30 | } 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/appliction/repository/impl/ProductRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction.repository.impl; 2 | 3 | import dev.techdozo.commons.error.ErrorCode; 4 | import dev.techdozo.commons.error.ResourceNotFoundException; 5 | import dev.techdozo.product.appliction.Product; 6 | import dev.techdozo.product.appliction.repository.ProductRepository; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | import java.util.Optional; 12 | import java.util.UUID; 13 | 14 | @Repository 15 | public class ProductRepositoryImpl implements ProductRepository { 16 | 17 | private final Map productStorage; 18 | 19 | public ProductRepositoryImpl() { 20 | productStorage = new HashMap<>(); 21 | } 22 | 23 | public Product get(String productId) { 24 | var product = Optional.ofNullable(productStorage.get(productId)); 25 | 26 | return product.orElseThrow( 27 | () -> 28 | new ResourceNotFoundException( 29 | "Product ID not found", 30 | Map.of("resource_id", productId, "message", "Product ID not found"))); 31 | } 32 | 33 | @Override 34 | public String save(Product product) { 35 | var uuid = UUID.randomUUID().toString(); 36 | productStorage.put(uuid, product); 37 | return uuid; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/resource/ProductGatewayController.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.resource; 2 | 3 | import dev.techdozo.api.product.application.model.Product; 4 | import dev.techdozo.api.product.application.service.ProductService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | import javax.validation.constraints.NotBlank; 11 | 12 | @RestController 13 | public class ProductGatewayController { 14 | 15 | @Autowired private ProductService productService; 16 | 17 | @PostMapping("/products") 18 | public ResponseEntity createProduct( 19 | @RequestHeader(value = "userId") String userId, @RequestBody Product product) { 20 | 21 | product.setUserId(userId); 22 | var productId = productService.createNewProduct(product); 23 | var productResponse = new ProductResponse(productId); 24 | return new ResponseEntity<>(productResponse, HttpStatus.CREATED); 25 | } 26 | 27 | @GetMapping("/products/{productId}") 28 | public ResponseEntity getProduct(@PathVariable @NotBlank String productId) { 29 | var product = productService.getProduct(productId); 30 | return new ResponseEntity<>(product, HttpStatus.CREATED); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "org.springframework.boot" version "2.4.5" 3 | id "com.google.protobuf" version "0.8.16" 4 | id 'java' 5 | } 6 | 7 | group 'dev.techdozo.grpc' 8 | version '1.0-SNAPSHOT' 9 | sourceCompatibility = 11 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | dependencies { 16 | // compile(project(":commons")) { 17 | // transitive = false 18 | // } 19 | 20 | implementation project(':commons') 21 | 22 | //gRPC dependencies 23 | implementation 'net.devh:grpc-server-spring-boot-starter:2.12.0.RELEASE' 24 | 25 | //Logging 26 | implementation 'org.apache.logging.log4j:log4j-api:2.13.3' 27 | implementation 'org.apache.logging.log4j:log4j-core:2.13.3' 28 | 29 | testCompile group: 'junit', name: 'junit', version: '4.12' 30 | } 31 | 32 | sourceSets { 33 | main { 34 | java { 35 | srcDirs 'build/generated/source/proto/main/grpc' 36 | srcDirs 'build/generated/source/proto/main/java' 37 | } 38 | } 39 | } 40 | 41 | protobuf { 42 | protoc { 43 | artifact = 'com.google.protobuf:protoc:3.12.4' 44 | } 45 | plugins { 46 | grpc { 47 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 48 | } 49 | } 50 | generateProtoTasks { 51 | all()*.plugins { 52 | grpc {} 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.ControllerAdvice; 7 | import org.springframework.web.bind.annotation.ExceptionHandler; 8 | 9 | @Slf4j 10 | @ControllerAdvice 11 | public class GlobalExceptionHandler { 12 | 13 | @ExceptionHandler(ServiceException.class) 14 | public ResponseEntity handleException(ServiceException cause) { 15 | var errorResponse = new ErrorResponse(); 16 | var errorCode = cause.getErrorCode(); 17 | errorResponse.setErrorCode(errorCode); 18 | errorResponse.setMessage(cause.getMessage()); 19 | errorResponse.setDetails(cause.getErrorMetaData()); 20 | return new ResponseEntity<>(errorResponse, errorCode.getHttpStatus()); 21 | } 22 | 23 | @ExceptionHandler(ResourceNotFoundException.class) 24 | public ResponseEntity handleException(ResourceNotFoundException cause) { 25 | var errorResponse = new ErrorResponse(); 26 | errorResponse.setErrorCode(ErrorCode.RESOURCE_NOT_FOUND); 27 | errorResponse.setMessage(cause.getMessage()); 28 | errorResponse.setDetails(cause.getErrorMetaData()); 29 | return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /grpc-spring-boot/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "io.spring.dependency-management" version "1.0.11.RELEASE" 3 | } 4 | 5 | group = 'dev.techdozo.grpc' 6 | //repositories { 7 | // mavenCentral() 8 | //} 9 | 10 | def mapstructVersion = '1.4.2.Final' 11 | def lombokVersion = '1.18.6' 12 | 13 | subprojects { 14 | 15 | group = 'dev.techdozo.grpc' 16 | version = '0.0.1' 17 | apply plugin: 'io.spring.dependency-management' 18 | apply plugin: 'java-library' 19 | 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compileOnly "org.projectlombok:lombok:${lombokVersion}" 27 | annotationProcessor "org.projectlombok:lombok:${lombokVersion}" 28 | implementation "org.mapstruct:mapstruct:${mapstructVersion}" 29 | annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}" 30 | //tests 31 | testImplementation 'com.google.code.bean-matchers:bean-matchers:0.12' 32 | testImplementation 'org.mockito:mockito-core:3.6.0' 33 | testCompile group: 'junit', name: 'junit', version: '4.13.1' 34 | testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3' 35 | } 36 | 37 | dependencyManagement { 38 | imports { 39 | mavenBom("org.springframework.boot:spring-boot-dependencies:2.5.0") 40 | } 41 | } 42 | 43 | compileJava { 44 | sourceCompatibility = 11 45 | targetCompatibility = 11 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0.0' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | dependencies { 14 | //gRPC dependencies 15 | implementation 'com.google.protobuf:protobuf-java:3.17.3' 16 | implementation 'io.grpc:grpc-all:1.40.1' 17 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 18 | 19 | //Lombok dependencies - 20 | compileOnly 'org.projectlombok:lombok:1.18.20' 21 | annotationProcessor 'org.projectlombok:lombok:1.18.20' 22 | 23 | //Logging 24 | implementation 'org.apache.logging.log4j:log4j-api:2.14.1' 25 | implementation 'org.apache.logging.log4j:log4j-core:2.14.1' 26 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' 27 | 28 | testCompile group: 'junit', name: 'junit', version: '4.12' 29 | } 30 | 31 | sourceSets { 32 | main { 33 | java { 34 | srcDirs 'build/generated/source/proto/main/grpc' 35 | srcDirs 'build/generated/source/proto/main/java' 36 | } 37 | } 38 | } 39 | 40 | protobuf { 41 | protoc { 42 | artifact = 'com.google.protobuf:protoc:3.12.4' 43 | } 44 | plugins { 45 | grpc { 46 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 47 | } 48 | } 49 | generateProtoTasks { 50 | all()*.plugins { 51 | grpc {} 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /grpc-example/order-service/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0-SNAPSHOT' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | dependencies { 14 | //gRPC dependencies 15 | implementation 'com.google.protobuf:protobuf-java:3.12.4' 16 | implementation 'io.grpc:grpc-all:1.31.0' 17 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 18 | 19 | //Lombok dependencies - 20 | compileOnly 'org.projectlombok:lombok:1.18.6' 21 | annotationProcessor 'org.projectlombok:lombok:1.18.6' 22 | 23 | //Logging 24 | implementation 'org.apache.logging.log4j:log4j-api:2.13.3' 25 | implementation 'org.apache.logging.log4j:log4j-core:2.13.3' 26 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.3' 27 | 28 | testCompile group: 'junit', name: 'junit', version: '4.12' 29 | } 30 | 31 | sourceSets { 32 | main { 33 | java { 34 | srcDirs 'build/generated/source/proto/main/grpc' 35 | srcDirs 'build/generated/source/proto/main/java' 36 | } 37 | } 38 | } 39 | 40 | protobuf { 41 | protoc { 42 | artifact = 'com.google.protobuf:protoc:3.12.4' 43 | } 44 | plugins { 45 | grpc { 46 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 47 | } 48 | } 49 | generateProtoTasks { 50 | all()*.plugins { 51 | grpc {} 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0.0' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | dependencies { 14 | //gRPC dependencies 15 | implementation 'com.google.protobuf:protobuf-java:3.17.3' 16 | implementation 'io.grpc:grpc-all:1.40.1' 17 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 18 | 19 | //Lombok dependencies - 20 | compileOnly 'org.projectlombok:lombok:1.18.20' 21 | annotationProcessor 'org.projectlombok:lombok:1.18.20' 22 | 23 | //Logging 24 | implementation 'org.apache.logging.log4j:log4j-api:2.14.1' 25 | implementation 'org.apache.logging.log4j:log4j-core:2.14.1' 26 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' 27 | 28 | testCompile group: 'junit', name: 'junit', version: '4.12' 29 | } 30 | 31 | sourceSets { 32 | main { 33 | java { 34 | srcDirs 'build/generated/source/proto/main/grpc' 35 | srcDirs 'build/generated/source/proto/main/java' 36 | } 37 | } 38 | } 39 | 40 | protobuf { 41 | protoc { 42 | artifact = 'com.google.protobuf:protoc:3.12.4' 43 | } 44 | plugins { 45 | grpc { 46 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 47 | } 48 | } 49 | generateProtoTasks { 50 | all()*.plugins { 51 | grpc {} 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0.0' 8 | sourceCompatibility = 11 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | dependencies { 15 | //gRPC dependencies 16 | implementation 'com.google.protobuf:protobuf-java:3.17.3' 17 | implementation 'io.grpc:grpc-all:1.40.1' 18 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 19 | 20 | //Lombok dependencies - 21 | compileOnly 'org.projectlombok:lombok:1.18.20' 22 | annotationProcessor 'org.projectlombok:lombok:1.18.20' 23 | 24 | //Logging 25 | implementation 'org.apache.logging.log4j:log4j-api:2.14.1' 26 | implementation 'org.apache.logging.log4j:log4j-core:2.14.1' 27 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' 28 | 29 | testCompile group: 'junit', name: 'junit', version: '4.12' 30 | } 31 | 32 | sourceSets { 33 | main { 34 | java { 35 | srcDirs 'build/generated/source/proto/main/grpc' 36 | srcDirs 'build/generated/source/proto/main/java' 37 | } 38 | } 39 | } 40 | 41 | protobuf { 42 | protoc { 43 | artifact = 'com.google.protobuf:protoc:3.12.4' 44 | } 45 | plugins { 46 | grpc { 47 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 48 | } 49 | } 50 | generateProtoTasks { 51 | all()*.plugins { 52 | grpc {} 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /grpc-example/product-service/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0-SNAPSHOT' 8 | sourceCompatibility = 11 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | dependencies { 15 | //gRPC dependencies 16 | implementation 'com.google.protobuf:protobuf-java:3.12.4' 17 | implementation 'io.grpc:grpc-all:1.31.0' 18 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 19 | 20 | //Lombok dependencies - 21 | compileOnly 'org.projectlombok:lombok:1.18.6' 22 | annotationProcessor 'org.projectlombok:lombok:1.18.6' 23 | 24 | //Logging 25 | implementation 'org.apache.logging.log4j:log4j-api:2.13.3' 26 | implementation 'org.apache.logging.log4j:log4j-core:2.13.3' 27 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.3' 28 | 29 | testCompile group: 'junit', name: 'junit', version: '4.12' 30 | } 31 | 32 | sourceSets { 33 | main { 34 | java { 35 | srcDirs 'build/generated/source/proto/main/grpc' 36 | srcDirs 'build/generated/source/proto/main/java' 37 | } 38 | } 39 | } 40 | 41 | protobuf { 42 | protoc { 43 | artifact = 'com.google.protobuf:protoc:3.12.4' 44 | } 45 | plugins { 46 | grpc { 47 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 48 | } 49 | } 50 | generateProtoTasks { 51 | all()*.plugins { 52 | grpc {} 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | } 5 | 6 | group 'dev.techdozo.grpc' 7 | version '1.0.0' 8 | sourceCompatibility = 11 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | dependencies { 15 | //gRPC dependencies 16 | implementation 'com.google.protobuf:protobuf-java:3.17.3' 17 | implementation 'io.grpc:grpc-all:1.40.1' 18 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 19 | 20 | //Lombok dependencies - 21 | compileOnly 'org.projectlombok:lombok:1.18.20' 22 | annotationProcessor 'org.projectlombok:lombok:1.18.20' 23 | 24 | //Logging 25 | implementation 'org.apache.logging.log4j:log4j-api:2.14.1' 26 | implementation 'org.apache.logging.log4j:log4j-core:2.14.1' 27 | implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' 28 | 29 | testCompile group: 'junit', name: 'junit', version: '4.12' 30 | } 31 | 32 | sourceSets { 33 | main { 34 | java { 35 | srcDirs 'build/generated/source/proto/main/grpc' 36 | srcDirs 'build/generated/source/proto/main/java' 37 | } 38 | } 39 | } 40 | 41 | protobuf { 42 | protoc { 43 | artifact = 'com.google.protobuf:protoc:3.12.4' 44 | } 45 | plugins { 46 | grpc { 47 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 48 | } 49 | } 50 | generateProtoTasks { 51 | all()*.plugins { 52 | grpc {} 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /grpc-spring-boot/commons/src/main/java/dev/techdozo/commons/error/ErrorCode.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.commons.error; 2 | 3 | import lombok.Getter; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.lang.Nullable; 6 | 7 | @Getter 8 | public enum ErrorCode { 9 | 10 | /** Specified resource not found */ 11 | RESOURCE_NOT_FOUND("ResourceNotFound", "Resource not found", HttpStatus.NOT_FOUND), 12 | BAD_ARGUMENT("BadArgument", "Bad argument", HttpStatus.BAD_REQUEST), 13 | INVALID_OPERATION("InvalidOperation", "Operation not allowed", HttpStatus.PRECONDITION_FAILED); 14 | 15 | private final String shortCode; 16 | 17 | private final String message; 18 | 19 | private final HttpStatus httpStatus; 20 | 21 | 22 | ErrorCode(String shortCode, String message, HttpStatus httpStatus) { 23 | this.shortCode = shortCode; 24 | this.message = message; 25 | this.httpStatus = httpStatus; 26 | } 27 | 28 | public static ErrorCode errorCode(String shortCode) { 29 | var errorCode = resolve(shortCode); 30 | if (errorCode == null) { 31 | throw new IllegalArgumentException("No matching constant for [" + shortCode + "]"); 32 | } 33 | return errorCode; 34 | } 35 | 36 | @Nullable 37 | public static ErrorCode resolve(String shortCode) { 38 | // used cached VALUES instead of values() to prevent array allocation 39 | for (ErrorCode errorCode : values()) { 40 | if (errorCode.shortCode.equals(shortCode)) { 41 | return errorCode; 42 | } 43 | } 44 | return null; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/error/ServiceExceptionMapper.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.error; 2 | 3 | import com.google.protobuf.Any; 4 | import com.google.protobuf.InvalidProtocolBufferException; 5 | import dev.techdozo.commons.error.ErrorCode; 6 | import dev.techdozo.commons.error.ServiceException; 7 | import dev.techdozo.product.Resources.ErrorDetail; 8 | import io.grpc.StatusRuntimeException; 9 | 10 | import java.util.Map; 11 | 12 | /** Maps gRPC error to generic {@link ServiceException} */ 13 | public class ServiceExceptionMapper { 14 | 15 | public static ServiceException map(StatusRuntimeException error) { 16 | 17 | var status = io.grpc.protobuf.StatusProto.fromThrowable(error); 18 | 19 | ErrorDetail errorDetail = null; 20 | 21 | for (Any any : status.getDetailsList()) { 22 | if (!any.is(ErrorDetail.class)) { 23 | continue; 24 | } 25 | try { 26 | errorDetail = any.unpack(ErrorDetail.class); 27 | } catch (InvalidProtocolBufferException cause) { 28 | errorDetail = 29 | ErrorDetail.newBuilder() 30 | .setErrorCode(ErrorCode.INVALID_OPERATION.getMessage()) 31 | .setMessage(cause.getMessage()) 32 | .putAllMetadata(Map.of()) 33 | .build(); 34 | } 35 | } 36 | 37 | return new ServiceException( 38 | ErrorCode.errorCode(errorDetail.getErrorCode()), 39 | errorDetail.getMessage(), 40 | errorDetail.getMetadataMap()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/java/dev/techdozo/product/appliction/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction.repository; 2 | 3 | import dev.techdozo.product.appliction.ProductInfo; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Optional; 8 | 9 | public class ProductRepository { 10 | 11 | private final Map productStorage; 12 | 13 | public ProductRepository() { 14 | productStorage = loadProduct(); 15 | } 16 | 17 | public Optional get(String productId) { 18 | return Optional.ofNullable(productStorage.get(productId)); 19 | } 20 | 21 | private Map loadProduct() { 22 | Map products = new HashMap<>(); 23 | products.put( 24 | "apple-123", 25 | ProductInfo.builder() 26 | .sku("abc") 27 | .name("Apple iPhone 12 Pro (128GB)") 28 | .description("Apple iPhone 12 Pro (128GB) - Graphite") 29 | .price(1617.29) 30 | .build()); 31 | products.put( 32 | "apple-124", 33 | ProductInfo.builder() 34 | .sku("abc") 35 | .name("Apple iPhone 12 Pro Max (128GB)") 36 | .description("Apple iPhone 12 Pro (128GB) - Graphite") 37 | .price(1752.59) 38 | .build()); 39 | return products; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/appliction/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction.repository; 2 | 3 | import dev.techdozo.product.appliction.Product; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Optional; 8 | 9 | public class ProductRepository { 10 | 11 | private final Map productStorage; 12 | 13 | public ProductRepository() { 14 | productStorage = loadProduct(); 15 | } 16 | 17 | public Optional get(String productId) { 18 | var product = 19 | Product.builder() 20 | .productId(productId) 21 | .name("Apple iPhone 12 Pro (128GB) - " + productId) 22 | .description("Apple iPhone 12 Pro (128GB) - Graphite - " + productId) 23 | .price(1617.29) 24 | .build(); 25 | return Optional.ofNullable(product); 26 | } 27 | 28 | private Map loadProduct() { 29 | Map products = new HashMap<>(); 30 | products.put( 31 | "apple-123", 32 | Product.builder() 33 | .productId("abc") 34 | .name("Apple iPhone 12 Pro (128GB)") 35 | .description("Apple iPhone 12 Pro (128GB) - Graphite") 36 | .price(1617.29) 37 | .build()); 38 | products.put( 39 | "apple-124", 40 | Product.builder() 41 | .productId("abc") 42 | .name("Apple iPhone 12 Pro Max (128GB)") 43 | .description("Apple iPhone 12 Pro (128GB) - Graphite") 44 | .price(1752.59) 45 | .build()); 46 | return products; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/java/dev/techdozo/product/appliction/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.appliction.repository; 2 | 3 | import dev.techdozo.product.appliction.Product; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Optional; 8 | 9 | public class ProductRepository { 10 | 11 | private final Map productStorage; 12 | 13 | public ProductRepository() { 14 | productStorage = loadProduct(); 15 | } 16 | 17 | public Optional get(String productId) { 18 | return Optional.ofNullable(productStorage.get(productId)); 19 | } 20 | 21 | private Map loadProduct() { 22 | Map products = new HashMap<>(); 23 | products.put( 24 | "apple-123", 25 | Product.builder() 26 | .productId("apple-123") 27 | .name("Apple iPhone 12 Pro (128GB)") 28 | .description("Apple iPhone 12 Pro (128GB) - Graphite") 29 | .price(1617.29) 30 | .build()); 31 | products.put( 32 | "apple-124", 33 | Product.builder() 34 | .productId("apple-124") 35 | .name("Apple iPhone 12 Pro Max (128GB)") 36 | .description("Apple iPhone 12 Pro (128GB) - Red") 37 | .price(1752.59) 38 | .build()); 39 | products.put( 40 | "apple-125", 41 | Product.builder() 42 | .productId("apple-125") 43 | .name("Apple iPhone 12 Pro Max (128GB)") 44 | .description("Apple iPhone 12 Pro (128GB) - Blue") 45 | .price(1701.59) 46 | .build()); 47 | return products; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcBlockingClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.product.api.ProductServiceGrpc; 4 | import dev.techdozo.product.api.Service.ListProductRequest; 5 | import io.grpc.ManagedChannelBuilder; 6 | import io.grpc.StatusRuntimeException; 7 | import lombok.extern.slf4j.Slf4j; 8 | 9 | import java.util.List; 10 | 11 | @Slf4j 12 | public class UnaryGrpcBlockingClient { 13 | 14 | private final String host; 15 | private final int port; 16 | 17 | public UnaryGrpcBlockingClient(String host, int port) { 18 | this.host = host; 19 | this.port = port; 20 | } 21 | 22 | public void callServer() { 23 | 24 | log.info("Calling Server.."); 25 | var managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build(); 26 | var productServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 27 | 28 | var productRequest = 29 | ListProductRequest.newBuilder() 30 | .addAllProductId(List.of("apple-123", "apple-124", "apple-125")) 31 | .build(); 32 | 33 | var productResponse = productServiceBlockingStub.listProduct(productRequest); 34 | 35 | try { 36 | while (productResponse.hasNext()) { 37 | log.info("Received Product from server, info {}", productResponse.next()); 38 | } 39 | } catch (StatusRuntimeException statusRuntimeException) { 40 | log.error("Error, message {}", statusRuntimeException.getMessage()); 41 | } 42 | } 43 | 44 | public static void main(String[] args) { 45 | var client = new UnaryGrpcBlockingClient("0.0.0.0", 3000); 46 | client.callServer(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/api/interceptor/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api.interceptor; 2 | 3 | import com.google.protobuf.Any; 4 | import com.google.rpc.Code; 5 | import dev.techdozo.commons.error.ResourceNotFoundException; 6 | import dev.techdozo.product.Resources.ErrorDetail; 7 | import io.grpc.Status; 8 | import io.grpc.StatusRuntimeException; 9 | import io.grpc.protobuf.StatusProto; 10 | import lombok.extern.slf4j.Slf4j; 11 | import net.devh.boot.grpc.server.advice.GrpcAdvice; 12 | import net.devh.boot.grpc.server.advice.GrpcExceptionHandler; 13 | 14 | @Slf4j 15 | @GrpcAdvice 16 | public class ExceptionHandler { 17 | 18 | @GrpcExceptionHandler(ResourceNotFoundException.class) 19 | public StatusRuntimeException handleResourceNotFoundException(ResourceNotFoundException cause) { 20 | 21 | log.error("Error, message {}", cause.getMessage()); 22 | 23 | ErrorDetail errorDetail = 24 | ErrorDetail.newBuilder() 25 | .setErrorCode(cause.getErrorCode().getShortCode()) 26 | .setMessage(cause.getMessage()) 27 | .putAllMetadata(cause.getErrorMetaData()) 28 | .build(); 29 | 30 | var status = 31 | com.google.rpc.Status.newBuilder() 32 | .setCode(Code.NOT_FOUND.getNumber()) 33 | .setMessage("Resource not found") 34 | .addDetails(Any.pack(errorDetail)) 35 | .build(); 36 | 37 | return StatusProto.toStatusRuntimeException(status); 38 | } 39 | 40 | @GrpcExceptionHandler 41 | public Status handleInvalidArgument(IllegalArgumentException e) { 42 | return Status.INVALID_ARGUMENT.withDescription("Your description").withCause(e); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id "com.google.protobuf" version "0.8.12" 4 | id 'org.springframework.boot' version '2.3.1.RELEASE' 5 | } 6 | 7 | group 'dev.techdozo.grpc' 8 | version '1.0-SNAPSHOT' 9 | sourceCompatibility = 11 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | dependencies { 16 | implementation project(':commons') 17 | //gRPC dependencies 18 | implementation 'com.google.protobuf:protobuf-java:3.12.4' 19 | implementation 'io.grpc:grpc-all:1.37.0' 20 | implementation 'javax.annotation:javax.annotation-api:1.3.2' 21 | 22 | //Spring Boot 23 | implementation 'org.springframework.boot:spring-boot-starter' 24 | implementation 'org.springframework.boot:spring-boot-starter-data-rest' 25 | 26 | //Validation 27 | compile group: 'org.hibernate', name: 'hibernate-validator', version: '7.0.1.Final' 28 | 29 | 30 | //Logging 31 | implementation 'org.apache.logging.log4j:log4j-api:2.13.3' 32 | implementation 'org.apache.logging.log4j:log4j-core:2.13.3' 33 | 34 | testCompile group: 'junit', name: 'junit', version: '4.12' 35 | } 36 | 37 | sourceSets { 38 | main { 39 | java { 40 | srcDirs 'build/generated/source/proto/main/grpc' 41 | srcDirs 'build/generated/source/proto/main/java' 42 | } 43 | } 44 | } 45 | 46 | protobuf { 47 | protoc { 48 | artifact = 'com.google.protobuf:protoc:3.12.4' 49 | } 50 | plugins { 51 | grpc { 52 | artifact = 'io.grpc:protoc-gen-grpc-java:1.31.0' 53 | } 54 | } 55 | generateProtoTasks { 56 | all()*.plugins { 57 | grpc {} 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/java/dev/techdozo/product/api/ProductService.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api; 2 | 3 | import dev.techdozo.product.api.Resources.GetProductRequest; 4 | import dev.techdozo.product.api.Resources.GetProductResponse; 5 | import dev.techdozo.product.appliction.ProductInfo; 6 | import dev.techdozo.product.appliction.repository.ProductRepository; 7 | import io.grpc.Status; 8 | import io.grpc.StatusException; 9 | import io.grpc.stub.StreamObserver; 10 | import lombok.extern.slf4j.Slf4j; 11 | 12 | import java.util.Optional; 13 | 14 | @Slf4j 15 | public class ProductService extends ProductServiceGrpc.ProductServiceImplBase { 16 | 17 | private final ProductRepository productRepository; 18 | 19 | public ProductService() { 20 | this.productRepository = new ProductRepository(); 21 | } 22 | 23 | @Override 24 | public void getProduct( 25 | GetProductRequest request, StreamObserver responseObserver) { 26 | 27 | log.info("Calling product repository.."); 28 | 29 | var productId = request.getProductId(); 30 | Optional productInfo = productRepository.get(productId); 31 | 32 | if (productInfo.isPresent()) { 33 | var product = productInfo.get(); 34 | 35 | var getProductResponse = 36 | GetProductResponse.newBuilder() 37 | .setName(product.getName()) 38 | .setDescription(product.getDescription()) 39 | .setPrice(product.getPrice()) 40 | .build(); 41 | responseObserver.onNext(getProductResponse); 42 | responseObserver.onCompleted(); 43 | } else { 44 | responseObserver.onError(new StatusException(Status.NOT_FOUND)); 45 | } 46 | 47 | log.info("Finished calling Product API service.."); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/java/dev/techdozo/product/GrpcServer.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product; 2 | 3 | import dev.techdozo.product.api.ProductService; 4 | import io.grpc.Server; 5 | import io.grpc.ServerBuilder; 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | import java.io.IOException; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | @Slf4j 12 | public class GrpcServer { 13 | private final int port; 14 | private final Server server; 15 | 16 | public GrpcServer(int port) { 17 | this.port = port; 18 | var productService = new ProductService(); 19 | this.server = ServerBuilder.forPort(port).addService(productService).build(); 20 | } 21 | 22 | public void start() throws IOException { 23 | log.info("Starting Server.."); 24 | server.start(); 25 | log.info("Server Started on port {} ", port); 26 | 27 | Runtime.getRuntime() 28 | .addShutdownHook( 29 | new Thread( 30 | () -> { 31 | try { 32 | this.stop(); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | })); 37 | } 38 | 39 | private void stop() throws InterruptedException { 40 | log.info("Stopping Server.."); 41 | if (server != null) { 42 | server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 43 | } 44 | } 45 | 46 | private void blockUntilShutDown() throws InterruptedException { 47 | if (this.server != null) { 48 | server.awaitTermination(); 49 | } 50 | } 51 | 52 | public static void main(String[] args) throws IOException, InterruptedException { 53 | var productServer = new GrpcServer(3000); 54 | productServer.start(); 55 | productServer.blockUntilShutDown(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcBlockingClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.order.client.interceptor.GrpcClientRequestInterceptor; 4 | import dev.techdozo.order.client.interceptor.GrpcClientResponseInterceptor; 5 | import dev.techdozo.order.context.UserContext; 6 | import dev.techdozo.product.api.ProductServiceGrpc; 7 | import dev.techdozo.product.api.Service.GetProductRequest; 8 | import io.grpc.ManagedChannelBuilder; 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | import java.util.Random; 12 | 13 | @Slf4j 14 | public class UnaryGrpcBlockingClient { 15 | 16 | private final String host; 17 | private final int port; 18 | 19 | public UnaryGrpcBlockingClient(String host, int port) { 20 | this.host = host; 21 | this.port = port; 22 | } 23 | 24 | public void callServer() { 25 | 26 | log.info("Calling Server.."); 27 | var managedChannel = 28 | ManagedChannelBuilder.forAddress(host, port) 29 | .intercept(new GrpcClientResponseInterceptor(), new GrpcClientRequestInterceptor()) 30 | .usePlaintext() 31 | .build(); 32 | 33 | var productServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 34 | 35 | 36 | var productRequest = GetProductRequest.newBuilder().setProductId("apple-123").build(); 37 | 38 | var productResponse = productServiceBlockingStub.getProduct(productRequest); 39 | 40 | log.info("Received Product from server, info {}", productResponse); 41 | } 42 | 43 | public static void main(String[] args) { 44 | // Pseudo code to generate JWT token 45 | var token = "Bearer " + new Random().nextInt(); 46 | UserContext.setUserContext(token); 47 | var client = new UnaryGrpcBlockingClient("0.0.0.0", 3000); 48 | client.callServer(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/api/ProductService.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api; 2 | 3 | import dev.techdozo.product.api.Service.GetProductRequest; 4 | import dev.techdozo.product.api.Service.GetProductResponse; 5 | import dev.techdozo.product.appliction.Product; 6 | import dev.techdozo.product.appliction.repository.ProductRepository; 7 | import io.grpc.Status; 8 | import io.grpc.StatusException; 9 | import io.grpc.stub.StreamObserver; 10 | import lombok.extern.slf4j.Slf4j; 11 | 12 | import java.util.Optional; 13 | 14 | @Slf4j 15 | public class ProductService extends ProductServiceGrpc.ProductServiceImplBase { 16 | 17 | private final ProductRepository productRepository; 18 | 19 | public ProductService() { 20 | this.productRepository = new ProductRepository(); 21 | } 22 | 23 | @Override 24 | public void getProduct( 25 | GetProductRequest request, StreamObserver responseObserver) { 26 | 27 | log.info("Calling product repository.."); 28 | 29 | var productId = request.getProductId(); 30 | //Fetch Product information from repository 31 | Optional optionalProduct = productRepository.get(productId); 32 | 33 | if (optionalProduct.isPresent()) { 34 | var product = optionalProduct.get(); 35 | //If found build response 36 | var productResponse = 37 | Service.Product.newBuilder() 38 | .setName(product.getName()) 39 | .setDescription(product.getDescription()) 40 | .setPrice(product.getPrice()) 41 | .build(); 42 | var getProductResponse = GetProductResponse.newBuilder().setProduct(productResponse).build(); 43 | 44 | responseObserver.onNext(getProductResponse); 45 | responseObserver.onCompleted(); 46 | } else { 47 | responseObserver.onError(new StatusException(Status.NOT_FOUND)); 48 | } 49 | log.info("Finished calling Product API service.."); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /grpc-example/product-service/src/main/java/dev/techdozo/product/ProductServer.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product; 2 | 3 | import dev.techdozo.product.api.ProductService; 4 | import io.grpc.Server; 5 | import io.grpc.ServerBuilder; 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | import java.io.IOException; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | @Slf4j 12 | public class ProductServer { 13 | 14 | private final int port; 15 | private final Server server; 16 | 17 | public ProductServer(int port) { 18 | this.port = port; 19 | var productService = new ProductService(); 20 | this.server = ServerBuilder.forPort(port).addService(productService).build(); 21 | } 22 | 23 | public void start() throws IOException { 24 | log.info("Starting Server.."); 25 | 26 | server.start(); 27 | 28 | log.info("Server Started on port {} ", port); 29 | 30 | Runtime.getRuntime() 31 | .addShutdownHook( 32 | new Thread( 33 | () -> { 34 | try { 35 | this.stop(); 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | })); 40 | } 41 | 42 | private void stop() throws InterruptedException { 43 | log.info("Stopping Server.."); 44 | 45 | if (server != null) { 46 | server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 47 | } 48 | } 49 | 50 | private void blockUntilShutDown() throws InterruptedException { 51 | if (this.server != null) { 52 | server.awaitTermination(); 53 | } 54 | } 55 | 56 | public static void main(String[] args) throws IOException, InterruptedException { 57 | var productServer = new ProductServer(8081); 58 | productServer.start(); 59 | productServer.blockUntilShutDown(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcAsynClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.product.api.ProductServiceGrpc; 4 | import dev.techdozo.product.api.Service; 5 | import dev.techdozo.product.api.Service.GetProductResponse; 6 | import io.grpc.ManagedChannelBuilder; 7 | import io.grpc.stub.StreamObserver; 8 | import lombok.SneakyThrows; 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | import java.util.List; 12 | 13 | @Slf4j 14 | public class UnaryGrpcAsynClient { 15 | 16 | private final String host; 17 | private final int port; 18 | 19 | public UnaryGrpcAsynClient(String host, int port) { 20 | this.host = host; 21 | this.port = port; 22 | } 23 | 24 | @SneakyThrows 25 | public void callServer() { 26 | 27 | log.info("Calling Server.."); 28 | var managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build(); 29 | // Create a new async stub 30 | var productServiceAsyncStub = ProductServiceGrpc.newStub(managedChannel); 31 | 32 | var productRequest = 33 | Service.ListProductRequest.newBuilder() 34 | .addAllProductId(List.of("apple-123", "apple-124", "apple-125")) 35 | .build(); 36 | 37 | productServiceAsyncStub.listProduct(productRequest, new ProductCallback()); 38 | 39 | Thread.sleep(3000); 40 | 41 | log.info("Finished call"); 42 | } 43 | 44 | private static class ProductCallback implements StreamObserver { 45 | 46 | @Override 47 | public void onNext(GetProductResponse value) { 48 | log.info("Received product, {}", value); 49 | } 50 | 51 | @Override 52 | public void onError(Throwable cause) { 53 | log.error("Error occurred, cause {}", cause.getMessage()); 54 | } 55 | 56 | @Override 57 | public void onCompleted() { 58 | log.info("Stream completed"); 59 | } 60 | } 61 | 62 | public static void main(String[] args) { 63 | var client = new UnaryGrpcAsynClient("0.0.0.0", 3000); 64 | client.callServer(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcBlockingClientErrorResponse.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.product.api.ProductServiceGrpc; 4 | import dev.techdozo.product.api.Service.ListProductRequest; 5 | import io.grpc.ManagedChannelBuilder; 6 | import lombok.extern.slf4j.Slf4j; 7 | 8 | import java.util.List; 9 | 10 | import static dev.techdozo.product.api.Service.GetProductResponse.ProductResponseCase.ERROR; 11 | import static dev.techdozo.product.api.Service.GetProductResponse.ProductResponseCase.PRODUCT; 12 | 13 | @Slf4j 14 | public class UnaryGrpcBlockingClientErrorResponse { 15 | 16 | private final String host; 17 | private final int port; 18 | 19 | public UnaryGrpcBlockingClientErrorResponse(String host, int port) { 20 | this.host = host; 21 | this.port = port; 22 | } 23 | 24 | public void callServer() { 25 | 26 | log.info("Calling Server.."); 27 | var managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build(); 28 | var productServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 29 | 30 | var productRequest = 31 | ListProductRequest.newBuilder() 32 | .addAllProductId(List.of("apple-123", "apple-128", "apple-125")) 33 | .build(); 34 | 35 | var productResponse = productServiceBlockingStub.listProduct(productRequest); 36 | 37 | while (productResponse.hasNext()) { 38 | var getProductResponse = productResponse.next(); 39 | var productResponseCase = getProductResponse.getProductResponseCase(); 40 | 41 | if (productResponseCase == PRODUCT) { 42 | log.info("Received Product from server, info {}", getProductResponse); 43 | } else if (productResponseCase == ERROR) 44 | // Some business logic 45 | log.info("Received Error {}", getProductResponse.getError()); 46 | } 47 | } 48 | 49 | public static void main(String[] args) { 50 | var client = new UnaryGrpcBlockingClientErrorResponse("0.0.0.0", 3000); 51 | client.callServer(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/GrpcServer.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product; 2 | 3 | import dev.techdozo.product.api.ProductService; 4 | import dev.techdozo.product.interceptor.GrpcServerRequestInterceptor; 5 | import dev.techdozo.product.interceptor.GrpcServerResponseInterceptor; 6 | import io.grpc.Server; 7 | import io.grpc.ServerBuilder; 8 | import io.grpc.ServerInterceptors; 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | import java.io.IOException; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | @Slf4j 15 | public class GrpcServer { 16 | 17 | private final int port; 18 | private final Server server; 19 | 20 | public GrpcServer(int port) { 21 | this.port = port; 22 | var productService = new ProductService(); 23 | this.server = 24 | ServerBuilder.forPort(port) 25 | .addService( 26 | ServerInterceptors.intercept( 27 | productService, 28 | new GrpcServerResponseInterceptor(), 29 | new GrpcServerRequestInterceptor())) 30 | .build(); 31 | } 32 | 33 | public void start() throws IOException { 34 | log.info("Starting Server.."); 35 | 36 | server.start(); 37 | 38 | log.info("Server Started on port {} ", port); 39 | 40 | Runtime.getRuntime() 41 | .addShutdownHook( 42 | new Thread( 43 | () -> { 44 | try { 45 | this.stop(); 46 | } catch (InterruptedException e) { 47 | e.printStackTrace(); 48 | } 49 | })); 50 | } 51 | 52 | private void stop() throws InterruptedException { 53 | log.info("Stopping Server.."); 54 | 55 | if (server != null) { 56 | server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 57 | } 58 | } 59 | 60 | private void blockUntilShutDown() throws InterruptedException { 61 | if (this.server != null) { 62 | server.awaitTermination(); 63 | } 64 | } 65 | 66 | public static void main(String[] args) throws IOException, InterruptedException { 67 | var productServer = new GrpcServer(3000); 68 | productServer.start(); 69 | productServer.blockUntilShutDown(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/api/ProductApiService.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api; 2 | 3 | import dev.techdozo.product.Resources.CreateProductRequest; 4 | import dev.techdozo.product.Resources.CreateProductResponse; 5 | import dev.techdozo.product.Resources.GetProductRequest; 6 | import dev.techdozo.product.Resources.GetProductResponse; 7 | import dev.techdozo.product.api.mapper.ProductMapper; 8 | import dev.techdozo.product.appliction.repository.ProductRepository; 9 | import dev.techdozo.product.resource.ProductServiceGrpc; 10 | import io.grpc.stub.StreamObserver; 11 | import lombok.extern.slf4j.Slf4j; 12 | import net.devh.boot.grpc.server.service.GrpcService; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | 15 | @Slf4j 16 | @GrpcService 17 | public class ProductApiService extends ProductServiceGrpc.ProductServiceImplBase { 18 | 19 | @Autowired private ProductRepository productRepository; 20 | 21 | @Override 22 | public void createProduct( 23 | CreateProductRequest request, StreamObserver responseObserver) { 24 | 25 | log.info("Calling Product Repository.."); 26 | 27 | var product = ProductMapper.MAPPER.map(request); 28 | var productId = productRepository.save(product); 29 | 30 | var createProductResponse = CreateProductResponse.newBuilder().setProductId(productId).build(); 31 | 32 | responseObserver.onNext(createProductResponse); 33 | responseObserver.onCompleted(); 34 | 35 | log.info("Saved Product, Id {} ..", productId); 36 | } 37 | 38 | @Override 39 | public void getProduct( 40 | GetProductRequest request, StreamObserver responseObserver) { 41 | 42 | log.info("Calling Product Repository.."); 43 | 44 | String productId = request.getProductId(); 45 | 46 | var product = productRepository.get(productId); 47 | 48 | var response = 49 | GetProductResponse.newBuilder() 50 | .setName(product.getName()) 51 | .setDescription(product.getDescription()) 52 | .setPrice(product.getPrice()) 53 | .setUserId(product.getUserId()) 54 | .build(); 55 | responseObserver.onNext(response); 56 | responseObserver.onCompleted(); 57 | 58 | log.info("Finished calling Product API service.."); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcAsynClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import dev.techdozo.order.client.interceptor.GrpcClientRequestInterceptor; 4 | import dev.techdozo.order.context.UserContext; 5 | import dev.techdozo.product.api.ProductServiceGrpc; 6 | import dev.techdozo.product.api.Service.GetProductRequest; 7 | import dev.techdozo.product.api.Service.GetProductResponse; 8 | import io.grpc.ManagedChannelBuilder; 9 | import io.grpc.stub.StreamObserver; 10 | import lombok.SneakyThrows; 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | import java.util.Random; 14 | 15 | @Slf4j 16 | public class UnaryGrpcAsynClient { 17 | 18 | private final String host; 19 | private final int port; 20 | 21 | public UnaryGrpcAsynClient(String host, int port) { 22 | this.host = host; 23 | this.port = port; 24 | } 25 | 26 | @SneakyThrows 27 | public void callServer() { 28 | 29 | log.info("Calling Server.."); 30 | var managedChannel = ManagedChannelBuilder.forAddress(host, port) 31 | .intercept(new GrpcClientRequestInterceptor()).usePlaintext().build(); 32 | 33 | 34 | // Create a new async stub 35 | var productServiceAsyncStub = ProductServiceGrpc.newStub(managedChannel); 36 | 37 | var productRequest = GetProductRequest.newBuilder().setProductId("apple-123").build(); 38 | 39 | productServiceAsyncStub.getProduct(productRequest, new ProductCallback()); 40 | 41 | Thread.sleep(3000); 42 | 43 | log.info("Finished call"); 44 | } 45 | 46 | private static class ProductCallback implements StreamObserver { 47 | 48 | @Override 49 | public void onNext(GetProductResponse value) { 50 | log.info("Received product, {}", value); 51 | } 52 | 53 | @Override 54 | public void onError(Throwable cause) { 55 | log.error("Error occurred, cause {}", cause.getMessage()); 56 | } 57 | 58 | @Override 59 | public void onCompleted() { 60 | log.info("Stream completed"); 61 | } 62 | } 63 | 64 | public static void main(String[] args) { 65 | //Pseudo code to generate JWT token 66 | var token = "Bearer " + new Random().nextInt(); 67 | UserContext.setUserContext(token); 68 | var client = new UnaryGrpcAsynClient("0.0.0.0", 3000); 69 | client.callServer(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-server/src/main/java/dev/techdozo/product/interceptor/GrpcMDCInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.interceptor; 2 | 3 | import io.grpc.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.slf4j.MDC; 6 | 7 | /** 8 | * gRPC server interceptor with ThreadLocal 9 | */ 10 | @Slf4j 11 | public class GrpcMDCInterceptor implements ServerInterceptor { 12 | 13 | private static final String TRACE_ID = "traceId"; 14 | 15 | @Override 16 | public ServerCall.Listener interceptCall( 17 | ServerCall serverCall, Metadata metadata, ServerCallHandler next) { 18 | 19 | log.info("Setting user context, metadata {}", metadata); 20 | 21 | var traceId = metadata.get(Metadata.Key.of("traceId", Metadata.ASCII_STRING_MARSHALLER)); 22 | 23 | MDC.put(TRACE_ID, traceId); 24 | 25 | try { 26 | return new WrappingListener<>(next.startCall(serverCall, metadata), traceId); 27 | } finally { 28 | MDC.clear(); 29 | } 30 | } 31 | 32 | private static class WrappingListener 33 | extends ForwardingServerCallListener.SimpleForwardingServerCallListener { 34 | private final String traceId; 35 | 36 | public WrappingListener(ServerCall.Listener delegate, String traceId) { 37 | super(delegate); 38 | this.traceId = traceId; 39 | } 40 | 41 | @Override 42 | public void onMessage(R message) { 43 | MDC.put(TRACE_ID, traceId); 44 | try { 45 | super.onMessage(message); 46 | } finally { 47 | MDC.clear(); 48 | } 49 | } 50 | 51 | @Override 52 | public void onHalfClose() { 53 | MDC.put(TRACE_ID, traceId); 54 | try { 55 | super.onHalfClose(); 56 | } finally { 57 | MDC.clear(); 58 | } 59 | } 60 | 61 | @Override 62 | public void onCancel() { 63 | MDC.put(TRACE_ID, traceId); 64 | try { 65 | super.onCancel(); 66 | } finally { 67 | MDC.clear(); 68 | } 69 | } 70 | 71 | @Override 72 | public void onComplete() { 73 | MDC.put(TRACE_ID, traceId); 74 | try { 75 | super.onComplete(); 76 | } finally { 77 | MDC.clear(); 78 | } 79 | } 80 | 81 | @Override 82 | public void onReady() { 83 | MDC.put(TRACE_ID, traceId); 84 | try { 85 | super.onReady(); 86 | } finally { 87 | MDC.clear(); 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/grpc-server/src/main/java/dev/techdozo/product/api/ProductService.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api; 2 | 3 | import com.google.rpc.Code; 4 | import dev.techdozo.product.api.Service.GetProductResponse; 5 | import dev.techdozo.product.appliction.Product; 6 | import dev.techdozo.product.appliction.repository.ProductRepository; 7 | import io.grpc.stub.StreamObserver; 8 | import lombok.extern.slf4j.Slf4j; 9 | 10 | import java.util.Optional; 11 | 12 | @Slf4j 13 | public class ProductService extends ProductServiceGrpc.ProductServiceImplBase { 14 | 15 | private final ProductRepository productRepository; 16 | 17 | public ProductService() { 18 | this.productRepository = new ProductRepository(); 19 | } 20 | 21 | @Override 22 | public void listProduct( 23 | Service.ListProductRequest request, StreamObserver responseObserver) { 24 | 25 | log.info("Fetching products.."); 26 | 27 | var productIds = request.getProductIdList(); 28 | 29 | for (var productId : productIds) { 30 | log.info("Sending detail of product id {}", productId); 31 | // Fetch Product information from repository 32 | Optional optionalProduct = productRepository.get(productId); 33 | // If found send stream response else send error code 34 | 35 | Service.GetProductResponse getProductResponse; 36 | if (optionalProduct.isPresent()) { 37 | var product = optionalProduct.get(); 38 | // If found build response 39 | var productResponse = 40 | Service.Product.newBuilder() 41 | .setName(product.getName()) 42 | .setDescription(product.getDescription()) 43 | .setPrice(product.getPrice()) 44 | .build(); 45 | getProductResponse = GetProductResponse.newBuilder().setProduct(productResponse).build(); 46 | log.info("Called onNext for id {}", productId); 47 | } else { 48 | com.google.rpc.Status status = 49 | com.google.rpc.Status.newBuilder() 50 | .setCode(Code.NOT_FOUND.getNumber()) 51 | .setMessage("Product id not found") 52 | .build(); 53 | getProductResponse = GetProductResponse.newBuilder().setError(status).build(); 54 | } 55 | responseObserver.onNext(getProductResponse); 56 | } 57 | // Indicates that stream is done. 58 | responseObserver.onCompleted(); 59 | log.info("Finished calling Product API service.."); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-api-gateway/src/main/java/dev/techdozo/api/product/application/service/impl/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.api.product.application.service.impl; 2 | 3 | import dev.techdozo.api.product.application.error.ServiceExceptionMapper; 4 | import dev.techdozo.api.product.application.mapper.ProductMapper; 5 | import dev.techdozo.api.product.application.model.Product; 6 | import dev.techdozo.api.product.application.service.ProductService; 7 | import dev.techdozo.product.Resources.CreateProductRequest; 8 | import dev.techdozo.product.Resources.GetProductRequest; 9 | import dev.techdozo.product.resource.ProductServiceGrpc; 10 | import io.grpc.ManagedChannel; 11 | import io.grpc.StatusRuntimeException; 12 | import lombok.extern.slf4j.Slf4j; 13 | 14 | @Slf4j 15 | public class ProductServiceImpl implements ProductService { 16 | 17 | private ManagedChannel managedChannel; 18 | 19 | public ProductServiceImpl(ManagedChannel managedChannel) { 20 | this.managedChannel = managedChannel; 21 | } 22 | 23 | @Override 24 | public String createNewProduct(Product product) { 25 | log.info("Calling Server.."); 26 | 27 | var createProductRequest = 28 | CreateProductRequest.newBuilder() 29 | .setPrice(product.getPrice()) 30 | .setName(product.getName()) 31 | .setDescription(product.getDescription()) 32 | .setUserId(product.getUserId()) 33 | .build(); 34 | 35 | var productApiServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 36 | 37 | var response = productApiServiceBlockingStub.createProduct(createProductRequest); 38 | 39 | var productId = response.getProductId(); 40 | 41 | log.info("Received Product information from product service, info {}", product); 42 | 43 | return productId; 44 | } 45 | 46 | @Override 47 | public Product getProduct(String productId) { 48 | log.info("Calling Server.."); 49 | Product product; 50 | try { 51 | var request = GetProductRequest.newBuilder().setProductId(productId).build(); 52 | var productApiServiceBlockingStub = ProductServiceGrpc.newBlockingStub(managedChannel); 53 | var response = productApiServiceBlockingStub.getProduct(request); 54 | // Map to domain object 55 | product = ProductMapper.MAPPER.map(response); 56 | log.info("Received Product information from product service, info {}", product); 57 | } catch (StatusRuntimeException error) { 58 | log.info("Error while calling product service, reason {} ", error.getMessage()); 59 | throw ServiceExceptionMapper.map(error); 60 | } 61 | return product; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /grpc-spring-boot/product-service/src/main/java/dev/techdozo/product/api/interceptor/GlobalExceptionHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.product.api.interceptor; 2 | 3 | import com.google.protobuf.Any; 4 | import com.google.rpc.Code; 5 | import com.google.rpc.ErrorInfo; 6 | import dev.techdozo.commons.error.ResourceNotFoundException; 7 | import io.grpc.*; 8 | import io.grpc.protobuf.StatusProto; 9 | 10 | /** 11 | * To run this class uncomment bean definition in AppConfig @GrpcGlobalServerInterceptor public 12 | * GlobalExceptionHandlerInterceptor errorHandler() { return new 13 | * GlobalExceptionHandlerInterceptor(); } 14 | */ 15 | public class GlobalExceptionHandlerInterceptor implements ServerInterceptor { 16 | 17 | @Override 18 | public ServerCall.Listener interceptCall( 19 | ServerCall serverCall, Metadata headers, ServerCallHandler serverCallHandler) { 20 | ServerCall.Listener delegate = serverCallHandler.startCall(serverCall, headers); 21 | return new ExceptionHandler<>(delegate, serverCall, headers); 22 | } 23 | 24 | private static class ExceptionHandler 25 | extends ForwardingServerCallListener.SimpleForwardingServerCallListener { 26 | 27 | private final ServerCall delegate; 28 | private final Metadata headers; 29 | 30 | ExceptionHandler( 31 | ServerCall.Listener listener, ServerCall serverCall, Metadata headers) { 32 | super(listener); 33 | this.delegate = serverCall; 34 | this.headers = headers; 35 | } 36 | 37 | @Override 38 | public void onHalfClose() { 39 | try { 40 | super.onHalfClose(); 41 | } catch (RuntimeException ex) { 42 | handleException(ex, delegate, headers); 43 | throw ex; 44 | } 45 | } 46 | 47 | private void handleException( 48 | RuntimeException exception, ServerCall serverCall, Metadata headers) { 49 | // Catch specific Exception and Process 50 | if (exception instanceof ResourceNotFoundException) { 51 | var errorMetaData = ((ResourceNotFoundException) exception).getErrorMetaData(); 52 | // Build google.rpc.ErrorInfo 53 | var errorInfo = 54 | ErrorInfo.newBuilder() 55 | .setReason("Resource not found") 56 | .setDomain("Product") 57 | .putAllMetadata(errorMetaData) 58 | .build(); 59 | 60 | var rpcStatus = 61 | com.google.rpc.Status.newBuilder() 62 | .setCode(Code.NOT_FOUND.getNumber()) 63 | .setMessage("Product id not found") 64 | .addDetails(Any.pack(errorInfo)) 65 | .build(); 66 | 67 | var statusRuntimeException = StatusProto.toStatusRuntimeException(rpcStatus); 68 | 69 | var newStatus = Status.fromThrowable(statusRuntimeException); 70 | // Get metadata from statusRuntimeException 71 | Metadata newHeaders = statusRuntimeException.getTrailers(); 72 | 73 | serverCall.close(newStatus, newHeaders); 74 | } else { 75 | serverCall.close(Status.UNKNOWN, headers); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /grpc-unary-rpc/grpc-client/src/main/java/dev/techdozo/order/client/UnaryGrpcFutureClient.java: -------------------------------------------------------------------------------- 1 | package dev.techdozo.order.client; 2 | 3 | import com.google.common.util.concurrent.FutureCallback; 4 | import com.google.common.util.concurrent.Futures; 5 | import com.google.common.util.concurrent.ListenableFuture; 6 | import dev.techdozo.order.client.interceptor.GrpcClientRequestInterceptor; 7 | import dev.techdozo.order.context.UserContext; 8 | import dev.techdozo.product.api.ProductServiceGrpc; 9 | import dev.techdozo.product.api.Service.GetProductRequest; 10 | import dev.techdozo.product.api.Service.GetProductResponse; 11 | import io.grpc.ManagedChannelBuilder; 12 | import lombok.SneakyThrows; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.checkerframework.checker.nullness.compatqual.NullableDecl; 15 | 16 | import java.util.Random; 17 | import java.util.concurrent.ExecutorService; 18 | import java.util.concurrent.Executors; 19 | import java.util.concurrent.TimeUnit; 20 | 21 | @Slf4j 22 | public class UnaryGrpcFutureClient { 23 | 24 | private final String host; 25 | private final int port; 26 | private final ExecutorService fixedThreadPool; 27 | 28 | public UnaryGrpcFutureClient(String host, int port) { 29 | this.host = host; 30 | this.port = port; 31 | this.fixedThreadPool = Executors.newFixedThreadPool(2); 32 | } 33 | 34 | @SneakyThrows 35 | public void callServer() { 36 | 37 | log.info("Calling Server.."); 38 | var managedChannel = 39 | ManagedChannelBuilder.forAddress(host, port) 40 | .intercept(new GrpcClientRequestInterceptor()) 41 | .usePlaintext() 42 | .build(); 43 | // Create a new future stub 44 | var productServiceFutureStub = ProductServiceGrpc.newFutureStub(managedChannel); 45 | 46 | var productRequest = GetProductRequest.newBuilder().setProductId("apple-123").build(); 47 | 48 | ListenableFuture listenableFuture = 49 | productServiceFutureStub.getProduct(productRequest); 50 | 51 | listenableFuture.addListener(this::notifyListener, fixedThreadPool); 52 | 53 | Futures.addCallback(listenableFuture, new ProductCallback(), fixedThreadPool); 54 | 55 | Thread.sleep(2000); 56 | 57 | fixedThreadPool.shutdown(); 58 | fixedThreadPool.awaitTermination(2, TimeUnit.SECONDS); 59 | } 60 | 61 | private void notifyListener() { 62 | log.info("Notifying downstream operation"); 63 | } 64 | 65 | private static class ProductCallback implements FutureCallback { 66 | 67 | @Override 68 | public void onSuccess(@NullableDecl GetProductResponse result) { 69 | log.info("Received product f1 {}", result); 70 | } 71 | 72 | @Override 73 | public void onFailure(Throwable error) { 74 | log.error("Error occurred, reason {}", error.getMessage()); 75 | } 76 | } 77 | 78 | public static void main(String[] args) { 79 | // Pseudo code to generate JWT token 80 | var token = "Bearer " + new Random().nextInt(); 81 | UserContext.setUserContext(token); 82 | var client = new UnaryGrpcFutureClient("0.0.0.0", 3000); 83 | client.callServer(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /grpc-example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /grpc-spring-boot/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /grpc-unary-rpc/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /grpc-example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /grpc-spring-boot/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /grpc-unary-rpc/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /grpc-server-streaming-rpc/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | --------------------------------------------------------------------------------