Package io.micronaut.http.server.netty
Class NettyHttpServer
java.lang.Object
io.micronaut.http.server.netty.NettyHttpServer
- All Implemented Interfaces:
ApplicationContextLifeCycle<EmbeddedServer>,ApplicationContextProvider,ApplicationEventListener<RefreshEvent>,LifeCycle,Ordered,ChannelPipelineCustomizer,WebSocketSessionRepository,NettyEmbeddedServer,NettyServerCustomizer.Registry,RefreshEventListener,EmbeddedApplication<EmbeddedServer>,GracefulShutdownCapable,EmbeddedServer,Closeable,AutoCloseable,EventListener
Implements the bootstrap and configuration logic for the Netty implementation of
EmbeddedServer.- Since:
- 1.0
- Author:
- Graeme Rocher
- See Also:
-
Field Summary
FieldsFields inherited from interface io.micronaut.http.netty.channel.ChannelPipelineCustomizer
HANDLER_ACCESS_LOGGER, HANDLER_ACTIVITY_LISTENER, HANDLER_CONNECT_TTL, HANDLER_FLOW_CONTROL, HANDLER_HTTP_AGGREGATOR, HANDLER_HTTP_CHUNK, HANDLER_HTTP_CLIENT_CODEC, HANDLER_HTTP_CLIENT_INIT, HANDLER_HTTP_COMPRESSOR, HANDLER_HTTP_DECODER, HANDLER_HTTP_DECOMPRESSOR, HANDLER_HTTP_KEEP_ALIVE, HANDLER_HTTP_PROXY, HANDLER_HTTP_SERVER_CODEC, HANDLER_HTTP_STREAM, HANDLER_HTTP_TO_HTTPS_REDIRECT, HANDLER_HTTP2_CONNECTION, HANDLER_HTTP2_PING_SENDER, HANDLER_HTTP2_PROTOCOL_NEGOTIATOR, HANDLER_HTTP2_SETTINGS, HANDLER_HTTP2_UPGRADE_REQUEST, HANDLER_IDLE_STATE, HANDLER_INITIAL_ERROR, HANDLER_MICRONAUT_FULL_HTTP_RESPONSE, HANDLER_MICRONAUT_HTTP_RESPONSE, HANDLER_MICRONAUT_HTTP_RESPONSE_FULL, HANDLER_MICRONAUT_HTTP_RESPONSE_STREAM, HANDLER_MICRONAUT_INBOUND, HANDLER_MICRONAUT_SSE_CONTENT, HANDLER_MICRONAUT_SSE_EVENT_STREAM, HANDLER_MICRONAUT_WEBSOCKET_CLIENT, HANDLER_READ_TIMEOUT, HANDLER_SOCKS_5_PROXY, HANDLER_SSL, HANDLER_WEBSOCKET_UPGRADEFields inherited from interface io.micronaut.core.order.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCEFields inherited from interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener
DEFAULT_POSITION -
Constructor Summary
ConstructorsConstructorDescriptionNettyHttpServer(NettyHttpServerConfiguration serverConfiguration, NettyEmbeddedServices nettyEmbeddedServices, boolean isDefault) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChannel(io.netty.channel.Channel channel) Adds a channel to the repository.io.netty.channel.embedded.EmbeddedChannelbuildEmbeddedChannel(boolean ssl) Builds Embedded Channel.voidbuildEmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel prototype, boolean ssl) Builds Embedded Channel.protected io.netty.channel.EventLoopGroupprotected io.netty.bootstrap.ServerBootstrapprotected io.netty.channel.EventLoopGroupcreateWorkerEventLoopGroup(@Nullable EventLoopGroupConfiguration workerConfig) voiddoOnConnect(@NonNull ChannelPipelineListener listener) A hook to customize the pipeline upon establishing a connection.Resolves theApplicationContextfor this class.Gets the set of all ports this Netty server is bound to.io.netty.channel.group.ChannelGroupReturns theChannelGroupused to store WebSocketSessions.getHost()Returns the set of observed configuration prefixes that the event listener should listen for.intgetPort()getURI()getURL()booleanbooleanMost servers provide a way to block such that the server doesn't exit, however some require the creation of a keep alive thread.booleanvoidonApplicationEvent(RefreshEvent event) Handle an application event.voidregister(@NonNull NettyServerCustomizer customizer) Register a new customizer with this server.voidremoveChannel(io.netty.channel.Channel channel) Remove a channel from the repository.After a call toGracefulShutdownCapable.shutdownGracefully()report the state of the shutdown.Trigger a graceful shutdown.start()Starts the lifecyle component.stop()Stops the life cycle component.Stops the Netty instance, but keeps the ApplicationContext running.static <T> CompletionStage<T>toCompletionStage(io.netty.util.concurrent.Future<T> future) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.http.netty.channel.ChannelPipelineCustomizer
isServerChannelMethods inherited from interface io.micronaut.runtime.EmbeddedApplication
getEnvironment, isForceExit, isShutdownHookNeededMethods inherited from interface io.micronaut.runtime.server.EmbeddedServer
isServerMethods inherited from interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener
getOrder, supports
-
Field Details
-
OUTBOUND_KEY
- See Also:
-
-
Constructor Details
-
NettyHttpServer
public NettyHttpServer(NettyHttpServerConfiguration serverConfiguration, NettyEmbeddedServices nettyEmbeddedServices, boolean isDefault) - Parameters:
serverConfiguration- The Netty HTTP server configurationnettyEmbeddedServices- The embedded server contextisDefault- Is this the default server
-
-
Method Details
-
isKeepAlive
public boolean isKeepAlive()Description copied from interface:EmbeddedServerMost servers provide a way to block such that the server doesn't exit, however some require the creation of a keep alive thread.- Specified by:
isKeepAlivein interfaceEmbeddedServer- Returns:
- True if the server should be kept alive.
-
getServerConfiguration
- Returns:
- The configuration for the server
-
isRunning
public boolean isRunning() -
start
Description copied from interface:LifeCycleStarts the lifecyle component.- Specified by:
startin interfaceApplicationContextLifeCycle<EmbeddedServer>- Specified by:
startin interfaceLifeCycle- Specified by:
startin interfaceNettyEmbeddedServer- Returns:
- This lifecycle component
-
stop
Description copied from interface:LifeCycleStops the life cycle component.- Specified by:
stopin interfaceApplicationContextLifeCycle<EmbeddedServer>- Specified by:
stopin interfaceLifeCycle- Specified by:
stopin interfaceNettyEmbeddedServer- Returns:
- This lifecycle component
-
stopServerOnly
Description copied from interface:NettyEmbeddedServerStops the Netty instance, but keeps the ApplicationContext running. This for CRaC checkpointing purposes. This method will only return after waiting for netty to stop.- Specified by:
stopServerOnlyin interfaceNettyEmbeddedServer- Returns:
- The stopped NettyEmbeddedServer
-
register
Description copied from interface:NettyServerCustomizer.RegistryRegister a new customizer with this server. Note that this method must be called before the server is started: When a listener launches, it may only respect the customizers that were registered at the time, and ignore future additions.- Specified by:
registerin interfaceNettyEmbeddedServer- Specified by:
registerin interfaceNettyServerCustomizer.Registry- Parameters:
customizer- The customizer to register.
-
getPort
public int getPort()- Specified by:
getPortin interfaceEmbeddedServer- Returns:
- The port exposed by the server
-
getHost
- Specified by:
getHostin interfaceEmbeddedServer- Returns:
- The host of the server
-
getScheme
- Specified by:
getSchemein interfaceEmbeddedServer- Returns:
- The scheme of the server (http/https)
-
getURL
- Specified by:
getURLin interfaceEmbeddedServer- Returns:
- The full URL to the server
-
getURI
- Specified by:
getURIin interfaceEmbeddedServer- Returns:
- The full URI to the server
-
getContextURI
- Specified by:
getContextURIin interfaceEmbeddedServer- Returns:
- The full URI to the server with the context path
-
getApplicationContext
Description copied from interface:ApplicationContextProviderResolves theApplicationContextfor this class.- Specified by:
getApplicationContextin interfaceApplicationContextProvider- Specified by:
getApplicationContextin interfaceEmbeddedApplication<EmbeddedServer>- Returns:
- The
ApplicationContextfor the server
-
getApplicationConfiguration
- Specified by:
getApplicationConfigurationin interfaceEmbeddedApplication<EmbeddedServer>- Returns:
- The
ApplicationConfigurationinstance
-
getBoundPorts
Description copied from interface:NettyEmbeddedServerGets the set of all ports this Netty server is bound to.- Specified by:
getBoundPortsin interfaceNettyEmbeddedServer- Returns:
- An immutable set of bound ports if the server has been started with
NettyEmbeddedServer.start()an empty set otherwise.
-
createParentEventLoopGroup
protected io.netty.channel.EventLoopGroup createParentEventLoopGroup()- Returns:
- The parent event loop group
-
createWorkerEventLoopGroup
protected io.netty.channel.EventLoopGroup createWorkerEventLoopGroup(@Nullable @Nullable EventLoopGroupConfiguration workerConfig) - Parameters:
workerConfig- The worker configuration- Returns:
- The worker event loop group
-
createServerBootstrap
protected io.netty.bootstrap.ServerBootstrap createServerBootstrap()- Returns:
- The Netty server bootstrap
-
toCompletionStage
-
addChannel
public void addChannel(io.netty.channel.Channel channel) Description copied from interface:WebSocketSessionRepositoryAdds a channel to the repository.- Specified by:
addChannelin interfaceWebSocketSessionRepository- Parameters:
channel- The channel
-
removeChannel
public void removeChannel(io.netty.channel.Channel channel) Description copied from interface:WebSocketSessionRepositoryRemove a channel from the repository.- Specified by:
removeChannelin interfaceWebSocketSessionRepository- Parameters:
channel- The channel
-
getChannelGroup
public io.netty.channel.group.ChannelGroup getChannelGroup()Description copied from interface:WebSocketSessionRepositoryReturns theChannelGroupused to store WebSocketSessions.- Specified by:
getChannelGroupin interfaceWebSocketSessionRepository- Returns:
- A
ChannelGroup
-
getWebSocketSessionRepository
- Returns:
NettyHttpServerwhich implementsWebSocketSessionRepository
-
isClientChannel
public boolean isClientChannel()- Specified by:
isClientChannelin interfaceChannelPipelineCustomizer- Returns:
- Is this customizer the client.
-
doOnConnect
Description copied from interface:ChannelPipelineCustomizerA hook to customize the pipeline upon establishing a connection.- Specified by:
doOnConnectin interfaceChannelPipelineCustomizer- Parameters:
listener- The listener The listener.
-
getObservedConfigurationPrefixes
Description copied from interface:RefreshEventListenerReturns the set of observed configuration prefixes that the event listener should listen for.- Specified by:
getObservedConfigurationPrefixesin interfaceRefreshEventListener- Returns:
- A set of prefixes
-
shutdownGracefully
Description copied from interface:GracefulShutdownCapableTrigger a graceful shutdown. The returnedCompletionStagewill complete when the shutdown is complete.Note that the completion of the returned future may be user-dependent. If a user does not close their connection, the future may never terminate. Always add a timeout for a hard shutdown.
This method should not throw an exception, nor should the returned stage complete exceptionally. Just log an error instead.
- Specified by:
shutdownGracefullyin interfaceGracefulShutdownCapable- Specified by:
shutdownGracefullyin interfaceNettyEmbeddedServer- Returns:
- A future that completes when this bean is fully shut down
-
reportActiveTasks
Description copied from interface:GracefulShutdownCapableAfter a call toGracefulShutdownCapable.shutdownGracefully()report the state of the shutdown. IfGracefulShutdownCapable.shutdownGracefully()has not been called the behavior of this method is undefined.- Specified by:
reportActiveTasksin interfaceGracefulShutdownCapable- Returns:
- The current number of still-active tasks before the shutdown completes, or
Optional.empty()if no state can be reported
-
onApplicationEvent
Description copied from interface:ApplicationEventListenerHandle an application event.- Specified by:
onApplicationEventin interfaceApplicationEventListener<RefreshEvent>- Parameters:
event- the event to respond to
-
buildEmbeddedChannel
Builds Embedded Channel.- Parameters:
ssl- whether to enable SSL- Returns:
- The embedded channel with our server handlers
-
buildEmbeddedChannel
@Internal public void buildEmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel prototype, boolean ssl) Builds Embedded Channel.- Parameters:
prototype- The embedded channel to add our handlers tossl- whether to enable SSL
-