Package com.linecorp.armeria.server
Class Server
java.lang.Object
com.linecorp.armeria.server.Server
- All Implemented Interfaces:
AsyncCloseable
,ListenableAsyncCloseable
,AutoCloseable
public final class Server extends Object implements ListenableAsyncCloseable
Listens to
ServerPort
s and delegates client requests to Service
s.- See Also:
ServerBuilder
-
Method Summary
Modifier and Type Method Description int
activeLocalPort()
Returns the localServerPort
that thisServer
is listening to.int
activeLocalPort(SessionProtocol protocol)
Returns the localServerPort
which serves the givenSessionProtocol
.ServerPort
activePort()
Returns the primaryServerPort
that thisServer
is listening to.ServerPort
activePort(SessionProtocol protocol)
Returns the primaryServerPort
which serves the givenSessionProtocol
that thisServer
is listening to.Map<InetSocketAddress,ServerPort>
activePorts()
Returns allServerPort
s that thisServer
is listening to.void
addListener(ServerListener listener)
Adds the specifiedServerListener
to thisServer
, so that it is notified when the state of thisServer
changes.void
blockUntilShutdown()
Waits until the result ofCompletableFuture
which is completed after theclose()
orcloseAsync()
operation is completed.static ServerBuilder
builder()
Creates a newServerBuilder
.void
close()
Releases any underlying resources held by this object synchronously.CompletableFuture<?>
closeAsync()
Releases any underlying resources held by this object asynchronously.ServerConfig
config()
Returns the configuration of thisServer
.String
defaultHostname()
Returns the hostname of the defaultVirtualHost
, which is the hostname of the machine.boolean
isClosed()
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation has been completed.boolean
isClosing()
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
has been called.MeterRegistry
meterRegistry()
Returns theMeterRegistry
that collects various stats.EventLoop
nextEventLoop()
Returns aEventLoop
from the worker group.int
numConnections()
Returns the number of open connections on thisServer
.boolean
removeListener(ServerListener listener)
Removes the specifiedServerListener
from thisServer
, so that it is not notified anymore when the state of thisServer
changes.List<ServiceConfig>
serviceConfigs()
CompletableFuture<Void>
start()
CompletableFuture<Void>
stop()
Stops thisServer
to close all activeServerPort
s.String
toString()
CompletableFuture<?>
whenClosed()
Returns theCompletableFuture
which is completed after theAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation is completed.
-
Method Details
-
builder
Creates a newServerBuilder
. -
config
Returns the configuration of thisServer
. -
serviceConfigs
-
defaultHostname
Returns the hostname of the defaultVirtualHost
, which is the hostname of the machine. -
activePorts
Returns allServerPort
s that thisServer
is listening to.- Returns:
- a
Map
whose key is the bind address and value isServerPort
. an emptyMap
if thisServer
did not start. - See Also:
activePort()
-
activePort
Returns the primaryServerPort
that thisServer
is listening to. If thisServer
has both a local port and a non-local port, the non-local port is returned.- Returns:
- the primary
ServerPort
, ornull
if thisServer
did not start.
-
activePort
Returns the primaryServerPort
which serves the givenSessionProtocol
that thisServer
is listening to. If thisServer
has both a local port and a non-local port, the non-local port is returned.- Returns:
- the primary
ServerPort
, ornull
if there is no active port available for the givenSessionProtocol
.
-
activeLocalPort
public int activeLocalPort()Returns the localServerPort
that thisServer
is listening to.- Throws:
IllegalStateException
- if there is no active local port available or the server is not started yet
-
activeLocalPort
Returns the localServerPort
which serves the givenSessionProtocol
.- Throws:
IllegalStateException
- if there is no active local port available for the givenSessionProtocol
or the server is not started yet
-
meterRegistry
Returns theMeterRegistry
that collects various stats. -
addListener
Adds the specifiedServerListener
to thisServer
, so that it is notified when the state of thisServer
changes. This method is useful when you want to initialize/destroy the resources associated with aService
:> public class MyService extends SimpleService { > @Override > public void serviceAdded(Server server) { > server.addListener(new ServerListenerAdapter() { > @Override > public void serverStarting() { > ... initialize ... > } > > @Override > public void serverStopped() { > ... destroy ... > } > } > } > }
-
removeListener
Removes the specifiedServerListener
from thisServer
, so that it is not notified anymore when the state of thisServer
changes. -
start
Starts thisServer
to listen to theServerPort
s specified in theServerConfig
. Note that the startup procedure is asynchronous and thus this method returns immediately. To wait until thisServer
is fully started up, wait for the returnedCompletableFuture
:ServerBuilder builder = Server.builder(); ... Server server = builder.build(); server.start().get();
-
stop
Stops thisServer
to close all activeServerPort
s. Note that the shutdown procedure is asynchronous and thus this method returns immediately. To wait until thisServer
is fully shut down, wait for the returnedCompletableFuture
:Server server = ...; server.stop().get();
-
nextEventLoop
Returns aEventLoop
from the worker group. This can be used for, e.g., scheduling background tasks for the lifetime of theServer
usingEventExecutorGroup.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
. It is very important that these tasks do not block as this would block all requests in the server on thatEventLoop
. -
isClosing
public boolean isClosing()Description copied from interface:ListenableAsyncCloseable
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
has been called.- Specified by:
isClosing
in interfaceListenableAsyncCloseable
- See Also:
ListenableAsyncCloseable.isClosed()
-
isClosed
public boolean isClosed()Description copied from interface:ListenableAsyncCloseable
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation has been completed.- Specified by:
isClosed
in interfaceListenableAsyncCloseable
- See Also:
ListenableAsyncCloseable.isClosing()
-
whenClosed
Description copied from interface:ListenableAsyncCloseable
Returns theCompletableFuture
which is completed after theAsyncCloseable.close()
orAsyncCloseable.closeAsync()
operation is completed.- Specified by:
whenClosed
in interfaceListenableAsyncCloseable
-
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
-
numConnections
public int numConnections()Returns the number of open connections on thisServer
. -
blockUntilShutdown
Waits until the result ofCompletableFuture
which is completed after theclose()
orcloseAsync()
operation is completed.- Throws:
InterruptedException
-
toString
-