Class NettyTransport

java.lang.Object
org.mockserver.socket.NettyTransport

public final class NettyTransport extends Object
Transport-selection utility that picks the highest-performance Netty transport available on the current platform: Linux epoll when the native library is present, NIO everywhere else.

Epoll gives better throughput and lower latency on Linux, and is required for transparent-proxy SO_ORIGINAL_DST resolution (which needs EpollSocketChannel children). The selection is determined once at class-load time and cached; all methods are safe to call from any thread.

On non-Linux platforms (macOS, Windows) or when the opt-out flag useNativeTransport=false is set, everything transparently falls back to NIO with no behaviour change.

The class is placed in mockserver-core so both the server bootstrap (mockserver-netty) and the outbound HTTP client (NettyHttpClient in mockserver-core) can use it.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Class<? extends io.netty.channel.socket.DatagramChannel>
    datagramChannelClassFor(io.netty.channel.EventLoopGroup group)
    Returns the datagram channel class that is compatible with the given group.
    static boolean
    Returns true if the epoll native library is available on this platform, independent of the configuration opt-out flag.
    static io.netty.channel.EventLoopGroup
    newEventLoopGroup(int nThreads, ThreadFactory threadFactory, boolean useNativeTransport)
    Creates a new EventLoopGroup of the appropriate transport type.
    static Class<? extends io.netty.channel.ServerChannel>
    serverSocketChannelClassFor(io.netty.channel.EventLoopGroup group)
    Returns the server socket channel class that is compatible with the given group.
    static Class<? extends io.netty.channel.Channel>
    socketChannelClassFor(io.netty.channel.EventLoopGroup group)
    Returns the client socket channel class that is compatible with the given group.
    static boolean
    useNativeTransport(boolean useNativeTransport)
    Returns true if the native epoll transport should be used, considering both platform availability and the user's opt-out configuration flag.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • useNativeTransport

      public static boolean useNativeTransport(boolean useNativeTransport)
      Returns true if the native epoll transport should be used, considering both platform availability and the user's opt-out configuration flag.
      Parameters:
      useNativeTransport - the configuration flag (true = prefer native when available; false = force NIO)
      Returns:
      true to use epoll, false to use NIO
    • isEpollAvailable

      public static boolean isEpollAvailable()
      Returns true if the epoll native library is available on this platform, independent of the configuration opt-out flag.
    • newEventLoopGroup

      public static io.netty.channel.EventLoopGroup newEventLoopGroup(int nThreads, ThreadFactory threadFactory, boolean useNativeTransport)
      Creates a new EventLoopGroup of the appropriate transport type. If epoll is selected (per the config flag and platform availability), returns an EpollEventLoopGroup; otherwise returns a NioEventLoopGroup.

      If epoll group creation fails at runtime (e.g. native library load race), falls back to NIO gracefully.

      Parameters:
      nThreads - number of event loop threads
      threadFactory - thread factory for naming threads
      useNativeTransport - the configuration flag
      Returns:
      an event loop group — pass it to serverSocketChannelClassFor(io.netty.channel.EventLoopGroup), socketChannelClassFor(io.netty.channel.EventLoopGroup), or datagramChannelClassFor(io.netty.channel.EventLoopGroup) to obtain a channel class that is guaranteed to be compatible
    • serverSocketChannelClassFor

      public static Class<? extends io.netty.channel.ServerChannel> serverSocketChannelClassFor(io.netty.channel.EventLoopGroup group)
      Returns the server socket channel class that is compatible with the given group.

      Unlike the removed flag-based methods, this derives the channel class from the actual EventLoopGroup instance — so the two can never desync even when newEventLoopGroup(int, java.util.concurrent.ThreadFactory, boolean) fell back from epoll to NIO at runtime.

      Parameters:
      group - the EventLoopGroup the bootstrap will use
      Returns:
      EpollServerSocketChannel.class when the group is an EpollEventLoopGroup, NioServerSocketChannel.class otherwise
    • socketChannelClassFor

      public static Class<? extends io.netty.channel.Channel> socketChannelClassFor(io.netty.channel.EventLoopGroup group)
      Returns the client socket channel class that is compatible with the given group.

      This guarantees group/channel compatibility regardless of how the group was created (config-driven, caller-supplied NIO, or epoll fallback to NIO).

      Parameters:
      group - the EventLoopGroup the bootstrap will use
      Returns:
      EpollSocketChannel.class when the group is an EpollEventLoopGroup, NioSocketChannel.class otherwise
    • datagramChannelClassFor

      public static Class<? extends io.netty.channel.socket.DatagramChannel> datagramChannelClassFor(io.netty.channel.EventLoopGroup group)
      Returns the datagram channel class that is compatible with the given group.

      Used by the DNS mock server bootstrap to pick the correct datagram channel for whatever event loop group it is registered with.

      Parameters:
      group - the EventLoopGroup the bootstrap will use
      Returns:
      EpollDatagramChannel.class when the group is an EpollEventLoopGroup, NioDatagramChannel.class otherwise