今天在翻看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. LED Holiday Light - Color Creation Of LED Christmas Lights

    In the long and interesting history of Christmas lights, nothing is brighter than light-emitting dio ...

  2. c#中的yield词法

    yield关键字的作用是将当前集合中的元素立即返回,实例: 通过断点可以看到,控制台每显示一个集合中的元素,都会到query方法中去取集合元素. 其实yield return是“语法糖”,其本质是生成 ...

  3. sql查询——子查询

    -- 子查询 -- 一句查询语句内,再套一句查询语句 ,叫子查询 -- 查询班级类身高最高的人的名字 select name from students where high=(select max( ...

  4. Unity 坐标系转换

    在Unity中我们通常会用到以下几个坐标系下的点: 世界坐标系:World Space 简单来讲,我们通过 transform.position | transform.rotation 获取得到的位 ...

  5. 结合字符串常量池/String.intern()/String Table来谈一下你对java中String的理解

    1.字符串常量池 每创建一个字符串常量,JVM会首先检查字符串常量池,如果字符串已经在常量池中存在,那么就返回常量池中的实例引用.如果字符串不在池中,就会实例化一个字符串放到字符串池中.常量池提高了J ...

  6. Java之字符串替换replace()

    replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的 import java.uti ...

  7. asmx 、Web Service、Web API

    asmx .Web Service.Web API asmx 是WEB服务文件 asmx.cs里有相关代码 属于B/S形式,用SOAP方式HTTP访问,用XML返回 可以返回基础类型和PUBLIC结构 ...

  8. 2.17NOIP模拟赛(by hzwer) T1 小奇挖矿

    [题目背景] 小奇要开采一些矿物,它驾驶着一台带有钻头(初始能力值 w)的飞船,按既定 路线依次飞过喵星系的 n 个星球. [问题描述] 星球分为 2 类:资源型和维修型. 1. 资源型:含矿物质量 ...

  9. Android Studio 初级控件笔记

    Android支持的像素单位 Android支持的像素单位有:px(像素).in(英寸).mm(毫米).pt(磅,1/72英寸).dp(与设备无关的显示单位).dip(就是dp).sp(用于设置字体大 ...

  10. 剑指offer 面试题38 字符串的排列

    我惯用的dfs模板直接拿来套 class Solution { public: vector<string> Permutation(string str) { if(str.empty( ...