netty服务器端启动
package com.imooc.netty.ch3; import com.imooc.netty.ch6.AuthHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.AttributeKey; /**
* @author
*/
public final class Server { public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, true)
.childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
.handler(new ServerHandler())
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new AuthHandler());
//.. }
}); ChannelFuture f = b.bind().sync(); f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package com.imooc.netty.ch3; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.concurrent.TimeUnit; public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("channelActive");
} @Override
public void channelRegistered(ChannelHandlerContext ctx) {
System.out.println("channelRegistered");
} @Override
public void handlerAdded(ChannelHandlerContext ctx) {
System.out.println("handlerAdded");
} @Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg); new Thread(new Runnable() {
@Override
public void run() {
// 耗时的操作
String result = loadFromDB(); ctx.channel().writeAndFlush(result);
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
// ...
}
}, , TimeUnit.SECONDS); }
}).start();
} private String loadFromDB() {
return "hello world!";
}
}
1:服务端的socket 在哪里进行初始化
2:在哪进行accept连接




netty服务器端启动的更多相关文章
- 在CentOS7服务器端启动jupyter notebook服务,在windows端使用jupyter notebook,服务器充当后台计算云端
在CentOS7服务器端启动jupyter notebook服务,在windows端使用jupyter notebook,服务器充当后台计算云端 在服务器端启动jupyter notebook服务,在 ...
- Netty Nio启动全流程
Netty Nio启动全流程 1. 各组件之间的关系 说明:EventLoopGroup类似线程池,EventLoop为单线程,每个EventLoop关联一个Nio Selector,用于注册Chan ...
- 【Netty之旅四】你一定看得懂的Netty客户端启动源码分析!
前言 前面小飞已经讲解了NIO和Netty服务端启动,这一讲是Client的启动过程. 源码系列的文章依旧还是遵循大白话+画图的风格来讲解,本文Netty源码及以后的文章版本都基于:4.1.22.Fi ...
- 网络编程Netty入门:Netty的启动过程分析
目录 Netty的启动过程 Bootstrap 服务端的启动 客户端的启动 TCP粘包.拆包 图示 简单的例子 Netty编解码框架 Netty解码器 ByteToMessageDecoder实现类 ...
- 详细图解 Netty Reactor 启动全流程 | 万字长文 | 多图预警
本系列Netty源码解析文章基于 4.1.56.Final版本 大家第一眼看到这幅流程图,是不是脑瓜子嗡嗡的呢? 大家先不要惊慌,问题不大,本文笔者的目的就是要让大家清晰的理解这幅流程图,从而深刻的理 ...
- netty 服务器端流程调度Flow笔记
create NioEventLoopGroup Instance 一.NioServerSocketChannel init note:Initializing ChannelConfig crea ...
- 服务器端启动soket多线程
方法一: Socket socket=null try{ ServerSocket serversocket=nwe ServerSocket(8080) while(true){ socket=se ...
- Netty源码分析 (三)----- 服务端启动源码分析
本文接着前两篇文章来讲,主要讲服务端类剩下的部分,我们还是来先看看服务端的代码 /** * Created by chenhao on 2019/9/4. */ public final class ...
- Netty学习之服务器端创建
一.服务器端开发时序图 图片来源:Netty权威指南(第2版) 二.Netty服务器端开发步骤 使用Netty进行服务器端开发主要有以下几个步骤: 1.创建ServerBootstrap实例 Serv ...
随机推荐
- 秘制牛肉Alpha阶段项目展示
秘制牛肉Alpha阶段项目展示 1.团队成员和个人博客 · 左顺:"我是左顺,秘制牛肉队开发人员". · 王尖兵:"C,java,html5都会一点的菜鸡,没做过团队项目 ...
- {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
ElasticSearch-head 查询报 406错误码 {"error":"Content-Type header [application/x-www-form-u ...
- Java——对象的复制、克隆、序列化
原创作者: https://blog.csdn.net/lmb55/article/details/78277878对象克隆(复制)假如说你想复制一个简单变量.很简单: int apples = 5; ...
- LTS本地搭建详述
由于工作项目中使用LTS作为消息队列,这几天有空正好研究一下. 1. 先去GitHub上下载源码:https://github.com/ltsopensource/light-task-schedul ...
- 配置samba的流程
1.关闭防火墙和selinuxservice iptables stopsetenforce 02.配置本地yummount /dev/cdrom /mediacd /etc/yum.repos.dc ...
- vue项目中如何使用多语言(vue-i18n)
因项目需要,需要使用多语言,特此记录使用方法. 第一步:安装vue-i18n npm install vue-i18n 第二步:在生成的i18n文件夹中的index.js里引入vue-i18n 第三步 ...
- Android 开发 重写定位器类Timer与TimerTask
class AttendanceTimer extends Timer { private static final int LOCATION = 0x01; private static final ...
- command not found所有执行命令总是报找不到
输入 ll命令 提示: bash: ls: 未找到命令… 相似命令是: 'lz' 原因: 环境变量PATH被修改了 解决办法: 执行: export PATH=/bin:/usr/bin:$PATH ...
- 容器(docker)内运行Nginx
容器内运行nginx其实很简单,但是一开始还是浪费了我很多时间.这里写下来给大家省点时间. 1.创建nginx文件夹,放置各种配置及日志等. mkdir /docker/nginx docker 文件 ...
- spring boot 拦截异常 统一处理
spring boot 默认情况下会映射到 /error 进行异常处理,提示不友好,需要自定义异常处理,提供友好展示 1.自定义异常类(spring 对于 RuntimeException 异常才会进 ...