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>
public final class Endpoint extends Object implements Comparable<Endpoint>, EndpointGroup
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 Type Method Description String
authority()
Converts this endpoint into the authority part of a URI.void
close()
Releases any underlying resources held by this object synchronously.CompletableFuture<?>
closeAsync()
Releases any underlying resources held by this object asynchronously.int
compareTo(Endpoint that)
List<Endpoint>
endpoints()
Returns the endpoints held by thisEndpointGroup
.boolean
equals(Object obj)
int
hashCode()
boolean
hasIpAddr()
Returns whether this endpoint has an IP address resolved.boolean
hasPort()
Returns whether this endpoint has a port number specified.String
host()
Returns the host name of this endpoint.String
ipAddr()
Returns the IP address of this endpoint.StandardProtocolFamily
ipFamily()
Returns theStandardProtocolFamily
of this endpoint's IP address.boolean
isIpAddrOnly()
Returns whether this endpoint's host name is an IP address.static Endpoint
of(String host)
Creates a new hostEndpoint
with unspecified port number.static Endpoint
of(String host, int port)
Creates a new hostEndpoint
.static Endpoint
parse(String authority)
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.CompletableFuture<Endpoint>
select(ClientRequestContext ctx, ScheduledExecutorService executor, long timeoutMillis)
Selects anEndpoint
asynchronously from theEndpointGroup
associated with the specifiedClientRequestContext
, waiting up to the specifiedtimeoutMillis
.EndpointSelectionStrategy
selectionStrategy()
Returns theEndpointSelectionStrategy
of thisEndpointGroup
.Endpoint
selectNow(ClientRequestContext ctx)
Selects anEndpoint
from thisEndpointGroup
.String
toString()
URI
toUri(Scheme scheme)
Converts this endpoint into a URI using theScheme
.URI
toUri(Scheme scheme, String path)
Converts this endpoint into a URI using theScheme
and thepath
.URI
toUri(SessionProtocol sessionProtocol)
Converts this endpoint into a URI using theSessionProtocol
.URI
toUri(SessionProtocol sessionProtocol, String path)
Converts this endpoint into a URI using theSessionProtocol
andpath
.URI
toUri(String scheme)
Converts this endpoint into a URI using thescheme
.URI
toUri(String scheme, String path)
Converts this endpoint into a URI using thescheme
andpath
.int
weight()
Returns the weight of this endpoint.CompletableFuture<List<Endpoint>>
whenReady()
Returns aCompletableFuture
which is completed when the initialEndpoint
s are ready.Endpoint
withDefaultPort(int defaultPort)
Returns a new host endpoint with the specified default port number.Endpoint
withIpAddr(String ipAddr)
Returns a new host endpoint with the specified IP address.Endpoint
withoutDefaultPort(int defaultPort)
Returns a new host endpoint with the default port number removed.Endpoint
withoutPort()
Returns a new host endpoint with its port number unspecified.Endpoint
withPort(int port)
Returns a new host endpoint with the specified port number.Endpoint
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"<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
-
endpoints
Description copied from interface:EndpointGroup
Returns the endpoints held by thisEndpointGroup
.- Specified by:
endpoints
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
public CompletableFuture<Endpoint> select(ClientRequestContext ctx, ScheduledExecutorService executor, long timeoutMillis)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.
-
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.- Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
ipAddr
Returns the IP address of this endpoint.- Returns:
- the IP address, or
null
if the host name is not resolved yet - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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.- Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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- Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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 - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
port
public int port()Returns the port number of this endpoint.- Throws:
IllegalStateException
- if this endpoint is not a host but a group, or 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- Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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.- Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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. - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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. - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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. - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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:
IllegalStateException
- if this endpoint is not a host but a group
-
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. - Throws:
IllegalStateException
- if this endpoint is not a host but a group
-
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
-
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
. -
closeAsync
Description copied from interface:AsyncCloseable
Releases any underlying resources held by this object asynchronously.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Returns:
- the
CompletableFuture
which is completed after the resources are released
-
close
public void close()Description copied from interface:AsyncCloseable
Releases any underlying resources held by this object synchronously.- Specified by:
close
in interfaceAsyncCloseable
- Specified by:
close
in interfaceAutoCloseable
-
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<Endpoint>
-
toString
-