最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java搭建簡單Netty開發(fā)環(huán)境入門教程

 更新時(shí)間:2021年06月25日 17:10:01   作者:xiahb_jp  
這篇文章主要介紹了Java搭建簡單Netty開發(fā)環(huán)境入門教程,有詳細(xì)的代碼展示和maven依賴,能夠幫助你快速上手Netty開發(fā)框架,需要的朋友可以參考下

下面就是準(zhǔn)備Netty的jar包了,如果你會(huì)maven的話自然是使用maven最為方便了。只需要在pom文件中導(dǎo)入以下幾行

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.16.Final</version>
    </dependency>

當(dāng)然啦,不會(huì)maven的也不用愁,可以在官網(wǎng)直接下載jar包,點(diǎn)擊跳轉(zhuǎn)。并在編輯器中將下載的jar包引入你的lib中,就可以愉快的開始Netty開發(fā)了

下面貼一個(gè)簡單的netty案例

一、 服務(wù)端代碼

1. EchoServerHandler.java

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
 
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter{
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //將客戶端傳入的消息轉(zhuǎn)換為Netty的ByteBuf類型
        ByteBuf in = (ByteBuf) msg;
 
        // 在控制臺(tái)打印傳入的消息
        System.out.println(
                "Server received: " + in.toString(CharsetUtil.UTF_8)
        );
        //將接收到的消息寫給發(fā)送者,而不沖刷出站消息
        ctx.write(in);
    }
 
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // 將未處決消息沖刷到遠(yuǎn)程節(jié)點(diǎn), 并且關(guān)閉該Channel
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                .addListener(ChannelFutureListener.CLOSE);
    }
 
    /**
     * 異常處理
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //打印異常棧跟蹤
        cause.printStackTrace();
 
        // 關(guān)閉該Channel
        ctx.close();
    }
}

2. EchoServer.java

import io.netty.bootstrap.ServerBootstrap;
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.NioServerSocketChannel;
 
import java.net.InetSocketAddress;
 
public class EchoServer {
    private final static int port = 8080;
 
    public static void main(String[] args) {
        start();
    }
 
    private static void start() {
        final EchoServerHandler serverHandler = new EchoServerHandler();
        // 創(chuàng)建EventLoopGroup
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        // 創(chuàng)建EventLoopGroup
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                //指定所使用的NIO傳輸Channel
        .channel(NioServerSocketChannel.class)
                //使用指定的端口設(shè)置套接字地址
        .localAddress(new InetSocketAddress(port))
                // 添加一個(gè)EchoServerHandler到Channle的ChannelPipeline
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                //EchoServerHandler被標(biāo)注為@shareable,所以我們可以總是使用同樣的案例
                socketChannel.pipeline().addLast(serverHandler);
            }
        });
 
        try {
            // 異步地綁定服務(wù)器;調(diào)用sync方法阻塞等待直到綁定完成
            ChannelFuture f = b.bind().sync();
            // 獲取Channel的CloseFuture,并且阻塞當(dāng)前線程直到它完成
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 優(yōu)雅的關(guān)閉EventLoopGroup,釋放所有的資源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

二、 客戶端代碼

1. EchoClientHandler.java

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
 
@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //當(dāng)被通知Channel是活躍的時(shí)候,發(fā)送一條消息
        ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
    }
 
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        System.out.println(
                "Client received: " + byteBuf.toString(CharsetUtil.UTF_8)
        );
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

2. EchoClient.java

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;
 
import java.net.InetSocketAddress;
 
public class EchoClient {
    private final static String HOST = "localhost";
    private final static int PORT = 8080;
 
    public static void start() {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .remoteAddress(new InetSocketAddress(HOST, PORT))
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new EchoClientHandler());
                    }
                });
        try {
            ChannelFuture f = bootstrap.connect().sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
 
    public static void main(String[] args) {
        start();
    }
}

先后運(yùn)行EchoServer.java和EchoClient.java.如果控制臺(tái)分別打印了

Server received: Netty rocks!

Client received: Netty rocks!

那么恭喜你,你已經(jīng)可以開始netty的開發(fā)了。

點(diǎn)擊查看Netty結(jié)合Protobuf編解碼

到此這篇關(guān)于Java搭建簡單Netty開發(fā)環(huán)境入門教程的文章就介紹到這了,更多相關(guān)Java搭建Netty環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java整合onlyoffice的各種踩坑記錄

    java整合onlyoffice的各種踩坑記錄

    這篇文章主要給大家介紹了關(guān)于java整合onlyoffice的各種踩坑,OnlyOffice是一種強(qiáng)大的在線協(xié)作軟件,專為企業(yè)和個(gè)人設(shè)計(jì),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • JAVA 對(duì)50取余數(shù)的五種方法試下

    JAVA 對(duì)50取余數(shù)的五種方法試下

    在數(shù)學(xué)計(jì)算中經(jīng)常會(huì)遇到余數(shù),本文主要介紹了JAVA 對(duì)50取余數(shù)的五種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • Spring中的StopWatch記錄操作時(shí)間代碼實(shí)例

    Spring中的StopWatch記錄操作時(shí)間代碼實(shí)例

    這篇文章主要介紹了Spring中的StopWatch記錄操作時(shí)間代碼實(shí)例,spring-framework提供的一個(gè)StopWatch類可以做類似任務(wù)執(zhí)行時(shí)間控制,也就是封裝了一個(gè)對(duì)開始時(shí)間,結(jié)束時(shí)間記錄操作的Java類,需要的朋友可以參考下
    2023-11-11
  • Flink實(shí)戰(zhàn)之實(shí)現(xiàn)流式數(shù)據(jù)去重

    Flink實(shí)戰(zhàn)之實(shí)現(xiàn)流式數(shù)據(jù)去重

    流式數(shù)據(jù)是一種源源不斷產(chǎn)生的數(shù)據(jù),本文探索了一種流式大數(shù)據(jù)的實(shí)時(shí)去重方法,不一定適用于所有場(chǎng)景,不過或許可以給面對(duì)相似問題的你一點(diǎn)點(diǎn)啟發(fā),
    2025-03-03
  • java中為什么要謹(jǐn)慎使用Arrays.asList、ArrayList的subList

    java中為什么要謹(jǐn)慎使用Arrays.asList、ArrayList的subList

    這篇文章主要介紹了java中為什么要謹(jǐn)慎使用Arrays.asList、ArrayList的subList,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 詳解Spring系列之@ComponentScan自動(dòng)掃描組件

    詳解Spring系列之@ComponentScan自動(dòng)掃描組件

    這篇文章主要介紹了Spring @ComponentScan自動(dòng)掃描組件使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Java 批量文件壓縮導(dǎo)出并下載到本地示例代碼

    Java 批量文件壓縮導(dǎo)出并下載到本地示例代碼

    這篇文章主要介紹了Java 批量文件壓縮導(dǎo)出并下載到本地示例代碼,實(shí)現(xiàn)思路首先要把zip流寫入到http響應(yīng)輸出流中,再把excel的流寫入zip流中,具體示例代碼,大家通過本文學(xué)習(xí)吧
    2017-12-12
  • Springboot?實(shí)現(xiàn)Server-Sent?Events的項(xiàng)目實(shí)踐

    Springboot?實(shí)現(xiàn)Server-Sent?Events的項(xiàng)目實(shí)踐

    本文介紹了在Spring?Boot中實(shí)現(xiàn)Server-Sent?Events(SSE),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • SpringBoot與Redis的令牌主動(dòng)失效機(jī)制實(shí)現(xiàn)

    SpringBoot與Redis的令牌主動(dòng)失效機(jī)制實(shí)現(xiàn)

    本文詳細(xì)介紹了基于SpringBoot和Redis實(shí)現(xiàn)令牌主動(dòng)失效機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • 使用java?-jar命令啟動(dòng)Spring?Boot應(yīng)用時(shí)指定特定配置文件的幾種實(shí)現(xiàn)方式

    使用java?-jar命令啟動(dòng)Spring?Boot應(yīng)用時(shí)指定特定配置文件的幾種實(shí)現(xiàn)方式

    這篇文章主要介紹了在使用java-jar命令啟動(dòng)SpringBoot應(yīng)用時(shí),指定特定配置文件的幾種方式,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2025-01-01

最新評(píng)論

昆山市| 苗栗县| 玉龙| 泌阳县| 宝山区| 惠东县| 清镇市| 桦川县| 菏泽市| 神农架林区| 靖远县| 陆川县| 昌宁县| 宜章县| 青冈县| 德令哈市| 吉隆县| 浠水县| 平武县| 宜良县| 祁东县| 罗江县| 东乡族自治县| 栖霞市| 九龙城区| 宝兴县| 罗甸县| 定南县| 安义县| 宜川县| 闻喜县| 枣庄市| 湘潭县| 吉木乃县| 景宁| 宿迁市| 长岛县| 枣阳市| 惠东县| 龙陵县| 渝北区|