Package com.linecorp.armeria.client
Interface ClientFactory
- All Superinterfaces:
AsyncCloseable,AutoCloseable,ListenableAsyncCloseable,Unwrappable
- All Known Implementing Classes:
DecoratingClientFactory
Creates and manages clients.
Life cycle of the default
Life cycle of the default ClientFactory
Clients or ClientBuilder uses the default ClientFactory returned by
ofDefault(), unless you specified a ClientFactory explicitly. Calling AsyncCloseable.close()
on the default ClientFactory will neither terminate its I/O threads nor release other related
resources unlike other ClientFactory to protect itself from accidental premature termination.
Instead, when the current ClassLoader is the system
class loader, a shutdown hook is registered so that they are
released when the JVM exits.
If you are in a multi-classloader environment or you desire an early/explicit termination of the default
ClientFactory, use closeDefault().
-
Method Summary
Modifier and TypeMethodDescriptionacquireEventLoop(SessionProtocol sessionProtocol, EndpointGroup endpointGroup, @Nullable Endpoint endpoint)static ClientFactoryBuilderbuilder()Returns a newly createdClientFactoryBuilder.default <T> @Nullable ClientBuilderParamsclientBuilderParams(T client)Returns theClientBuilderParamsheld inclient.static voidCloses the defaultClientFactory.static voidDisables the shutdown hook which closes the defaultClientFactory.Returns theEventLoopGroupbeing used by thisClientFactory.static ClientFactoryinsecure()Returns the insecure defaultClientFactoryimplementation which does not verify server's TLS certificate chain.default booleanisClientTypeSupported(Class<?> clientType)Verifies that client typeClassis supported by thisClientFactory.Returns theMeterRegistrythat collects various stats.newClient(ClientBuilderParams params)Creates a new client with the specifiedClientBuilderParams.intReturns the number of open connections managed by thisClientFactory.static ClientFactoryReturns the defaultClientFactoryimplementation.options()Returns theClientFactoryOptionsthat has been used to create thisClientFactory.voidsetMeterRegistry(MeterRegistry meterRegistry)Sets theMeterRegistrythat collects various stats.Returns theSchemes supported by thisClientFactory.default ClientFactoryunwrap()Unwraps this object and returns the object being decorated.default <T> TUnwraps the specifiedclientobject into the object of the specifiedtype.default ClientBuilderParamsvalidateParams(ClientBuilderParams params)default SchemevalidateScheme(Scheme scheme)Makes sure the specifiedSchemeis supported by thisClientFactory.default URIvalidateUri(URI uri)Makes sure the specifiedURIis supported by thisClientFactory.Methods inherited from interface com.linecorp.armeria.common.util.AsyncCloseable
close, closeAsyncMethods inherited from interface com.linecorp.armeria.common.util.ListenableAsyncCloseable
isClosed, isClosing, whenClosedMethods inherited from interface com.linecorp.armeria.common.util.Unwrappable
as
-
Method Details
-
ofDefault
Returns the defaultClientFactoryimplementation. -
insecure
Returns the insecure defaultClientFactoryimplementation which does not verify server's TLS certificate chain. -
builder
Returns a newly createdClientFactoryBuilder. -
closeDefault
static void closeDefault()Closes the defaultClientFactory. -
disableShutdownHook
static void disableShutdownHook()Disables the shutdown hook which closes the defaultClientFactory. This method is useful when you need full control over the life cycle of the defaultClientFactory. -
supportedSchemes
Returns theSchemes supported by thisClientFactory. -
isClientTypeSupported
Verifies that client typeClassis supported by thisClientFactory. Can be used to support multipleClientFactorys for a singleScheme. -
eventLoopGroup
EventLoopGroup eventLoopGroup()Returns theEventLoopGroupbeing used by thisClientFactory. Can be used to, e.g., schedule a periodic task without creating a separate event loop. UseeventLoopSupplier()instead if what you need is anEventLooprather than anEventLoopGroup. -
eventLoopSupplier
-
acquireEventLoop
ReleasableHolder<EventLoop> acquireEventLoop(SessionProtocol sessionProtocol, EndpointGroup endpointGroup, @Nullable @Nullable Endpoint endpoint)Acquires anEventLoopthat is expected to handle a connection to the specifiedEndpoint. The caller must release the returnedEventLoopback by callingReleasableHolder.release()so thatClientFactoryutilizesEventLoops efficiently.- Parameters:
sessionProtocol- theSessionProtocolof the connectionendpointGroup- theEndpointGroupwhereendpointbelongs to.endpoint- theEndpointwhere a request is being sent.nullif theEndpointis not known yet.
-
meterRegistry
MeterRegistry meterRegistry()Returns theMeterRegistrythat collects various stats. -
setMeterRegistry
Sets theMeterRegistrythat collects various stats. Note that this method is intended to be used during the initialization phase of an application, so that the application gets a chance to switch to the preferredMeterRegistryimplementation. Invoking this method after this factory started to export stats to the oldMeterRegistrymay result in undocumented behavior. -
options
ClientFactoryOptions options()Returns theClientFactoryOptionsthat has been used to create thisClientFactory. -
newClient
Creates a new client with the specifiedClientBuilderParams. The client instance returned by this method must be an instance ofClientBuilderParams.clientType(). -
numConnections
int numConnections()Returns the number of open connections managed by thisClientFactory. -
clientBuilderParams
Returns theClientBuilderParamsheld inclient. This is used when creating a new derivedClientwhich inheritsClientBuilderParamsfromclient. If thisClientFactorydoes not know how to handle theClientBuilderParamsfor the providedclient, it should returnnull. -
unwrap
Unwraps the specifiedclientobject into the object of the specifiedtype. For example,ClientFactory clientFactory = ...; WebClient client = WebClient.builder(...) .factory(clientFactory) .decorator(LoggingClient.newDecorator()) .build(); LoggingClient unwrapped = clientFactory.unwrap(client, LoggingClient.class); // If the client implements Unwrappable, you can just use the 'as()' method. LoggingClient unwrapped2 = client.as(LoggingClient.class);- Parameters:
client- the client objecttype- the type of the object to return- Returns:
- the object of the specified
typeif found, ornullif not found. - See Also:
Client.as(Class),Clients.unwrap(Object, Class),Unwrappable
-
unwrap
Description copied from interface:UnwrappableUnwraps this object and returns the object being decorated. If thisUnwrappableis the innermost object, this method returns itself. For example:class Foo implements Unwrappable {} class Bar<T extends Unwrappable> extends AbstractUnwrappable<T> { Bar(T delegate) { super(delegate); } } class Qux<T extends Unwrappable> extends AbstractUnwrappable<T> { Qux(T delegate) { super(delegate); } } Foo foo = new Foo(); assert foo.unwrap() == foo; Bar<Foo> bar = new Bar<>(foo); assert bar.unwrap() == foo; Qux<Bar<Foo>> qux = new Qux<>(bar); assert qux.unwrap() == bar; assert qux.unwrap().unwrap() == foo;- Specified by:
unwrapin interfaceUnwrappable
-
validateUri
Makes sure the specifiedURIis supported by thisClientFactory.- Parameters:
uri- theURIof the server endpoint- Returns:
- the validated and normalized
URIwhich always has a non-empty path. - Throws:
IllegalArgumentException- if the scheme of the specifiedURIis not supported by thisClientFactory
-
validateScheme
Makes sure the specifiedSchemeis supported by thisClientFactory.- Parameters:
scheme- theSchemeof the server endpoint- Returns:
- the specified
Scheme - Throws:
IllegalArgumentException- if theSchemeis not supported by thisClientFactory
-
validateParams
- Returns:
- the specified
ClientBuilderParams
-