Skip to content

Instantly share code, notes, and snippets.

@meteorcloudy
Created July 16, 2020 10:52
Show Gist options
  • Save meteorcloudy/a3704682ed60965cb360ae926ac705c0 to your computer and use it in GitHub Desktop.
Save meteorcloudy/a3704682ed60965cb360ae926ac705c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
mkdir netty-bug
cd netty-bug
wget https://repo1.maven.org/maven2/io/netty/netty-all/4.1.51.Final/netty-all-4.1.51.Final.jar -O netty-all.jar
cat > Example.java <<EOF
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.*;
import io.netty.channel.nio.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.*;
import java.net.InetSocketAddress;
public class Example {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
Class clazz;
if (args.length < 1) {
System.out.println("Use epoll");
bossGroup = new EpollEventLoopGroup(1);
workerGroup = new EpollEventLoopGroup();
clazz = EpollServerSocketChannel.class;
} else {
System.out.println("Use nio");
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
clazz = NioServerSocketChannel.class;
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(clazz)
.childHandler(
new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {}
});
ChannelFuture f = b.bind(new InetSocketAddress("[::1]", 0)).sync();
f.channel().closeFuture().sync();
}
}
EOF
javac -cp netty-all.jar Example.java
# Use NIO
java -cp netty-all.jar:. Example nio
# Use Epoll
java -cp netty-all.jar:. Example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment