今天在翻看netty的源码的时候发现netty对EventLoopGroup的实现有不止常用的NIOEventLoopGroup ,一共有以下几种。

  • EpollEventLoopGroup
  • NioEventLoopGroup
  • KQueueEventLoopGroup

  其中NioEventLoopGroup则是我们比较常用的,这个使用了java NIO中的SelectorProvider.provider()来选择系统默认的selectorProvider(即多路复用IO的支持)。

public NioEventLoopGroup(ThreadFactory threadFactory) {
this(0, threadFactory, SelectorProvider.provider());
}

而JDK则会根据自己的版本来返回默认的。

    public static SelectorProvider provider() {
synchronized (lock) {
if (provider != null)
return provider;
return AccessController.doPrivileged(
new PrivilegedAction<SelectorProvider>() {
public SelectorProvider run() {
if (loadProviderFromProperty())
return provider;
if (loadProviderAsService())
return provider;
provider = sun.nio.ch.DefaultSelectorProvider.create();
return provider;
}
});
}
}

  如果我们没有在参数中指定,那么provider = sun.nio.ch.DefaultSelectorProvider.create();这句代码则会找到当前jdk版本默认的selectorProvider。 不同的系统对应的默认selectorProvider不一样。
oracle jdk也会根据发行的不同操作系统的版本来提供不同的selectorProvider实现 例如windows的
则是WindowsSelectorProvider (注意本文是以oracle jdk为例,open jdk这儿的实现是不一样的)

public class DefaultSelectorProvider {
private DefaultSelectorProvider() {
} public static SelectorProvider create() {
return new WindowsSelectorProvider();
}
}

  而BSD/MAC OS则是KQueue->KQueueSelectorProvider ,linux则是Epoll->EPollSelectorProvider(注意linux 2.5.44以后才提供,之前均为select/poll) 。而java NIO会根据jvm的版本来选择合适的selectorProvider,使用这个是确保不会出现系统

切换的问题,即java跨平台的特性还是得到保证的。那netty为何还要多此一举的为EPollSelectorProvider和KQueueSelectorProvider单独提供对应的EventLoopGroup即EpollEventLoopGroup和KQueueEventLoopGroup呢。要知道手动指定会有操作系统不匹配的风险,例如如下代码

public class Server {

    public static void main(String[] args) throws InterruptedException {

        ServerBootstrap serverBootstrap = new ServerBootstrap();
ChannelFuture channelFuture = serverBootstrap.group(new EpollEventLoopGroup(1) , new EpollEventLoopGroup(10))
.channel(EpollServerSocketChannel.class)
.handler(new LoggingHandler())
.childHandler(new InitialierHandler())
.bind(8080)
.sync();
channelFuture.channel().closeFuture().sync();
}
}

  如果这段代码在windows或者mac os上运行则会报本文标题上的错  (将其中EpollEnventLoopGroup换为NIOEventLoopGroup,EpollServerSocketChannel 换为NIOsERVERSocketChannel则可以平台通用)

Exception in thread "main" java.lang.UnsatisfiedLinkError: failed to load the required native library
at io.netty.channel.epoll.Epoll.ensureAvailability(Epoll.java:80)
at io.netty.channel.epoll.EpollEventLoop.<clinit>(EpollEventLoop.java:51)
at io.netty.channel.epoll.EpollEventLoopGroup.newChild(EpollEventLoopGroup.java:150)
at io.netty.channel.epoll.EpollEventLoopGroup.newChild(EpollEventLoopGroup.java:35)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:59)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:112)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:99)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:76)
at io.netty.channel.epoll.EpollEventLoopGroup.<init>(EpollEventLoopGroup.java:52)
at netty.Server.main(Server.java:22)
Caused by: java.lang.ExceptionInInitializerError
at io.netty.channel.epoll.Epoll.<clinit>(Epoll.java:39)
... 12 more
Caused by: java.lang.IllegalStateException: Only supported on Linux
at io.netty.channel.epoll.Native.loadNativeLibrary(Native.java:225)
at io.netty.channel.epoll.Native.<clinit>(Native.java:58)
... 13 more

  俗话说,有风险,肯定就有收益。netty单独提供的epoll/KQueue实现肯定是有性能上的优化的。这儿可以引用一下官方文档,地址:https://netty.io/wiki/native-transports.html

  其中大概的解释了下原因。

  即相比于java NIO,提供了更多的特定平台的功能,产生更少的垃圾,总体上提高了性能。java JDK因为要保证跨平台所以在功能上必须做出妥协

  

    所以我们在构建项目的时候可以根据项目指定环境的不同来指定不同的EnventLoopGroup提升性能。例如开发环境 windows可以使用java的NIO ,mac可以使用netty定制的KqueueSelector.

  生产环境一般为linux则可以使用netty定制的EpollSelector来提高负载性能

netty笔记-:EpollEventLoopGroup:Caused by: java.lang.ExceptionInInitializerError:Caused by: java.lang.IllegalStateException: Only supported on Linux的更多相关文章

  1. java.lang.ExceptionInInitializerError Caused by: org.hibernate.InvalidMappingException: Unable to read XML

    此错误是说无法读取你的xml文档,于是我们就该去更改xml文档,因为我是自动生成的,所以我找了一份之前手写的,发现是dtd错了,把之前的dtd拷贝过来之后程序就测试通过了

  2. java.lang.ExceptionInInitializerError异常

    今天在开发的过程中,遇到java.lang.ExceptionInInitializerError异常,百度查了一下,顺便学习学习,做个笔记 静态初始化程序中发生意外异常的信号,抛出Exception ...

  3. Exception in thread "main" java.lang.ExceptionInInitializerError

    Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...

  4. springboot下jar包方式运行Caused by: java.lang.ExceptionInInitializerError: null

    idea调试过程中不会出现此问题,异常如下 org.springframework.beans.factory.BeanCreationException: Error creating bean w ...

  5. java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

    java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...

  6. java.lang.ExceptionInInitializerError

    java.lang.ExceptionInInitializerError at com.csdhsm.compiler.test.DevTest.testReadInput(DevTest.java ...

  7. android java.lang.ExceptionInInitializerError

    11-08 13:36:05.108: E/AndroidRuntime(5318): java.lang.ExceptionInInitializerError 11-08 13:36:05.108 ...

  8. Java java.lang.ExceptionInInitializerError 错误解决方案

    引起 java.lang.ExceptionInInitializerError 错误的原因是:在类的初始化时,出错.也就是说,在加载类时,执行static的属性.方法块时,出错了. 比如 publi ...

  9. java含有静态代码块新建的时候报错java.lang.ExceptionInInitializerError

    问题描述 最近在写一些单元测试用例,为了避免连接外界服务,所有选择mock了数据库Dao层,计划将数据库所需要的数据存在List中,在类加载的时候初始化List并且填充数据.代码如下: public ...

随机推荐

  1. altair package and altair_viewer

    pip install altair pip install altair_viewer Altair is a declarative statistical visualization libra ...

  2. docker使用nginx实现ssl(https)反向代理其他容器应用

    安装nginx容器 搜索nginx镜像 docker search nginx 拉取最新版nginx docker pull nginx:latest 运行容器 docker run --name=n ...

  3. JVM&GC详解

    1.JVM简介 JVM是java的核心和基础,在java编译器和os平台之间的虚拟处理器.它是一种利用软件方法实现的抽象的计算机基于下层的操作系统和硬件平台,可以在上面执行java的字节码程序. ja ...

  4. Python 实现深度学习

    前言 最近由于疫情被困在家,于是准备每天看点专业知识,准备写成博客,不定期发布. 博客大概会写5~7篇,主要是"解剖"一些深度学习的底层技术.关于深度学习,计算机专业的人多少都会了 ...

  5. 普及C组第三题(8.12)

    2304. 光芒 (File IO): input:light.in output:light.out 时间限制: 1000 ms  空间限制: 题目: 输入: 输出: 样例输入 5 1 1 0 1 ...

  6. mini-batch是什么 以及dataloader的作用

    mini-batch是什么 以及dataloader的作用 待办 我们在训练神经网络时,使用的是mini-batch(一次输入多张图片),所以我们在使用一个叫DataLoader的工具为我们将5000 ...

  7. 路飞-后台xadmin配置

    xadmin后台管理 安装:luffy虚拟环境下 # >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 ...

  8. bzoj3626: [LNOI2014]LCA (树链剖分)

    很神奇的方法 感觉是有生之年都想不到正解的这种 考虑对i 到根的节点权值 + 1,则从根到z的路径和就是lca(i,z)的深度 所以依次把0 ~ n - 1的点权值 + 1 对于询问[l, r] 这个 ...

  9. JDK8新特性---stream流

    项目上用到了stream流,找篇blog,转载一下,介绍下Stream流的用法. 1 流概述  流是 JDK8 新增的成员,允许以声明性方式处理数据集合,可以把 Stream 流看作是遍历数据集合的一 ...

  10. HTML学习(17)URL

    HTML 统一资源定位器(Uniform Resource Locators) URL - 统一资源定位器 Web浏览器通过URL从Web服务器请求页面. scheme://host.domain:p ...