博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手写 reactor( netty reactor 模型)
阅读量:6296 次
发布时间:2019-06-22

本文共 3323 字,大约阅读时间需要 11 分钟。

 

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
selectionKeys= selector.selectedKeys(); Iterator
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://alvta.baihongyu.com/

你可能感兴趣的文章
Cassandra 处理客户端请求
查看>>
[WinApi]邮槽通信C/S实例
查看>>
linux NFS配置:NFS相关概念及其配置与查看
查看>>
需求转化到文档维护
查看>>
IIS 6.0安全增强
查看>>
使用Silverlight 2实现水中倒影效果
查看>>
aria2下载工具命令行和图形化界面使用
查看>>
SWT事件的四种写法
查看>>
AI算法透明不是必须,黑箱和可解释性可简化为优化问题
查看>>
生产环境一个like模糊匹配SQL优化
查看>>
linux wget命令详解
查看>>
Android真机运行错误INSTALL_FAILED_MEDIA_UNAVAILABLE
查看>>
WindowsServer2012史记4-重复数据删除的魅力
查看>>
Win2008 R2实战之只读域控制器部署(图)
查看>>
在Android源码树中添加userspace I2C读写工具(i2c-util)
查看>>
Nginx upstream的几种分配方式
查看>>
《互联网运营智慧》第7章“简单cdn”正式版下载
查看>>
如何解决SQL Server 2008 R2中“阻止保存要求重新创建表的更改”的问题!
查看>>
基于Xcode原型驱动的iOS应用设计
查看>>
SOA标准之----SCA架构思想
查看>>