use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project pancm_project by xuwujing.
the class NettyServerDemo4 method start.
/**
* Start.
*/
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new StringDecoder());
// 绑定自定义业务逻辑
p.addLast(new NettyServerHandlerDemo4());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收进来的连接
ChannelFuture future = sbs.bind(port).sync();
System.out.println("Netty服务端启动成功,端口为: " + port);
// 释放监听
future.channel().closeFuture().sync();
} catch (Exception e) {
// 释放资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project uavstack by uavorg.
the class AbstractHttpServiceComponent2 method start.
@Override
public void start(int port, int backlog, int listenThreadCount, int handleThreadCount, boolean forceExit) {
EventLoopGroup bossGroup = new NioEventLoopGroup(listenThreadCount);
EventLoopGroup workerGroup = new NioEventLoopGroup(handleThreadCount);
server = new ServerBootstrap();
server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port).option(ChannelOption.SO_BACKLOG, backlog).childHandler(new HttpServerInitializer(this));
try {
this.host = NetworkHelper.getLocalIP();
this.port = port;
// Start the server.
server.bind().sync();
if (log.isTraceEnable()) {
log.info(this, "HttpServiceComponent[" + this.cName + "] for feature[" + this.feature + "] started SUCCESS: port=" + this.port);
}
} catch (Exception e) {
log.err(this, "HttpServiceComponent[" + this.cName + "] for feature[" + this.feature + "] starts FAIL.", e);
if (forceExit == true) {
System.exit(-1);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class ChannelBenchTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), false)) {
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), acceptor.isSsl(), 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class NetworkChannelTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
if (NetworkUtils.isEnableEpoolNative()) {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new EpollEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class NetworkChannelTest method testServerPushesData.
@Test
public void testServerPushesData() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setEnableRealNetwork(true);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
try {
ByteBuf msg = buildAckResponse(message);
// send a message to the client
ByteBuf buffer = buildAckRequest(900);
Pdu response = channel.sendMessageWithPduReply(900, buffer, 10000);
assertEquals(Pdu.TYPE_ACK, response.type);
// send the response to the client
channel.sendReplyMessage(message.messageId, msg);
message.close();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (TimeoutException ex) {
ex.printStackTrace();
}
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
CopyOnWriteArrayList<Long> pushedMessagesFromServer = new CopyOnWriteArrayList<>();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void requestReceived(Pdu pdu, Channel channel) {
pushedMessagesFromServer.add(pdu.messageId);
assertTrue(pdu.isRequest());
ByteBuf msg = buildAckResponse(pdu);
// send the response to the server
channel.sendReplyMessage(pdu.messageId, msg);
pdu.close();
}
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
ByteBuf buffer = buildAckRequest(134);
try (Pdu result = client.sendMessageWithPduReply(134, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
assertEquals(1, pushedMessagesFromServer.size());
} finally {
executor.shutdown();
}
}
}
Aggregations