public final class Server
extends java.lang.Object
implements java.lang.AutoCloseable
ServerPort
s and delegates client requests to Service
s.ServerBuilder
Modifier and Type | Method and Description |
---|---|
java.util.Optional<ServerPort> |
activePort()
Returns the primary
ServerPort that this Server is listening to. |
java.util.Map<java.net.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 . |
java.lang.String |
defaultHostname()
Returns the hostname of the default
VirtualHost , which is the hostname of the machine unless
configured explicitly via ServerBuilder.defaultVirtualHost(VirtualHost) . |
io.netty.channel.EventLoop |
nextEventLoop()
Returns a
EventLoop from the worker group. |
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. |
java.util.concurrent.CompletableFuture<java.lang.Void> |
start()
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
stop()
Stops this
Server to close all active ServerPort s. |
public ServerConfig config()
Server
.public java.lang.String defaultHostname()
VirtualHost
, which is the hostname of the machine unless
configured explicitly via ServerBuilder.defaultVirtualHost(VirtualHost)
.public java.util.Map<java.net.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 java.util.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 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 java.util.concurrent.CompletableFuture<java.lang.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 java.util.concurrent.CompletableFuture<java.lang.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 io.netty.channel.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 java.lang.AutoCloseable