Package com.linecorp.armeria.client
Class Endpoint
java.lang.Object
com.linecorp.armeria.client.Endpoint
- All Implemented Interfaces:
EndpointGroup
,EndpointSelector
,AsyncCloseable
,Listenable<List<Endpoint>>
,AutoCloseable
,Comparable<Endpoint>
A remote endpoint that refers to a single host.
An endpoint has host()
, optional ipAddr()
and optional port()
. It can be
represented as "<host>"
or "<host>:<port>"
in the authority part of a URI. It can have
an IP address if the host name has been resolved and thus there's no need to query a DNS server.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Consumer<? super List<Endpoint>> listener, boolean notifyLatestEndpoints) Adds aConsumer
that will be invoked when thisEndpointGroup
changes its value.<T> T
attr
(AttributeKey<T> key) Returns the attribute value associated with the givenAttributeKey
of this endpoint, ornull
if there's no value associated with this key.attrs()
Returns theAttributes
of this endpoint, or an emptyAttributes
if this endpoint does not have any attributes.Converts this endpoint into the authority part of a URI.void
close()
This method does nothing.This method does nothing but returning an immediately complete future.int
Returns the endpoints held by thisEndpointGroup
.boolean
int
hashCode()
boolean
Returns whether this endpoint has an IP address resolved.boolean
hasPort()
Returns whether this endpoint has a port number specified.host()
Returns the host name of this endpoint.ipAddr()
Returns the IP address of this endpoint.ipFamily()
Returns theStandardProtocolFamily
of this endpoint's IP address.boolean
Returns whether this endpoint connects to a domain socket.boolean
Returns whether this endpoint's host name is an IP address.static Endpoint
Creates a new hostEndpoint
with unspecified port number.static Endpoint
Creates a new hostEndpoint
.static Endpoint
of
(SocketAddress addr) Creates a newEndpoint
from the specifiedSocketAddress
.static Endpoint
Parse the authority part of a URI.int
port()
Returns the port number of this endpoint.int
port
(int defaultValue) Returns the port number of this endpoint.select
(ClientRequestContext ctx, ScheduledExecutorService executor) Selects anEndpoint
asynchronously from theEndpointGroup
associated with the specifiedClientRequestContext
, waiting up to the specifiedtimeoutMillis
.select
(ClientRequestContext ctx, ScheduledExecutorService executor, long timeoutMillis) Deprecated.Returns theEndpointSelectionStrategy
of thisEndpointGroup
.long
Returns the timeout to wait until a successfulEndpoint
selection.Selects anEndpoint
from thisEndpointGroup
.toSocketAddress
(int defaultPort) Converts this endpoint into anInetSocketAddress
.toString()
Converts this endpoint into a URI using theScheme
.Converts this endpoint into a URI using theScheme
and thepath
.toUri
(SessionProtocol sessionProtocol) Converts this endpoint into a URI using theSessionProtocol
.toUri
(SessionProtocol sessionProtocol, @Nullable String path) Converts this endpoint into a URI using theSessionProtocol
andpath
.Converts this endpoint into a URI using thescheme
.Converts this endpoint into a URI using thescheme
andpath
.static Endpoint
unsafeCreate
(String host, int port) Creates a new hostEndpoint
without validation.int
weight()
Returns the weight of this endpoint.Returns aCompletableFuture
which is completed when the initialEndpoint
s are ready.<T> Endpoint
withAttr
(AttributeKey<T> key, T value) Returns a new host endpoint with the specifiedAttributeKey
and value.withAttrs
(Attributes newAttributes) Returns a newEndpoint
with the specifiedAttributes
.withDefaultPort
(int defaultPort) Returns a new host endpoint with the specified default port number.withDefaultPort
(SessionProtocol protocol) Returns a new host endpoint with the default port number of the specifiedSessionProtocol
.Returns a new endpoint with the specified host.withInetAddress
(InetAddress address) Returns a new host endpoint with the IP address of the specifiedInetAddress
.withIpAddr
(@Nullable String ipAddr) Returns a new host endpoint with the specified IP address.withoutDefaultPort
(int defaultPort) Returns a new host endpoint with the default port number removed.withoutDefaultPort
(SessionProtocol protocol) Returns a new host endpoint with the default port number of the specifiedSessionProtocol
removed.Returns a new host endpoint with its port number unspecified.withPort
(int port) Returns a new host endpoint with the specified port number.withWeight
(int weight) Returns a new host endpoint with the specified weight.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.client.endpoint.EndpointGroup
addListener, orElse, removeListener
-
Method Details
-
parse
Parse the authority part of a URI. The authority part may have one of the following formats:"<host>:<port>"
for a host endpoint (The userinfo part will be ignored.)"<host>"
for a host endpoint with no port number specified
"127.0.0.1:8080"
and"[::1]:8080"
. -
of
Creates a new hostEndpoint
.- Throws:
IllegalArgumentException
- ifhost
is not a valid host name orport
is not a valid port number
-
of
Creates a new hostEndpoint
with unspecified port number.- Throws:
IllegalArgumentException
- ifhost
is not a valid host name
-
of
Creates a newEndpoint
from the specifiedSocketAddress
. This method converts the following address types into an endpoint:- Throws:
IllegalArgumentException
- if the specifiedSocketAddress
is not supported
-
unsafeCreate
Creates a new hostEndpoint
without validation.Note that you should carefully use this method only when both
host
andport
are already valid. -
endpoints
Description copied from interface:EndpointGroup
Returns the endpoints held by thisEndpointGroup
.- Specified by:
endpoints
in interfaceEndpointGroup
-
addListener
Description copied from interface:EndpointGroup
Adds aConsumer
that will be invoked when thisEndpointGroup
changes its value. IfnotifyLatestEndpoints
is set to true and theEndpointGroup.whenReady()
is completed already, theConsumer
will be invoked immediately with the currentEndpointGroup.endpoints()
.- Specified by:
addListener
in interfaceEndpointGroup
-
selectionStrategy
Description copied from interface:EndpointGroup
Returns theEndpointSelectionStrategy
of thisEndpointGroup
.- Specified by:
selectionStrategy
in interfaceEndpointGroup
-
selectNow
Description copied from interface:EndpointGroup
Selects anEndpoint
from thisEndpointGroup
.- Specified by:
selectNow
in interfaceEndpointGroup
- Specified by:
selectNow
in interfaceEndpointSelector
- Returns:
- the
Endpoint
selected by theEndpointSelectionStrategy
, which was specified when constructing thisEndpointGroup
, ornull
if thisEndpointGroup
is empty.
-
select
@Deprecated public CompletableFuture<Endpoint> select(ClientRequestContext ctx, ScheduledExecutorService executor, long timeoutMillis) Deprecated.Description copied from interface:EndpointSelector
Selects anEndpoint
asynchronously from theEndpointGroup
associated with the specifiedClientRequestContext
, waiting up to the specifiedtimeoutMillis
.- Specified by:
select
in interfaceEndpointSelector
- Parameters:
ctx
- theClientRequestContext
of theRequest
being handled.executor
- theScheduledExecutorService
used for notifying theCompletableFuture
being returned and scheduling timeout tasks.timeoutMillis
- the amount of milliseconds to wait until a successfulEndpoint
selection.- Returns:
- the
CompletableFuture
that will be completed with theEndpoint
selected by thisEndpointSelector
's selection strategy, or completed withnull
if noEndpoint
was selected within the specifiedtimeoutMillis
, which can happen if theEndpointGroup
is empty.
-
select
public CompletableFuture<Endpoint> select(ClientRequestContext ctx, ScheduledExecutorService executor) Description copied from interface:EndpointSelector
Selects anEndpoint
asynchronously from theEndpointGroup
associated with the specifiedClientRequestContext
, waiting up to the specifiedtimeoutMillis
.- Specified by:
select
in interfaceEndpointSelector
- Parameters:
ctx
- theClientRequestContext
of theRequest
being handled.executor
- theScheduledExecutorService
used for notifying theCompletableFuture
being returned and scheduling timeout tasks.- Returns:
- the
CompletableFuture
that will be completed with theEndpoint
selected by thisEndpointSelector
's selection strategy, or completed withnull
if noEndpoint
was selected within the specifiedtimeoutMillis
, which can happen if theEndpointGroup
is empty.
-
selectionTimeoutMillis
public long selectionTimeoutMillis()Description copied from interface:EndpointGroup
Returns the timeout to wait until a successfulEndpoint
selection. If anEndpoint
is not resolved by thisEndpointGroup
within the timeout, a null value will be returned byEndpointSelector.select(ClientRequestContext, ScheduledExecutorService)
. The nullEndpoint
may cause a client request end with anEndpointSelectionTimeoutException
if noRetryingClient
is configured.0
meansEndpointGroup.selectNow(ClientRequestContext)
should always return anEndpoint
.- Specified by:
selectionTimeoutMillis
in interfaceEndpointGroup
-
whenReady
Description copied from interface:EndpointGroup
Returns aCompletableFuture
which is completed when the initialEndpoint
s are ready.- Specified by:
whenReady
in interfaceEndpointGroup
-
host
Returns the host name of this endpoint. -
withHost
Returns a new endpoint with the specified host. -
ipAddr
Returns the IP address of this endpoint.- Returns:
- the IP address, or
null
if the host name is not resolved yet
-
hasIpAddr
public boolean hasIpAddr()Returns whether this endpoint has an IP address resolved. This method is a shortcut foripAddr() != null
.- Returns:
true
if and only if this endpoint has an IP address.
-
isIpAddrOnly
public boolean isIpAddrOnly()Returns whether this endpoint's host name is an IP address.- Returns:
true
if and only if this endpoint's host name is an IP address
-
ipFamily
Returns theStandardProtocolFamily
of this endpoint's IP address.- Returns:
- the
StandardProtocolFamily
of this endpoint's IP address, ornull
if the host name is not resolved yet.
-
isDomainSocket
Returns whether this endpoint connects to a domain socket. -
port
public int port()Returns the port number of this endpoint.- Throws:
IllegalStateException
- this endpoint does not have its port specified.
-
port
public int port(int defaultValue) Returns the port number of this endpoint.- Parameters:
defaultValue
- the default value to return when this endpoint does not have its port specified
-
hasPort
public boolean hasPort()Returns whether this endpoint has a port number specified.- Returns:
true
if and only if this endpoint has a port number.
-
withPort
Returns a new host endpoint with the specified port number.- Parameters:
port
- the new port number- Returns:
- the new endpoint with the specified port number if this endpoint does not have a port or
it has a different port number than what's specified.
this
if this endpoint has the same port number with the specified one.
-
withoutPort
Returns a new host endpoint with its port number unspecified.- Returns:
- the new endpoint whose port is unspecified if this endpoint has its port.
this
if this endpoint does not have a port already. - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
withDefaultPort
Returns a new host endpoint with the specified default port number.- Parameters:
defaultPort
- the default port number- Returns:
- the new endpoint whose port is
defaultPort
if this endpoint does not have its port specified.this
if this endpoint already has its port specified.
-
withDefaultPort
Returns a new host endpoint with the default port number of the specifiedSessionProtocol
.- Parameters:
protocol
- theSessionProtocol
that will provide the default port number- Returns:
- the new endpoint whose port is the default port number of the specified
SessionProtocol
if this endpoint does not have its port specified.this
if this endpoint already has its port specified.
-
withoutDefaultPort
Returns a new host endpoint with the default port number removed.- Parameters:
defaultPort
- the default port number- Returns:
- the new endpoint without a port number if this endpoint had the same port number
with the specified default port number.
this
if this endpoint had a different port number than the specified default port number or this endpoint already does not have a port number.
-
withoutDefaultPort
Returns a new host endpoint with the default port number of the specifiedSessionProtocol
removed.- Parameters:
protocol
- theSessionProtocol
that will provide the default port number- Returns:
- the new endpoint without a port number if this endpoint had the same port number
with the default port number provided by the specified
SessionProtocol
.this
if this endpoint had a different port number than the default port number or this endpoint already does not have a port number.
-
withIpAddr
Returns a new host endpoint with the specified IP address.- Returns:
- the new endpoint with the specified IP address.
this
if this endpoint has the same IP address. - Throws:
IllegalArgumentException
- if the specified IP address is invalid
-
withInetAddress
Returns a new host endpoint with the IP address of the specifiedInetAddress
.- Returns:
- the new endpoint with the specified
InetAddress
.this
if this endpoint has the same IP address.
-
withWeight
Returns a new host endpoint with the specified weight.- Returns:
- the new endpoint with the specified weight.
this
if this endpoint has the same weight.
-
weight
public int weight()Returns the weight of this endpoint. -
authority
Converts this endpoint into the authority part of a URI.- Returns:
- the authority string
-
attr
Returns the attribute value associated with the givenAttributeKey
of this endpoint, ornull
if there's no value associated with this key. -
withAttr
Returns a new host endpoint with the specifiedAttributeKey
and value.- Returns:
- the new endpoint with the specified
AttributeKey
and value.this
if this endpoint has the same value with the specifiedAttributeKey
.
-
withAttrs
Returns a newEndpoint
with the specifiedAttributes
. Note that theattrs()
of thisEndpoint
is replaced with the specifiedAttributes
. -
attrs
Returns theAttributes
of this endpoint, or an emptyAttributes
if this endpoint does not have any attributes. -
toUri
Converts this endpoint into a URI using thescheme
.- Parameters:
scheme
- thescheme
forURI
.- Returns:
- the URI
-
toUri
Converts this endpoint into a URI using thescheme
andpath
. -
toUri
Converts this endpoint into a URI using theSessionProtocol
.- Parameters:
sessionProtocol
- theSessionProtocol
forURI
.- Returns:
- the URI
-
toUri
Converts this endpoint into a URI using theSessionProtocol
andpath
.- Parameters:
sessionProtocol
- theSessionProtocol
forURI
.path
- thepath
forURI
.- Returns:
- the URI
-
toUri
Converts this endpoint into a URI using theScheme
. -
toUri
Converts this endpoint into a URI using theScheme
and thepath
. -
toSocketAddress
Converts this endpoint into anInetSocketAddress
. The specifieddefaultPort
is used if this endpoint does not have a port number. ADomainSocketAddress
is returned if this endpoint refers to a Unix domain socket.- See Also:
-
closeAsync
This method does nothing but returning an immediately complete future.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Returns:
- the
CompletableFuture
which is completed after the resources are released
-
close
public void close()This method does nothing.- Specified by:
close
in interfaceAsyncCloseable
- Specified by:
close
in interfaceAutoCloseable
-
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<Endpoint>
-
toString
-