Class GrpcModule
- java.lang.Object
-
- pl.morgwai.base.grpc.scopes.GrpcModule
-
- All Implemented Interfaces:
Module
public class GrpcModule extends Object implements Module
gRPC GuiceScope
s,ContextTracker
s,Interceptor
s and some helper methods.Usually a single app-wide instance is created at the app startup.
In case of servers, gRPCservices
should beintercepted
withserverInterceptor
.
In case of clients, gRPCChannel
s should beintercepted
with eitherclientInterceptor
ornestingClientInterceptor
.
-
-
Field Summary
Fields Modifier and Type Field Description List<ContextTracker<?>>
allTrackers
Singleton oflistenerEventContextTracker
.static Key<List<ContextTracker<?>>>
allTrackersKey
Key
ofallTrackers
.ClientInterceptor
clientInterceptor
ContextBinder
contextBinder
ContextBinder
created withallTrackers
.ContextTracker<ListenerEventContext>
listenerEventContextTracker
Allows tracking ofContexts of a Listener events
.static Key<ContextTracker<ListenerEventContext>>
listenerEventContextTrackerKey
Key
oflistenerEventContextTracker
.Scope
listenerEventScope
Scopes objects to theContext of a Listener event
(eitherserver
orclient
) and as a consequence also to the context of the corresponding user inboundStreamObserver
call.ClientInterceptor
nestingClientInterceptor
Scope
rpcScope
ServerInterceptor
serverInterceptor
-
Constructor Summary
Constructors Constructor Description GrpcModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitTerminationOfAllExecutors()
Awaits for termination
of all executors obtained from this module.List<GrpcContextTrackingExecutor>
awaitTerminationOfAllExecutors(long timeout, TimeUnit unit)
Awaits for termination
of all executors obtained from this module.void
configure(Binder binder)
Creates infrastructure bindings.List<GrpcContextTrackingExecutor>
enforceTerminationOfAllExecutors(long timeout, TimeUnit unit)
Enforces termination
of all executors obtained from this module.List<TrackableContext<?>>
getActiveContexts()
List<GrpcContextTrackingExecutor>
getExecutors()
List of allExecutors
created by thisModule
.GrpcContextTrackingExecutor
newContextTrackingExecutor(String name, int poolSize)
Constructs a fixed size, context tracking executor that uses an unboundLinkedBlockingQueue
and a newNamingThreadFactory
named after this executor.GrpcContextTrackingExecutor
newContextTrackingExecutor(String name, int poolSize, int queueSize)
Constructs a fixed size, context tracking executor that uses aLinkedBlockingQueue
of sizequeueSize
, the defaultRejectedExecutionHandler
and a newNamingThreadFactory
named after this executor.GrpcContextTrackingExecutor
newContextTrackingExecutor(String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
Constructs a context tracking executor.GrpcContextTrackingExecutor
newContextTrackingExecutor(String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Constructs a context tracking executor.void
shutdownAllExecutors()
Shutdowns all executors obtained from this module.Awaitable.WithUnit
toAwaitableOfEnforcedTerminationOfAllExecutors()
Awaitable.WithUnit
toAwaitableOfTerminationOfAllExecutors()
-
-
-
Field Detail
-
listenerEventContextTracker
public final ContextTracker<ListenerEventContext> listenerEventContextTracker
Allows tracking ofContexts of a Listener events
.- See Also:
listenerEventScope
-
listenerEventScope
public final Scope listenerEventScope
Scopes objects to theContext of a Listener event
(eitherserver
orclient
) and as a consequence also to the context of the corresponding user inboundStreamObserver
call.
-
rpcScope
public final Scope rpcScope
-
allTrackers
public final List<ContextTracker<?>> allTrackers
Singleton oflistenerEventContextTracker
. TypeList<ContextTracker<?>>
is bound to it inconfigure(Binder)
method.
-
contextBinder
public final ContextBinder contextBinder
ContextBinder
created withallTrackers
.
-
serverInterceptor
public final ServerInterceptor serverInterceptor
-
nestingClientInterceptor
public final ClientInterceptor nestingClientInterceptor
Allclient Channels
must beintercepted
either by thisInterceptor
or byclientInterceptor
. If a client call was made within aContext
of some enclosing "parent" call (server call or a previous chained client call), thisInterceptor
will join togetherContext of such call
with its parentRpcContext
, so that they will share allRPC-scoped
objects.
-
clientInterceptor
public final ClientInterceptor clientInterceptor
Allclient Channels
must beintercepted
either by thisInterceptor
or bynestingClientInterceptor
. ThisInterceptor
will keepClientRpcContext
s separate even if they were made within some enclosingRpcContext
s.
-
listenerEventContextTrackerKey
public static final Key<ContextTracker<ListenerEventContext>> listenerEventContextTrackerKey
Key
oflistenerEventContextTracker
.
-
allTrackersKey
public static final Key<List<ContextTracker<?>>> allTrackersKey
Key
ofallTrackers
.
-
-
Method Detail
-
getActiveContexts
public List<TrackableContext<?>> getActiveContexts()
-
configure
public void configure(Binder binder)
Creates infrastructure bindings. Specifically binds the following:List<ContextTracker<?>>
toallTrackers
ContextBinder
tocontextBinder
- Their respective types to
listenerEventContextTracker
and both contexts
-
getExecutors
public List<GrpcContextTrackingExecutor> getExecutors()
List of allExecutors
created by thisModule
.
-
newContextTrackingExecutor
public GrpcContextTrackingExecutor newContextTrackingExecutor(String name, int poolSize)
Constructs a fixed size, context tracking executor that uses an unboundLinkedBlockingQueue
and a newNamingThreadFactory
named after this executor.To avoid
OutOfMemoryError
s, an external mechanism that limits maximum number of tasks (such as a load balancer or a frontend proxy) should be used.
-
newContextTrackingExecutor
public GrpcContextTrackingExecutor newContextTrackingExecutor(String name, int poolSize, int queueSize)
Constructs a fixed size, context tracking executor that uses aLinkedBlockingQueue
of sizequeueSize
, the defaultRejectedExecutionHandler
and a newNamingThreadFactory
named after this executor.The default
RejectedExecutionHandler
throws aRejectedExecutionException
if the queue is full or the executor is shutting down. It should usually be handled by sendingStatus.UNAVAILABLE
to the client.
-
newContextTrackingExecutor
public GrpcContextTrackingExecutor newContextTrackingExecutor(String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
Constructs a context tracking executor.
-
newContextTrackingExecutor
public GrpcContextTrackingExecutor newContextTrackingExecutor(String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Constructs a context tracking executor.rejectionHandler
will receive a task wrapped with aContextBoundRunnable
.In order for
GrpcContextTrackingExecutor.execute(StreamObserver, Runnable)
to work properly, therejectionHandler
must throw aRejectedExecutionException
.
-
shutdownAllExecutors
public void shutdownAllExecutors()
Shutdowns all executors obtained from this module.
-
enforceTerminationOfAllExecutors
public List<GrpcContextTrackingExecutor> enforceTerminationOfAllExecutors(long timeout, TimeUnit unit) throws InterruptedException
Enforces termination
of all executors obtained from this module.- Returns:
- an empty list if all executors were terminated, list of unterminated otherwise.
- Throws:
InterruptedException
-
awaitTerminationOfAllExecutors
public List<GrpcContextTrackingExecutor> awaitTerminationOfAllExecutors(long timeout, TimeUnit unit) throws InterruptedException
Awaits for termination
of all executors obtained from this module.- Returns:
- an empty list if all executors were terminated, list of unterminated otherwise.
- Throws:
InterruptedException
-
awaitTerminationOfAllExecutors
public void awaitTerminationOfAllExecutors() throws InterruptedException
Awaits for termination
of all executors obtained from this module.- Throws:
InterruptedException
-
toAwaitableOfEnforcedTerminationOfAllExecutors
public Awaitable.WithUnit toAwaitableOfEnforcedTerminationOfAllExecutors()
-
toAwaitableOfTerminationOfAllExecutors
public Awaitable.WithUnit toAwaitableOfTerminationOfAllExecutors()
-
-