手写 reactor( netty reactor 模型)

简介:   public class Dispacther implements Runnable{ private String host = "127.0.0.1"; private int port = 8080; public final Selector se...

 

public class Dispacther implements Runnable{

	private String host = "127.0.0.1";
	private int port = 8080;
	
	public final Selector selector;  
    public final ServerSocketChannel serverSocketChannel;
	
    public Dispacther() throws IOException {
    	selector=Selector.open(); 
    	serverSocketChannel=ServerSocketChannel.open();  
    	InetSocketAddress inetSocketAddress=new InetSocketAddress(this.host,this.port);  
        serverSocketChannel.socket().bind(inetSocketAddress);  
        serverSocketChannel.configureBlocking(false);
        
        SelectionKey selectionKey=serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        selectionKey.attach(new Acceptor(this));
	}
    
	
	@Override
	public void run() { 
		try {  
            while(!Thread.interrupted()){  
                selector.select();  
                Set<SelectionKey> selectionKeys= selector.selectedKeys();  
                Iterator<SelectionKey> it=selectionKeys.iterator();   
                while(it.hasNext()){   
                    SelectionKey selectionKey=it.next();  
                    dispatch(selectionKey);  
                    selectionKeys.clear();  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
	} 

	 void dispatch(SelectionKey key) {  
	        Runnable r = (Runnable)(key.attachment());    
	        if (r != null){    
	            r.run();  
	        }    
	    }   
}

 

 

 

public class Acceptor implements Runnable{

	private Dispacther dispacther;
	
	
	public Acceptor(Dispacther dispacther) {
		this.dispacther = dispacther;
	}


	@Override
	public void run() { 
		 try {  
	            SocketChannel socketChannel=dispacther.serverSocketChannel.accept();  
	            if(socketChannel!=null) 
	                new SocketHandler(dispacther.selector, socketChannel);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	}

	 

}

 

 

public class SocketHandler implements Runnable {

   private SocketChannel socketChannel;  
   private Charset charset = Charset.forName("UTF-8");
   private Selector selector;
   
   public SocketHandler(Selector selector,SocketChannel socketChannel) throws IOException{  
        this.socketChannel=socketChannel;  
        this.selector = selector;
        socketChannel.configureBlocking(false);   
        SelectionKey selectionKey=socketChannel.register(selector, 0);  
  
        selectionKey.attach(this);     
        selectionKey.interestOps(SelectionKey.OP_READ);    
        selector.wakeup();  
    }  

	@Override
	public void run() { 
		  try { 	
			    ByteBuffer buff = ByteBuffer.allocate(1024);
				String content = "";
				while (socketChannel.read(buff) > 0) {
					socketChannel.read(buff);
					buff.flip();
					content += charset.decode(buff);
				}
				if(!"".equals(content)){
					System.out.println(  " content : " + content);
					for (SelectionKey key : this.selector.keys()) {
						Channel targetChannel = key.channel();
						if (targetChannel instanceof SocketChannel) {
							SocketChannel dest = (SocketChannel) targetChannel;
							dest.write(charset.encode(content));
							dest.close();
						}
					}
				}
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	}

	 

	 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!

目录
相关文章
|
编解码 弹性计算 缓存
Netty源码和Reactor模型
Netty源码和Reactor模型
120 0
|
监控 安全 Java
填坑Reactor模型和Netty线程模型
在高性能的I/O设计中,有两个著名的模型:Reactor模型和Proactor模型,其中Reactor模型用于同步I/O,而Proactor模型运用于异步I/O操作。实际上Netty线程模型就是Reactor模型的一个实现。
填坑Reactor模型和Netty线程模型
|
存储 编解码 网络协议
Netty 高效的Reactor线程模型
Netty 高效的Reactor线程模型
|
9月前
|
监控 安全 Linux
reactor的原理与实现
前情回顾 网络IO,会涉及到两个系统对象:   一个是用户空间调用的进程或线程   一个是内核空间的内核系统 如果发生IO操作read时,会奖励两个阶段:
110 1
|
Java
Netty的Reactor模式(上)
Netty的Reactor模式
156 0
Netty的Reactor模式(上)
|
设计模式 Java 程序员
Netty线程模型 - Reactor 模式
我相信有很多人会对这个Reactor模式比较陌生,但是Netty这个名字大家都会比较熟悉,即使没有学习使用过,也会对它有所耳闻,它可以说是Java高性能网络编程的代名词。Reactor模式就是Netty线程模型设计的核心,本文我们就以Reactor模式入手,探究一下经典的设计。
196 0
|
JavaScript 安全 Java
深入Netty逻辑架构,从Reactor线程模型开始(一)
深入Netty逻辑架构,从Reactor线程模型开始(一)
484 0
深入Netty逻辑架构,从Reactor线程模型开始(一)
|
消息中间件 Java 调度
深入Netty逻辑架构,从Reactor线程模型开始(二)
深入Netty逻辑架构,从Reactor线程模型开始(二)
228 0
深入Netty逻辑架构,从Reactor线程模型开始(二)

热门文章

最新文章