Package com.linecorp.armeria.client
Interface ClientFactory
-
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
AbstractClientFactory
,DecoratingClientFactory
public interface ClientFactory extends AutoCloseable
Creates and manages clients.Life cycle of the default
ClientFactory
Clients
orClientBuilder
usesDEFAULT
, the defaultClientFactory
, unless you specified aClientFactory
explicitly. Callingclose()
on the defaultClientFactory
will neither terminate its I/O threads nor release other related resources unlike otherClientFactory
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
, usecloseDefault()
.
-
-
Field Summary
Fields Modifier and Type Field Description static ClientFactory
DEFAULT
The defaultClientFactory
implementation.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description ReleasableHolder<EventLoop>
acquireEventLoop(Endpoint endpoint, SessionProtocol sessionProtocol)
default <T> Optional<ClientBuilderParams>
clientBuilderParams(T client)
Returns theClientBuilderParams
held inclient
.void
close()
Closes all clients managed by this factory and shuts down theEventLoopGroup
created implicitly by this factory.static void
closeDefault()
Closes the defaultClientFactory
.static void
disableShutdownHook()
Disables the shutdown hook which closes the defaultClientFactory
.EventLoopGroup
eventLoopGroup()
Returns theEventLoopGroup
being used by thisClientFactory
.Supplier<EventLoop>
eventLoopSupplier()
MeterRegistry
meterRegistry()
Returns theMeterRegistry
that collects various stats.<T> T
newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptions options)
<T> T
newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
<T> T
newClient(Scheme scheme, Endpoint endpoint, String path, Class<T> clientType, ClientOptions options)
<T> T
newClient(Scheme scheme, Endpoint endpoint, String path, Class<T> clientType, ClientOptionValue<?>... options)
<T> T
newClient(String uri, Class<T> clientType, ClientOptions options)
Creates a new client that connects to the specifieduri
.<T> T
newClient(String uri, Class<T> clientType, ClientOptionValue<?>... options)
Creates a new client that connects to the specifieduri
.<T> T
newClient(URI uri, Class<T> clientType, ClientOptions options)
Creates a new client that connects to the specifiedURI
.<T> T
newClient(URI uri, Class<T> clientType, ClientOptionValue<?>... options)
Creates a new client that connects to the specifiedURI
.void
setMeterRegistry(MeterRegistry meterRegistry)
Sets theMeterRegistry
that collects various stats.Set<Scheme>
supportedSchemes()
Returns theScheme
s supported by thisClientFactory
.default <T> Optional<T>
unwrap(Object client, Class<T> type)
Unwraps the specifiedclient
object into the object of the specifiedtype
.
-
-
-
Field Detail
-
DEFAULT
static final ClientFactory DEFAULT
The defaultClientFactory
implementation.
-
-
Method Detail
-
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
Set<Scheme> supportedSchemes()
Returns theScheme
s supported by thisClientFactory
.
-
eventLoopGroup
EventLoopGroup eventLoopGroup()
Returns theEventLoopGroup
being 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 anEventLoop
rather than anEventLoopGroup
.
-
acquireEventLoop
ReleasableHolder<EventLoop> acquireEventLoop(Endpoint endpoint, SessionProtocol sessionProtocol)
Acquires anEventLoop
that is expected to handle a connection to the specifiedEndpoint
. The caller must release the returnedEventLoop
back by callingReleasableHolder.release()
so thatClientFactory
utilizesEventLoop
s efficiently.
-
meterRegistry
MeterRegistry meterRegistry()
Returns theMeterRegistry
that collects various stats.
-
setMeterRegistry
void setMeterRegistry(MeterRegistry meterRegistry)
Sets theMeterRegistry
that 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 preferredMeterRegistry
implementation. Invoking this method after this factory started to export stats to the oldMeterRegistry
may result in undocumented behavior.
-
newClient
<T> T newClient(String uri, Class<T> clientType, ClientOptionValue<?>... options)
Creates a new client that connects to the specifieduri
.- Parameters:
uri
- the URI of the server endpointclientType
- the type of the new clientoptions
- theClientOptionValue
s
-
newClient
<T> T newClient(String uri, Class<T> clientType, ClientOptions options)
Creates a new client that connects to the specifieduri
.- Parameters:
uri
- the URI of the server endpointclientType
- the type of the new clientoptions
- theClientOptions
-
newClient
<T> T newClient(URI uri, Class<T> clientType, ClientOptionValue<?>... options)
Creates a new client that connects to the specifiedURI
.- Parameters:
uri
- the URI of the server endpointclientType
- the type of the new clientoptions
- theClientOptionValue
s
-
newClient
<T> T newClient(URI uri, Class<T> clientType, ClientOptions options)
Creates a new client that connects to the specifiedURI
.- Parameters:
uri
- the URI of the server endpointclientType
- the type of the new clientoptions
- theClientOptions
-
newClient
<T> T newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
- Parameters:
scheme
- theScheme
for theendpoint
endpoint
- the serverEndpoint
clientType
- the type of the new clientoptions
- theClientOptionValue
s
-
newClient
<T> T newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptions options)
- Parameters:
scheme
- theScheme
for theendpoint
endpoint
- the serverEndpoint
clientType
- the type of the new clientoptions
- theClientOptions
-
newClient
<T> T newClient(Scheme scheme, Endpoint endpoint, @Nullable String path, Class<T> clientType, ClientOptionValue<?>... options)
- Parameters:
scheme
- theScheme
for theendpoint
endpoint
- the serverEndpoint
path
- the servicepath
clientType
- the type of the new clientoptions
- theClientOptionValue
s
-
newClient
<T> T newClient(Scheme scheme, Endpoint endpoint, @Nullable String path, Class<T> clientType, ClientOptions options)
- Parameters:
scheme
- theScheme
for theendpoint
endpoint
- the serverEndpoint
path
- the servicepath
clientType
- the type of the new clientoptions
- theClientOptions
-
clientBuilderParams
default <T> Optional<ClientBuilderParams> clientBuilderParams(T client)
Returns theClientBuilderParams
held inclient
. This is used when creating a new derivedClient
which inheritsClientBuilderParams
fromclient
. If thisClientFactory
does not know how to handle theClientBuilderParams
for the providedclient
, it should returnOptional.empty()
.
-
unwrap
default <T> Optional<T> unwrap(Object client, Class<T> type)
Unwraps the specifiedclient
object into the object of the specifiedtype
. For example,ClientFactory clientFactory = ...; HttpClient client = new HttpClientBuilder() .factory(clientFactory) .decorator(LoggingClient.newDecorator()) .build(); LoggingClient unwrapped = clientFactory.unwrap(client, LoggingClient.class).get(); // If the client implements Unwrappable, you can just use the 'as()' method. LoggingClient unwrapped2 = client.as(LoggingClient.class).get();
- Parameters:
client
- the client objecttype
- the type of the object to return- Returns:
- the object of the specified
type
if found.Optional.empty()
if not found. - See Also:
Client.as(Class)
,Clients.unwrap(Object, Class)
,Unwrappable
-
close
void close()
Closes all clients managed by this factory and shuts down theEventLoopGroup
created implicitly by this factory.- Specified by:
close
in interfaceAutoCloseable
-
-