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>
,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
Fields 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_UPGRADE
Fields inherited from interface io.micronaut.core.order.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Fields inherited from interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener
DEFAULT_POSITION
-
Constructor Summary
ConstructorDescriptionNettyHttpServer
(NettyHttpServerConfiguration serverConfiguration, NettyEmbeddedServices nettyEmbeddedServices, boolean isDefault) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChannel
(io.netty.channel.Channel channel) Adds a channel to the repository.io.netty.channel.embedded.EmbeddedChannel
buildEmbeddedChannel
(boolean ssl) Builds Embedded Channel.void
buildEmbeddedChannel
(io.netty.channel.embedded.EmbeddedChannel prototype, boolean ssl) Builds Embedded Channel.protected io.netty.channel.EventLoopGroup
protected io.netty.bootstrap.ServerBootstrap
protected io.netty.channel.EventLoopGroup
createWorkerEventLoopGroup
(@Nullable EventLoopGroupConfiguration workerConfig) void
doOnConnect
(@NonNull ChannelPipelineListener listener) A hook to customize the pipeline upon establishing a connection.Resolves theApplicationContext
for this class.Gets the set of all ports this Netty server is bound to.io.netty.channel.group.ChannelGroup
Returns theChannelGroup
used to store WebSocketSessions.getHost()
Returns the set of observed configuration prefixes that the event listener should listen for.int
getPort()
getURI()
getURL()
boolean
boolean
Most servers provide a way to block such that the server doesn't exit, however some require the creation of a keep alive thread.boolean
void
onApplicationEvent
(RefreshEvent event) Handle an application event.void
register
(@NonNull NettyServerCustomizer customizer) Register a new customizer with this server.void
removeChannel
(io.netty.channel.Channel channel) Remove a channel from the repository.start()
Starts the lifecyle component.stop()
Stops the life cycle component.Stops the Netty instance, but keeps the ApplicationContext running.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.http.netty.channel.ChannelPipelineCustomizer
isServerChannel
Methods inherited from interface io.micronaut.runtime.EmbeddedApplication
getEnvironment, isForceExit
Methods inherited from interface io.micronaut.runtime.server.EmbeddedServer
isServer
Methods 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:EmbeddedServer
Most 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:
isKeepAlive
in 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:LifeCycle
Starts the lifecyle component.- Specified by:
start
in interfaceApplicationContextLifeCycle<EmbeddedServer>
- Specified by:
start
in interfaceLifeCycle
- Specified by:
start
in interfaceNettyEmbeddedServer
- Returns:
- This lifecycle component
-
stop
Description copied from interface:LifeCycle
Stops the life cycle component.- Specified by:
stop
in interfaceApplicationContextLifeCycle<EmbeddedServer>
- Specified by:
stop
in interfaceLifeCycle
- Specified by:
stop
in interfaceNettyEmbeddedServer
- Returns:
- This lifecycle component
-
stopServerOnly
Description copied from interface:NettyEmbeddedServer
Stops 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:
stopServerOnly
in interfaceNettyEmbeddedServer
- Returns:
- The stopped NettyEmbeddedServer
-
register
Description copied from interface:NettyServerCustomizer.Registry
Register 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:
register
in interfaceNettyEmbeddedServer
- Specified by:
register
in interfaceNettyServerCustomizer.Registry
- Parameters:
customizer
- The customizer to register.
-
getPort
public int getPort()- Specified by:
getPort
in interfaceEmbeddedServer
- Returns:
- The port exposed by the server
-
getHost
- Specified by:
getHost
in interfaceEmbeddedServer
- Returns:
- The host of the server
-
getScheme
- Specified by:
getScheme
in interfaceEmbeddedServer
- Returns:
- The scheme of the server (http/https)
-
getURL
- Specified by:
getURL
in interfaceEmbeddedServer
- Returns:
- The full URL to the server
-
getURI
- Specified by:
getURI
in interfaceEmbeddedServer
- Returns:
- The full URI to the server
-
getContextURI
- Specified by:
getContextURI
in interfaceEmbeddedServer
- Returns:
- The full URI to the server with the context path
-
getApplicationContext
Description copied from interface:ApplicationContextProvider
Resolves theApplicationContext
for this class.- Specified by:
getApplicationContext
in interfaceApplicationContextProvider
- Specified by:
getApplicationContext
in interfaceEmbeddedApplication<EmbeddedServer>
- Returns:
- The
ApplicationContext
for the server
-
getApplicationConfiguration
- Specified by:
getApplicationConfiguration
in interfaceEmbeddedApplication<EmbeddedServer>
- Returns:
- The
ApplicationConfiguration
instance
-
getBoundPorts
Description copied from interface:NettyEmbeddedServer
Gets the set of all ports this Netty server is bound to.- Specified by:
getBoundPorts
in 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
-
addChannel
public void addChannel(io.netty.channel.Channel channel) Description copied from interface:WebSocketSessionRepository
Adds a channel to the repository.- Specified by:
addChannel
in interfaceWebSocketSessionRepository
- Parameters:
channel
- The channel
-
removeChannel
public void removeChannel(io.netty.channel.Channel channel) Description copied from interface:WebSocketSessionRepository
Remove a channel from the repository.- Specified by:
removeChannel
in interfaceWebSocketSessionRepository
- Parameters:
channel
- The channel
-
getChannelGroup
public io.netty.channel.group.ChannelGroup getChannelGroup()Description copied from interface:WebSocketSessionRepository
Returns theChannelGroup
used to store WebSocketSessions.- Specified by:
getChannelGroup
in interfaceWebSocketSessionRepository
- Returns:
- A
ChannelGroup
-
getWebSocketSessionRepository
- Returns:
NettyHttpServer
which implementsWebSocketSessionRepository
-
isClientChannel
public boolean isClientChannel()- Specified by:
isClientChannel
in interfaceChannelPipelineCustomizer
- Returns:
- Is this customizer the client.
-
doOnConnect
Description copied from interface:ChannelPipelineCustomizer
A hook to customize the pipeline upon establishing a connection.- Specified by:
doOnConnect
in interfaceChannelPipelineCustomizer
- Parameters:
listener
- The listener The listener.
-
getObservedConfigurationPrefixes
Description copied from interface:RefreshEventListener
Returns the set of observed configuration prefixes that the event listener should listen for.- Specified by:
getObservedConfigurationPrefixes
in interfaceRefreshEventListener
- Returns:
- A set of prefixes
-
onApplicationEvent
Description copied from interface:ApplicationEventListener
Handle an application event.- Specified by:
onApplicationEvent
in 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
-