手写 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/



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

目录
相关文章
|
2月前
|
编解码 网络协议 API
Netty运行原理问题之Netty的主次Reactor多线程模型工作的问题如何解决
Netty运行原理问题之Netty的主次Reactor多线程模型工作的问题如何解决
|
8天前
|
存储 机器人 Linux
Netty(二)-服务端网络编程常见网络IO模型讲解
Netty(二)-服务端网络编程常见网络IO模型讲解
|
5月前
|
消息中间件 监控 Java
滴滴面试:谈谈你对Netty线程模型的理解?
Netty 线程模型是指 Netty 框架为了提供高性能、高并发的网络通信,而设计的管理和利用线程的策略和机制。 **Netty 线程模型被称为 Reactor(响应式)模型/模式,它是基于 NIO 多路复用模型的一种升级,它的核心思想是将 IO 事件和业务处理进行分离,使用一个或多个线程来执行任务的一种机制。** ## 1.**Reactor三大组件** Reactor 包含以下三大组件: ![image.png](https://cdn.nlark.com/yuque/0/2024/png/92791/1717079218890-89000a00-48bc-4a1a-b87e-e1b6
58 2
|
5月前
|
Java 调度
【Netty 网络通信】Reactor模式
【1月更文挑战第9天】【Netty 网络通信】Reactor模式
|
12月前
|
缓存 网络协议 前端开发
从BIO到NIO在到Netty线程模型详解
从BIO到NIO在到Netty线程模型详解
173 1
|
12月前
|
Java 容器
【深入研究NIO与Netty线程模型的源码】
【深入研究NIO与Netty线程模型的源码】
|
监控 网络协议 Dubbo
Netty入门到超神系列-Netty介绍和线程模型
经过前面章节的学习你应该能感受到NIO的问题,就是类比较多,方法也比较多,而且复杂,开发工作量和难度都非常大,还需要考虑网络问题、数据丢包和异常流的处理等等。 NIO是底层API,它的实现依赖于操作系统针对IO操作的APIs,使用NIO会经常发现代码在Linux上正常运行,但在Windows上就会出现问题。JDK的NIO还有一个Epoll Bug,它会导致 Selector 空轮询,最终导致 CPU 100%。 总之直接使用Java NIO会面临一系列的问题,Netty的其出现就是为了解决NIO的不足
115 0
|
Java
Netty中提供了哪些线程模型?
最近,我更新了一些Netty相关的内容,于是有很多粉丝开始私信问我一些关于Netty的问题。今天,给大家分享一个大家问得比较多问题,Netty中提供了哪些线程模型?
66 0
|
缓存 Java 测试技术
Netty实战(七)EventLoop和线程模型
简单地说,线程模型指定了操作系统、编程语言、框架或者应用程序的上下文中的线程管理的关键方面。
159 0
灵魂一击!Netty系列笔记之Reactor模式(建议收藏)
一、什么是 Reactor 三种 IO 模式和对应的开发模式如下: BIONIOAIOThread-Per-ConnectionReactorProactor Reactor 是一种开发模式,核心流程为: 1、注册感兴趣的事件 2、扫描是否有感兴趣的事件发生 3、事件发生后做相应的处理