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. winserver2012 下安装 sqlserver2008

    sqlserver2008 安装要求 winserver2012 必须装 .netframework3.5. 但是 winserver2012下,安装 .netframework3.5 ,必须要 wi ...

  2. 使用Git将本地文件提交到远程仓库

    一 操作准备条件: git远程仓库已经建好了,本地文件已经存在了,现在要将本地代码推到git远程仓库保存. 解决办法如下: 1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以 ...

  3. mysql修改root密码及修改密码过程中报错的解决方案

    参考网站: https://www.linuxidc.com/Linux/2018-05/152586.htmhttps://www.cnblogs.com/wangbaobao/p/7087032. ...

  4. (Python基础)简单购物车代码

    以下是最简单,最基础的购物车代码,一起学习,一起参考.product_list = [ ('Iphone',5800), ('Mac Pro',15800), ('car',580000), ('co ...

  5. ios监听静音键和音量键事件

    http://blog.csdn.net/slinloss/article/details/7870559

  6. 使用URLSearchParams处理axios发送的数据

    使用URLSearchParams处理axios发送的数据 在使用axios这个ajax插件的时候,我们有些时候会遇到一些问题,比如:数据格式不正确 以最简单的例子为基础(这里使用post方法): 在 ...

  7. Tesseract-OCR的使用记录

    参考: http://www.cnblogs.com/cnlian/p/5765871.html http://www.cnblogs.com/wzben/p/5930538.html 1.下载 Te ...

  8. mezzanine的breadcrumb

    page_menu tag为什么能渲染出当前页的面包屑路径??靠的是request的page对象,要是page的自己或是它的的祖先才渲染,是根据page的属性page.is_current_or_as ...

  9. Nginx与ftp服务器

    使用Nginx搭建ftp服务器

  10. 4-17疑难点 c语言之【结构体对齐】

    今天学习了结构体这一章节,了解到了结构体在分配内存的时候采取的是对齐的方式 例如: #include<stdio.h> struct test1 { int a; char b; shor ...