码迷,mamicode.com
首页 > Web开发 > 详细

Netty消息传递

时间:2020-05-01 12:59:47      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:main   over   span   art   coder   actor   服务   mes   star   

知识点

1、消息如何在管道中流转 ,一个管道中会有多个handler,当前的一个handler如何往下面的一个handler传递一个对象

  主要通过handler往下传递对象的方法是sendUpstream(event)

2、看下粘包和分包是怎么样一个情况
  hello hello
  通过定义一个稳定的结构 length + hello

1、为什么FrameDecoder return的对象就是往下传递的对象  (还是调用了sendUpstream)

2、buffer里面数据未被读取完怎么办?    (cumulation缓存)

3、为什么return null就可以缓存buffer     (cumulation缓存)

=============================分割线===========================

3、FrameDecoder里面的cumulation其实就是一个缓存的buffer对象

  包头+长度+数据

  把长度定义的很大,这种数据包,通常被称为socket攻击,字节流式攻击

1、Client.java

package com.example.netty.lesson11.pipeLine;

import java.net.Socket;

public class Client {

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

        Socket socket = new Socket("127.0.0.1", 51503);
        
        socket.getOutputStream().write("hello".getBytes());
        
        socket.close();
    }

}

2、Server.java

package com.example.netty.lesson11.pipeLine;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
 * 消息传递
 */
public class Server {

    public static void main(String[] args) {
        //服务类
        ServerBootstrap bootstrap = new ServerBootstrap();
        
        //boss线程监听端口,worker线程负责数据读写
        ExecutorService boss = Executors.newCachedThreadPool();
        ExecutorService worker = Executors.newCachedThreadPool();
        
        //设置niosocket工厂
        bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));
        
        //设置管道的工厂
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            
            @Override
            public ChannelPipeline getPipeline() throws Exception {

                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("handler1", new MyHandler1());
                pipeline.addLast("handler2", new MyHandler2());
                return pipeline;
            }
        });
        
        bootstrap.bind(new InetSocketAddress(51503));
        
        System.out.println("start!!!");
    }

}

3、MyHandler1.java

package com.example.netty.lesson11.pipeLine;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.UpstreamMessageEvent;

public class MyHandler1 extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

        ChannelBuffer buffer = (ChannelBuffer)e.getMessage();
        
        byte[] array = buffer.array();
        String message = new String(array);
        System.out.println("handler1:" + message);
        
        //传递给handler2
        ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "abc", e.getRemoteAddress()));
        ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "efg", e.getRemoteAddress()));
    }
}

4、MyHandler2.java

package com.example.netty.lesson11.pipeLine;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

public class MyHandler2 extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

        String message = (String)e.getMessage();
        
        System.out.println("handler2:" + message);
    }
}

完毕!

Netty消息传递

标签:main   over   span   art   coder   actor   服务   mes   star   

原文地址:https://www.cnblogs.com/ywjfx/p/12812907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!