Package com.linecorp.armeria.server
Class Server
java.lang.Object
com.linecorp.armeria.server.Server
- All Implemented Interfaces:
AsyncCloseable
,ListenableAsyncCloseable
,AutoCloseable
Listens to
ServerPort
s and delegates client requests to Service
s.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns the localServerPort
that thisServer
is listening to.int
activeLocalPort
(SessionProtocol protocol) Returns the localServerPort
which serves the givenSessionProtocol
.Returns the primaryServerPort
that thisServer
is listening to.activePort
(SessionProtocol protocol) Returns the primaryServerPort
which serves the givenSessionProtocol
that thisServer
is listening to.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
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.Releases any underlying resources held by this object asynchronously.Registers a JVM shutdown hook that closes thisServer
when the current JVM terminates.closeOnJvmShutdown
(Runnable whenClosing) Registers a JVM shutdown hook that closes thisServer
when the current JVM terminates.config()
Returns the configuration of thisServer
.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
Returns whetherAsyncCloseable.close()
orAsyncCloseable.closeAsync()
has been called.io.micrometer.core.instrument.MeterRegistry
Returns theMeterRegistry
that collects various stats.io.netty.channel.EventLoop
Returns aEventLoop
from the worker group.int
Returns the number of open connections on thisServer
.void
reconfigure
(ServerConfigurator serverConfigurator) Reconfigure Server configuration.boolean
removeListener
(ServerListener listener) Removes the specifiedServerListener
from thisServer
, so that it is not notified anymore when the state of thisServer
changes.start()
stop()
Stops thisServer
to close all activeServerPort
s.toString()
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
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
public io.micrometer.core.instrument.MeterRegistry 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
public io.netty.channel.EventLoop 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:
-
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:
-
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
-
reconfigure
Reconfigure Server configuration. This feature is only available once a server is configured and started. We do not allow ports to be reconfigured. -
closeOnJvmShutdown
Registers a JVM shutdown hook that closes thisServer
when the current JVM terminates. -
closeOnJvmShutdown
Registers a JVM shutdown hook that closes thisServer
when the current JVM terminates.
-