腾讯java后端面试题,Netty入门 -- 什么是Netty,25岁成功入职阿里P7的小哥哥告诉你

        channelFuture.channel().closeFuture().sync();

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        //优雅关闭

        boosGroup.shutdownGracefully();

        workerGroup.shutdownGracefully();

    }



}

}




**NettyServerHandler**



服务器处理器,处理客户端发送的消息并输出到控制台,并向服务端发送消息



package com.wanshi.netty.simple;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

import java.util.concurrent.TimeUnit;

/**

  • 自定义一个Handler,需要继承netty规定好的某个HandlerAdapter

  • 这时我们自定义的handler才能称为一个handler

*/

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

//读取数据事件(这里我们可以读取客户端发送的消息)



/**

 * 1.ChannelHandlerContext ctx: 上下文对象,含有 管道pipeline,通道channel,地址

 * 2.Object msg:就是客户端发送的数据,默认Object

 * @param ctx

 * @param msg

 * @throws Exception

 */

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    System.out.println("server ctx =" + ctx);

    //将 msg 转成一个ByteBuf

// ByteBuf buf = (ByteBuf) msg;

// System.out.println(“客户端发送消息是:” + buf.toString(CharsetUtil.UTF_8));

// System.out.println(“客户端地址:” + ctx.channel().remoteAddress());

    //自定义普通任务队列,将耗时长的任务加入队列,定义到NioEventLoop --> taskQueue

    ctx.channel().eventLoop().execute(new Runnable() {

        @Override

        public void run() {

            try {

                Thread.currentThread().sleep(10 * 1000);

                ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端:喵2~", CharsetUtil.UTF_8));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    });



    ctx.channel().eventLoop().execute(new Runnable() {

        @Override

        public void run() {

            try {

                Thread.currentThread().sleep(20 * 1000);

                ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端:喵3~", CharsetUtil.UTF_8));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    });



    //用户自定义定时任务 --》 该任务是提交到 scheduleQueue中

    ctx.channel().eventLoop().schedule(new Runnable() {

        @Override

        public void run() {

            try {

                Thread.currentThread().sleep(5 * 1000);

                ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端:喵4~", CharsetUtil.UTF_8));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }, 5, TimeUnit.SECONDS);



    System.out.println("go ~");

}



/**

 * 数据读取完毕

 * @param ctx

 * @throws Exception

 */

@Override

public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

    //writeAndFlush 是 write+flush

    //将数据写入到缓存,并刷新

    //一般讲,需要对发送的数据进行编码

    ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端:喵1~", CharsetUtil.UTF_8));

}



//处理异常,一般是需要关闭通道

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    ctx.close();

}

}




**NettyClient**



客户端,用于连接服务器



package com.wanshi.netty.simple;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {

public static void main(String[] args) throws Exception {

    //客户端需要一个事件循环组

    EventLoopGroup eventExecutors = new NioEventLoopGroup();

    try {

        //创建一个客户端启动对象

        //客户端使用的不是ServerGroup 而是Bootstrap

        Bootstrap bootstrap = new Bootstrap();



        //设置相关参数

        bootstrap.group(eventExecutors) //设置线程组

                .channel(NioSocketChannel.class) //设置客户端通道的实现类(反射)

                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override

                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                        socketChannel.pipeline().addLast(new NettyClientHandler()); //加入自己的处理器

                    }

                });



        System.out.println("客户端 is ok...");



        //启动客户端去连接服务器端, netty异步模型ChannelFuture

        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();

        //给关闭通道进行监听

        channelFuture.channel().closeFuture().sync();

    } finally {

        //优雅关闭线程池

        eventExecutors.shutdownGracefully();

    }



}

}




**NettyClientHandler**



客户端处理器,处理服务器发送的消息输出到控制台,并向服务器发送消息



package com.wanshi.netty.simple;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

MySQL全家桶笔记

还有更多面试复习笔记分享如下

Java架构专题面试复习

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

]

还有更多面试复习笔记分享如下

[外链图片转存中…(img-Y0AGTPap-1712534777290)]

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

  • 25
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值