apache mina 长连接
来源:互联网 发布:公交线路优化方案 编辑:程序博客网 时间:2024/04/30 02:02
apache mina 是较为流行的基于NIO的TCP/IP网络框架,她拥有高性能,以及高的实用性。
可以用于网页游戏服务器,手机游戏服务器。也可作为推送服务器使用。
版本:2.0.x
SocketAcceptor acceptor = new NioSocketAcceptor(); //这个构造函数有个参数,是processCount指的是core process数,一般是电脑的 CPU核数 + 1。LoggingFilter log = new LoggingFilter();log.setMessageSentLogLevel(LogLevel.DEBUG);acceptor.getFilterChain().addLast("logger", log);acceptor.getSessionConfig().setWriteTimeout(10000);acceptor.getSessionConfig().setWriterIdleTime(100000);TextLineCodecFactory lineFactory = new TextLineCodecFactory(Charset.forName("UTF-8"));lineFactory.setDecoderMaxLineLength(Integer.MAX_VALUE);lineFactory.setEncoderMaxLineLength(Integer.MAX_VALUE);acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(lineFactory));acceptor.getFilterChain().addLast("exceutor", new ExecutorFilter());acceptor.setHandler(new MyDataHandler()); //自定义数据处理 handler
acceptor.getFilterChain().addLast("exceutor", new ExecutorFilter());
默认的如果不设置 exceutor,就使用NioSocketAcceptor() 默认的processCount作为主线程数。
ExecutorFilter 默认为 DEFAULT_MAX_POOL_SIZE = 16;
如果在
MyDataHandler做了阻塞的话,最多16个线程 就无法再接受新的线程。
接收更多的线程:
new ExecutorFilter(Executors.newCachedThreadPool()); //线程数。长连接阻塞必须定义一个thread pool否则无法处理更多的连接数。
public class MyDataHandler extends IoHandlerAdapter { public void messageReceived(IoSession session, Object message) throws Exception {String act = message.toString();long _id = session.getId();session.setAttribute(_id, System.currentTimeMillis());process(session, message);}private void process(IoSession session, Object msg) { String cmd = getTcpCmd(msg); if("1001".equals(cmd)){ while (!hasClosed) { //阻塞当前线程,作为主动推送if (session.isClosing() || !session.isConnected()) {break; //异常关闭,客户端主动关闭}if (hasData()) {String data1 = "";try {data1 = readLatestData();} catch (Exception e) {e.printStackTrace();}session.write(data1 + "\n"); //发送数据给客户端,长连接LOG.info("Send Client OK :" + session);} }}}
- apache mina 长连接
- 基于Apache mina 的android 客户端tcp长连接实现
- Mina 长连接实践
- mina长连接ConnectGameServer 类
- Mina长连接Android使用
- Android长连接之mina
- mina实现长连接 推送
- 基于Apache Mina实现的TCP长连接和短连接实例
- 基于Apache Mina实现的TCP长连接和短连接实例
- 基于Apache Mina实现的TCP长连接和短连接实例
- 基于Apache Mina实现的TCP长连接和短连接实例
- Apache mina 入门(四) —— 客户端长连接方式实现断线重连监听
- 使用mina实现Android长连接
- Android 基于mina 实现 Socket 长连接
- Android使用Mina建立长连接
- APACHE MINA (异步连接框架)介绍
- apache mina (异步连接框架)介绍
- apache mina (异步连接框架)介绍
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 查找主串中是否还有子串,如果有则返回匹配的第一个位置
- pkg_number_trans.f_hex_to_dec()和pkg_number_trans.f_hex_to_bin()
- LEADTOOLS PDF应用实例:增强Google Drive搜索功能
- 【cocos2d-x入门之二】游戏和引擎构成
- apache mina 长连接
- win7系统下连接使用mac 蓝牙键盘(Apple Wireless Keyborad)
- SharePoint 2013 Odata 常用实例和基本操作
- 有道难题
- 【Cocos2d-x游戏引擎开发笔记(9)】实现DotA的技能冷却效果(转)
- java 压缩文件 解压缩文件
- 千万别哄自己玩
- 在linq中使用存储过程
- windows下maven打包eclipse工程