Skip to content

Commit

Permalink
Upgrade to 5.1.0 and switch to reactor API
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent.mathon committed Apr 11, 2023
1 parent 4ad56ea commit 9aa6b7f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<dependency>
<groupId>io.github.lognet</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>4.9.1</version>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.salesforce.servicelibs</groupId>
Expand Down Expand Up @@ -112,6 +112,13 @@
</dependency>
</dependencies>

<repositories>
<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>

<build>
<extensions>
<extension>
Expand All @@ -132,6 +139,15 @@
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${java.grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>reactor-grpc</id>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>reactor-grpc</artifactId>
<version>1.2.3</version>
<mainClass>com.salesforce.reactorgrpc.ReactorGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
public class GrpcExceptionAdvice {

@GRpcExceptionHandler
public Status handle(RuntimeException ex, GRpcExceptionScope scope) {
public Status handle(Throwable ex, GRpcExceptionScope scope) {
var status = Status.INTERNAL.withDescription(ex.getLocalizedMessage()).withCause(ex);
log.error("(GrpcExceptionAdvice) RuntimeException: ", ex);
log.error("(GrpcExceptionAdvice) Throwable: ", ex);
return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@
import com.acme.apidomains.interceptors.LogGrpcInterceptor;
import com.acme.apidomains.process_design.mappers.SkillMapper;
import com.acme.apidomains.process_design.services.SkillService;
import com.acme.domains.process_design.QueryServiceGrpc;
import com.acme.domains.process_design.ReactorQueryServiceGrpc;
import com.acme.domains.process_design.transport.ListSkills;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lognet.springboot.grpc.GRpcService;
import reactor.core.publisher.Mono;


@GRpcService(interceptors = {LogGrpcInterceptor.class})
@Slf4j
@RequiredArgsConstructor
public class ProcessDesignGrpcService extends QueryServiceGrpc.QueryServiceImplBase {
public class ProcessDesignGrpcService extends ReactorQueryServiceGrpc.QueryServiceImplBase {

private final SkillService skillService;

public void listSkills(ListSkills.ListSkillsRequest request, StreamObserver<ListSkills.ListSkillsResponse> responseObserver) {
skillService.findAllSkills().map(SkillMapper::toGrpc).collectList().map(SkillMapper::toGrpcResponse)
// These log shows the next step
.log()
// GRpcExceptionHandlerInterceptor do not intercept exceptions correctly, I have to do this to obtain the correct behavior which is not handy.
//.onErrorMap(ex -> Status.INTERNAL.withDescription(ex.getLocalizedMessage()).withCause(ex).asRuntimeException())
.subscribe(responseObserver::onNext, responseObserver::onError, responseObserver::onCompleted);
@Override
public Mono<ListSkills.ListSkillsResponse> listSkills(Mono<ListSkills.ListSkillsRequest> request) {
return request.flatMap(req -> skillService.findAllSkills().map(SkillMapper::toGrpc).collectList().map(SkillMapper::toGrpcResponse)
.doOnSuccess(response -> log.info("Result: {}", response)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class SkillServiceImpl implements SkillService {
@Override
@Transactional(readOnly = true)// removing this annotation and then transaction handling is a workaround, but we cannot live with such a limitation
public Flux<Skill> findAllSkills() {
if(true) throw new IllegalStateException("test");// if(true) return Flux.error(new IllegalStateException("test")) gives the exact same outcome
if(true) throw new RuntimeException("test");//
//if(true) return Flux.error(new RuntimeException("test")); // gives the exact same outcome
return skillRepository.findAll(Sort.by("name"));
}
}

0 comments on commit 9aa6b7f

Please sign in to comment.