Class AbstractChannelFactory<T extends ManagedChannelBuilder<T>>
java.lang.Object
net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory<T>
- Type Parameters:
T
- The type of builder used by this channel factory.
- All Implemented Interfaces:
AutoCloseable
,GrpcChannelFactory
- Direct Known Subclasses:
InProcessChannelFactory
,NettyChannelFactory
,ShadedNettyChannelFactory
public abstract class AbstractChannelFactory<T extends ManagedChannelBuilder<T>>
extends Object
implements GrpcChannelFactory
This abstract channel factory contains some shared code for other
GrpcChannelFactory
s. This class utilizes
connection pooling and thus needs to be closed
after usage.- Since:
- 5/17/16
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<GrpcChannelConfigurer>
protected final GlobalClientInterceptorRegistry
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractChannelFactory
(GrpcChannelsProperties properties, GlobalClientInterceptorRegistry globalClientInterceptorRegistry, List<GrpcChannelConfigurer> channelConfigurers) Creates a new AbstractChannelFactory with eager initialized references. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this channel factory and the channels created by this instance.protected void
Configures the given channel builder.protected void
configureCompression
(T builder, String name) Configures the compression options that should be used by the channel.protected void
configureKeepAlive
(T builder, String name) Configures the keep alive options that should be used by the channel.protected void
configureLimits
(T builder, String name) Configures limits such as max message sizes that should be used by the channel.protected void
configureSecurity
(T builder, String name) Configures the security options that should be used by the channel.protected void
configureUserAgent
(T builder, String name) Configures custom User-Agent for the channel.final Channel
createChannel
(String name) Creates a new channel for the given service name.createChannel
(String name, List<ClientInterceptor> customInterceptors, boolean sortInterceptors) Creates a new channel for the given service name.Gets an unmodifiable map that contains the names of the created channel with their currentConnectivityState
.protected final String
Gets the default scheme that should be used for a client channel's target if no address is specified for a client's channel properties.protected final GrpcChannelProperties
getPropertiesFor
(String name) Gets the channel properties for the given client name.protected boolean
isNonNullAndNonBlank
(String value) Checks whether the given value is non null and non blank.protected abstract T
newChannelBuilder
(String name) Creates a newManagedChannelBuilder
for the given client name.protected ManagedChannel
newManagedChannel
(String name) Creates a newManagedChannel
for the given client name.protected void
watchConnectivityState
(String name, ManagedChannel channel) Watch the given channel for connectivity changes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory
createChannel
-
Field Details
-
globalClientInterceptorRegistry
-
channelConfigurers
-
-
Constructor Details
-
AbstractChannelFactory
protected AbstractChannelFactory(GrpcChannelsProperties properties, GlobalClientInterceptorRegistry globalClientInterceptorRegistry, List<GrpcChannelConfigurer> channelConfigurers) Creates a new AbstractChannelFactory with eager initialized references.- Parameters:
properties
- The properties for the channels to create.globalClientInterceptorRegistry
- The interceptor registry to use.channelConfigurers
- The channel configurers to use. Can be empty.
-
-
Method Details
-
createChannel
Description copied from interface:GrpcChannelFactory
Creates a new channel for the given service name. The returned channel will use all globally registeredClientInterceptor
s.Note: The underlying implementation might reuse existing
ManagedChannel
s allow connection reuse.- Specified by:
createChannel
in interfaceGrpcChannelFactory
- Parameters:
name
- The name of the service.- Returns:
- The newly created channel for the given service.
-
createChannel
public Channel createChannel(String name, List<ClientInterceptor> customInterceptors, boolean sortInterceptors) Description copied from interface:GrpcChannelFactory
Creates a new channel for the given service name. The returned channel will use all globally registeredClientInterceptor
s.Note: The underlying implementation might reuse existing
ManagedChannel
s allow connection reuse.Note: The given interceptors will be appended to the global interceptors and applied using
ClientInterceptors.interceptForward(Channel, ClientInterceptor...)
.- Specified by:
createChannel
in interfaceGrpcChannelFactory
- Parameters:
name
- The name of the service.customInterceptors
- A list of additional client interceptors that should be added to the channel.sortInterceptors
- Whether the interceptors (both global and custom) should be sorted before being applied.- Returns:
- The newly created channel for the given service.
-
newChannelBuilder
Creates a newManagedChannelBuilder
for the given client name.- Parameters:
name
- The name to create the channel builder for.- Returns:
- The newly created channel builder.
-
newManagedChannel
Creates a newManagedChannel
for the given client name. The name will be used to determine the properties for the new channel. The calling method is responsible for lifecycle management of the created channel. ManagedChannels should be reused if possible to allow connection reuse.- Parameters:
name
- The name to create the channel for.- Returns:
- The newly created channel.
- See Also:
-
getPropertiesFor
Gets the channel properties for the given client name.- Parameters:
name
- The client name to use.- Returns:
- The properties for the given client name.
-
getDefaultScheme
Gets the default scheme that should be used for a client channel's target if no address is specified for a client's channel properties.- Returns:
- The default scheme defined in
GrpcChannelsProperties
.
-
configure
Configures the given channel builder. This method can be overwritten to add features that are not yet supported by this library.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
configureKeepAlive
Configures the keep alive options that should be used by the channel.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
configureSecurity
Configures the security options that should be used by the channel.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
isNonNullAndNonBlank
Checks whether the given value is non null and non blank.- Parameters:
value
- The value to check.- Returns:
- True, if the given value was neither null nor blank. False otherwise.
-
configureLimits
Configures limits such as max message sizes that should be used by the channel.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
configureCompression
Configures the compression options that should be used by the channel.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
configureUserAgent
Configures custom User-Agent for the channel.- Parameters:
builder
- The channel builder to configure.name
- The name of the client to configure.
-
getConnectivityState
Description copied from interface:GrpcChannelFactory
Gets an unmodifiable map that contains the names of the created channel with their currentConnectivityState
. This method will return an empty map, if the feature is not supported.- Specified by:
getConnectivityState
in interfaceGrpcChannelFactory
- Returns:
- A map with the channel names and their connectivity state.
-
watchConnectivityState
Watch the given channel for connectivity changes.- Parameters:
name
- The name of the channel in the state overview.channel
- The channel to watch the state of.
-
close
@PreDestroy public void close()Closes this channel factory and the channels created by this instance. The shutdown happens in two phases, first an orderly shutdown is initiated on all channels and then the method waits for all channels to terminate. If the channels don't have terminated after 60 seconds then they will be forcefully shutdown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceGrpcChannelFactory
-