public final class Server extends Object implements AutoCloseable
ServerPort
s and delegates client requests to Service
s.ServerBuilder
Modifier and Type | Method and Description |
---|---|
Optional<ServerPort> |
activePort()
Returns the primary
ServerPort that this Server is listening to. |
Map<InetSocketAddress,ServerPort> |
activePorts()
Returns all
ServerPort s that this Server is listening to. |
void |
addListener(ServerListener listener)
Adds the specified
ServerListener to this Server , so that it is notified when the state
of this Server changes. |
void |
close()
A shortcut to
stop().get() . |
ServerConfig |
config()
Returns the configuration of this
Server . |
String |
defaultHostname()
Returns the hostname of the default
VirtualHost , which is the hostname of the machine unless
configured explicitly via ServerBuilder.defaultVirtualHost(VirtualHost) . |
MeterRegistry |
meterRegistry()
Returns the
MeterRegistry that collects various stats. |
EventLoop |
nextEventLoop()
Returns a
EventLoop from the worker group. |
int |
numConnections()
Returns the number of open connections on this
Server . |
boolean |
removeListener(ServerListener listener)
Removes the specified
ServerListener from this Server , so that it is not notified
anymore when the state of this Server changes. |
CompletableFuture<Void> |
start()
|
CompletableFuture<Void> |
stop()
Stops this
Server to close all active ServerPort s. |
String |
toString() |
public ServerConfig config()
Server
.public String defaultHostname()
VirtualHost
, which is the hostname of the machine unless
configured explicitly via ServerBuilder.defaultVirtualHost(VirtualHost)
.public Map<InetSocketAddress,ServerPort> activePorts()
ServerPort
s that this Server
is listening to.Map
whose key is the bind address and value is ServerPort
.
an empty Map
if this Server
did not start.activePort()
public Optional<ServerPort> activePort()
ServerPort
that this Server
is listening to. This method is useful
when a Server
listens to only one ServerPort
.Optional.empty()
if this Server
did not startpublic MeterRegistry meterRegistry()
MeterRegistry
that collects various stats.public void addListener(ServerListener listener)
ServerListener
to this Server
, so that it is notified when the state
of this Server
changes. This method is useful when you want to initialize/destroy the resources
associated with a Service
:
> 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 ...
> }
> }
> }
> }
public boolean removeListener(ServerListener listener)
ServerListener
from this Server
, so that it is not notified
anymore when the state of this Server
changes.public CompletableFuture<Void> start()
Server
to listen to the ServerPort
s specified in the ServerConfig
.
Note that the startup procedure is asynchronous and thus this method returns immediately. To wait until
this Server
is fully started up, wait for the returned CompletableFuture
:
ServerBuilder builder = new ServerBuilder();
...
Server server = builder.build();
server.start().get();
public CompletableFuture<Void> stop()
Server
to close all active ServerPort
s. Note that the shutdown procedure is
asynchronous and thus this method returns immediately. To wait until this Server
is fully
shut down, wait for the returned CompletableFuture
:
Server server = ...;
server.stop().get();
public EventLoop nextEventLoop()
EventLoop
from the worker group. This can be used for, e.g., scheduling background
tasks for the lifetime of the Server
using
EventExecutorGroup.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 that EventLoop
.public void close()
stop().get()
.close
in interface AutoCloseable
public int numConnections()
Server
.© Copyright 2015–2017 LINE Corporation. All rights reserved.