public final class Server extends Object implements ListenableAsyncCloseable
ServerPorts and delegates client requests to Services.ServerBuilder| Modifier and Type | Method and Description |
|---|---|
int |
activeLocalPort()
Returns the local
ServerPort that this Server is listening to. |
int |
activeLocalPort(SessionProtocol protocol)
Returns the local
ServerPort which serves the given SessionProtocol. |
ServerPort |
activePort()
Returns the primary
ServerPort that this Server is listening to. |
Map<InetSocketAddress,ServerPort> |
activePorts()
Returns all
ServerPorts 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. |
static ServerBuilder |
builder()
Creates a new
ServerBuilder. |
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 this
Server. |
String |
defaultHostname()
Returns the hostname of the default
VirtualHost, which is the hostname of the machine. |
boolean |
isClosed()
Returns whether
AsyncCloseable.close() or AsyncCloseable.closeAsync() operation has been completed. |
boolean |
isClosing()
Returns whether
AsyncCloseable.close() or AsyncCloseable.closeAsync() has been called. |
MeterRegistry |
meterRegistry()
Returns the
MeterRegistry that collects various stats. |
io.netty.channel.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. |
List<ServiceConfig> |
serviceConfigs()
|
CompletableFuture<Void> |
start()
|
CompletableFuture<Void> |
stop()
Stops this
Server to close all active ServerPorts. |
String |
toString() |
CompletableFuture<?> |
whenClosed()
Returns the
CompletableFuture which is completed after the AsyncCloseable.close() or
AsyncCloseable.closeAsync() operation is completed. |
public static ServerBuilder builder()
ServerBuilder.public ServerConfig config()
Server.public List<ServiceConfig> serviceConfigs()
public String defaultHostname()
VirtualHost, which is the hostname of the machine.public Map<InetSocketAddress,ServerPort> activePorts()
ServerPorts 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 ServerPort activePort()
ServerPort that this Server is listening to. This method is useful
when a Server listens to only one ServerPort.ServerPort, or null if this Server did not start.public int activeLocalPort()
ServerPort that this Server is listening to.IllegalStateException - if there is no active local port available
or the server is not started yetpublic int activeLocalPort(SessionProtocol protocol)
ServerPort which serves the given SessionProtocol.IllegalStateException - if there is no active local port available for the given
SessionProtocol or the server is not started yetpublic 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 ServerPorts 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 = Server.builder();
...
Server server = builder.build();
server.start().get();
public CompletableFuture<Void> stop()
Server to close all active ServerPorts. 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 boolean isClosing()
ListenableAsyncCloseableAsyncCloseable.close() or AsyncCloseable.closeAsync() has been called.isClosing in interface ListenableAsyncCloseableListenableAsyncCloseable.isClosed()public boolean isClosed()
ListenableAsyncCloseableAsyncCloseable.close() or AsyncCloseable.closeAsync() operation has been completed.isClosed in interface ListenableAsyncCloseableListenableAsyncCloseable.isClosing()public CompletableFuture<?> whenClosed()
ListenableAsyncCloseableCompletableFuture which is completed after the AsyncCloseable.close() or
AsyncCloseable.closeAsync() operation is completed.whenClosed in interface ListenableAsyncCloseablepublic CompletableFuture<?> closeAsync()
AsyncCloseablecloseAsync in interface AsyncCloseableCompletableFuture which is completed after the resources are releasedpublic void close()
AsyncCloseableclose in interface AsyncCloseableclose in interface AutoCloseablepublic int numConnections()
Server.Copyright © 2020 LeanCloud. All rights reserved.