Skip to content

Commit c5b69ed

Browse files
author
James Smith
committed
added example and created new annotation for instantiating server interceptors as beans rather than directly via GRpcServerRunner
1 parent 963fe9a commit c5b69ed

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.lognet.springboot.grpc.demo;
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+
import org.lognet.springboot.grpc.GRpcInterceptor;
9+
10+
/**
11+
* Created by jamessmith on 9/7/16.
12+
*/
13+
@Slf4j
14+
@GRpcInterceptor
15+
public class DemoInterceptor implements ServerInterceptor {
16+
17+
@Override
18+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
19+
ServerCallHandler<ReqT, RespT> next) {
20+
log.info("Demo interceptor invoked!");
21+
return next.startCall(call, headers);
22+
}
23+
}

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/GreeterApp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@SpringBootApplication
1818
public class GreeterApp {
1919

20-
@GRpcService
20+
@GRpcService(interceptors = { DemoInterceptor.class })
2121
public static class GreeterService extends GreeterGrpc.GreeterImplBase {
2222
@Override
2323
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
@@ -32,4 +32,3 @@ public static void main(String[] args) {
3232
}
3333

3434
}
35-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.springframework.stereotype.Service;
10+
11+
/**
12+
* Created by jamessmith on 9/7/16.
13+
*/
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
@Service
18+
public @interface GRpcInterceptor {
19+
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcServerRunner.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ private List<ServerInterceptor> buildInterceptors(Class<? extends ServerIntercep
7575
List<ServerInterceptor> interceptors = new ArrayList<>();
7676

7777
for(Class<? extends ServerInterceptor> clazz : classes) {
78-
try {
79-
interceptors.add(clazz.newInstance());
80-
} catch (InstantiationException | IllegalAccessException e) {
81-
log.error("Unable to instantiate ServerInterceptor from class {}", clazz, e);
82-
}
78+
interceptors.add(applicationContext.getBean(clazz));
8379
}
8480

8581
return interceptors;

0 commit comments

Comments
 (0)