第二课 Netty服务端

来源:互联网 发布:酒瓶设计软件 编辑:程序博客网 时间:2024/06/03 06:30

netty应用的领域

1分布式进程通信
例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x

2、游戏服务器开发
最新的游戏服务器有部分公司可能已经开始采用netty4.x 或 netty5.x


代码示例

public class Server {    public static void main(String[] args) {        ServerBootstrap serverBootstrap = new ServerBootstrap();        ExecutorService boss = Executors.newCachedThreadPool();        ExecutorService worker = Executors.newCachedThreadPool();        //设置NioSocket工厂        serverBootstrap.setFactory(new NioServerSocketChannelFactory(boss,worker));        //设置管道工厂        serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {            @Override            public ChannelPipeline getPipeline() throws Exception {                //创建管道                ChannelPipeline pipeline = Channels.pipeline();                pipeline.addLast("decoder", new StringDecoder());                pipeline.addLast("encoder", new StringEncoder());                pipeline.addLast("ServerHandler", new ServerHandler());                return pipeline;            }        });        serverBootstrap.bind(new InetSocketAddress(8000));        System.out.println("NIOServer started!!!!!!!!!!!");    }}
public class ServerHandler extends SimpleChannelHandler{    @Override    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)            throws Exception {        System.out.println("channelClosed");        super.channelClosed(ctx, e);    }    @Override    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event)            throws Exception {        System.out.println("channelConnected"+event.getChannel().getRemoteAddress());        super.channelConnected(ctx, event);    }    @Override    public void channelDisconnected(ChannelHandlerContext ctx,            ChannelStateEvent e) throws Exception {        System.out.println("channelDisconnected");        super.channelDisconnected(ctx, e);    }    @Override    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)            throws Exception {        System.out.println("exceptionCaught");        super.exceptionCaught(ctx, e);    }    @Override    public void messageReceived(ChannelHandlerContext context, MessageEvent message)            throws Exception {        String buffer = (String) message.getMessage();        System.out.println("received message:"+buffer);        ChannelBuffer buffer2 = ChannelBuffers.copiedBuffer("Hi".getBytes());        context.getChannel().write(buffer2);        super.messageReceived(context, message);    }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 处处被小人其欠负怎么办??? 衣服开了一个口怎么办 脸上长痘痘留下的坑怎么办 脸部被打得皮肤怎么办 打脸引起耳朵疼怎么办 被打了耳痛耳鸣怎么办 苹果6视频锁屏怎么办 抠耳朵抠疼了怎么办 图库的相片没了怎么办 遇到打假牌的人怎么办 部队保障卡丢了怎么办 廊坊武警学院取消现役学员怎么办 孩子去当兵联系不上怎么办 军训戴眼镜晒痕怎么办 想进部队体检没过怎么办 大腿跟小腿不直怎么办 腿被车门夹了怎么办 脚出汗穿凉鞋滑怎么办 玩游戏手出汗屏幕滑怎么办 新买的鞋子臭怎么办 当公民利益受到侵犯怎么办 唇钉里面长肉怎么办 宝宝舔了一口酒怎么办 头被玻璃门撞了怎么办 30多了还一事无成 未来怎么办 27岁失业了该怎么办 无业证明不给开怎么办 典型的缺乏运动的肥胖怎么办 30岁了不想结婚怎么办 专家解释欠30万怎么办 欠医院十几万钱怎么办 当你迷茫的时候怎么办 被骗了一年的积蓄怎么办 当兵身高视力都不够怎么办 当兵中途不想当了怎么办 当兵后不想当了怎么办 在泰国想剪头发怎么办 省二证书丢了怎么办 职称计算机级别报错怎么办 科一不会用电脑怎么办 西安科目三挂了怎么办