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服务器端启动的更多相关文章

  1. 在CentOS7服务器端启动jupyter notebook服务,在windows端使用jupyter notebook,服务器充当后台计算云端

    在CentOS7服务器端启动jupyter notebook服务,在windows端使用jupyter notebook,服务器充当后台计算云端 在服务器端启动jupyter notebook服务,在 ...

  2. Netty Nio启动全流程

    Netty Nio启动全流程 1. 各组件之间的关系 说明:EventLoopGroup类似线程池,EventLoop为单线程,每个EventLoop关联一个Nio Selector,用于注册Chan ...

  3. 【Netty之旅四】你一定看得懂的Netty客户端启动源码分析!

    前言 前面小飞已经讲解了NIO和Netty服务端启动,这一讲是Client的启动过程. 源码系列的文章依旧还是遵循大白话+画图的风格来讲解,本文Netty源码及以后的文章版本都基于:4.1.22.Fi ...

  4. 网络编程Netty入门:Netty的启动过程分析

    目录 Netty的启动过程 Bootstrap 服务端的启动 客户端的启动 TCP粘包.拆包 图示 简单的例子 Netty编解码框架 Netty解码器 ByteToMessageDecoder实现类 ...

  5. 详细图解 Netty Reactor 启动全流程 | 万字长文 | 多图预警

    本系列Netty源码解析文章基于 4.1.56.Final版本 大家第一眼看到这幅流程图,是不是脑瓜子嗡嗡的呢? 大家先不要惊慌,问题不大,本文笔者的目的就是要让大家清晰的理解这幅流程图,从而深刻的理 ...

  6. netty 服务器端流程调度Flow笔记

    create NioEventLoopGroup Instance 一.NioServerSocketChannel init note:Initializing ChannelConfig crea ...

  7. 服务器端启动soket多线程

    方法一: Socket socket=null try{ ServerSocket serversocket=nwe ServerSocket(8080) while(true){ socket=se ...

  8. Netty源码分析 (三)----- 服务端启动源码分析

    本文接着前两篇文章来讲,主要讲服务端类剩下的部分,我们还是来先看看服务端的代码 /** * Created by chenhao on 2019/9/4. */ public final class ...

  9. Netty学习之服务器端创建

    一.服务器端开发时序图 图片来源:Netty权威指南(第2版) 二.Netty服务器端开发步骤 使用Netty进行服务器端开发主要有以下几个步骤: 1.创建ServerBootstrap实例 Serv ...

随机推荐

  1. localStorage sessionStorage 用法

    sessionStorage.getItem('key') // 获取 sessionStorage.setItem('key','value') //设置 sessionStorage.remove ...

  2. 自动生成构造方法、getter、setter

    前言 ​ 一直很想知道IDE是如何自动去生成类的构造和getter.setter方法,大概是有个想法,获取当前的类,利用反射去获取属性名和类型,然后生成,所以我想自己试试. 写个模板类 package ...

  3. 0 Python学习计划

    每天一小节,坚持水滴石穿. 2019-4-1 景略

  4. 在Airtest中如何使用无线模式控制手机

    在使用Airtest超快速开发App爬虫文章的最后,我们留了一个尾巴:如何启动Airtest的无线模式,不用USB线就能控制手机? 本文将会讲到具体的做法.做法分为两种:第一种是在Airtest的ID ...

  5. leetcode969

    class Solution(object): def pancakeSort(self, A: 'List[int]') -> 'List[int]': n = len(A) result = ...

  6. notepad++之删除空行

    正则表达式替换 查找目标: \r\n{0,1}[\s\t]*\r\n 替换为: \r\n 循环查找:勾选

  7. cobbler登录web界面时出现“Internal Server Error”

    当进行cobbler配置后,并进行web登录时,出现错误: 先查看其日志位置 #cat /etc/httpd/conf.d/ssl.conf 在其中位置发现其错误的日志位置为/etc/httpd/lo ...

  8. python3编码

    一.字符编码 1.什么实字符编码:将人识别的字符转换成计算机能识别的01,而转换的过程或者规则就是字符编码表. 而这种字符编码表表示了一种对应关系. 2.常用的字符编码表有:ascii.unicode ...

  9. Silverlight中验证码生成

    public class ValidationCode { Random r = new Random(DateTime.Now.Millisecond); /// <summary> / ...

  10. 小白安装openvas总结-原创20181213

    先对该工具进行熟悉: OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus ...