升级t-io到最新版v2.4.0,优化部分设计

This commit is contained in:
wchao 2018-05-17 01:22:54 +08:00
parent 0c0b5be1a2
commit 56a8638af6
8 changed files with 75 additions and 60 deletions

View File

@ -52,7 +52,7 @@ public class HelloClientAioHandler implements AioHandler,ClientAioHandler
} }
@Override @Override
public TcpPacket decode(ByteBuffer buffer, ChannelContext channelContext) throws AioDecodeException { public TcpPacket decode(ByteBuffer buffer,int limit, int position, int readableLength,ChannelContext channelContext) throws AioDecodeException {
TcpPacket tcpPacket = TcpServerDecoder.decode(buffer, channelContext); TcpPacket tcpPacket = TcpServerDecoder.decode(buffer, channelContext);
return tcpPacket; return tcpPacket;
} }

View File

@ -7,13 +7,13 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.jim.common.cache.ICache;
import org.slf4j.LoggerFactory;
import org.jim.common.cache.redis.JedisTemplate.Pair; import org.jim.common.cache.redis.JedisTemplate.Pair;
import org.jim.common.cache.redis.JedisTemplate.PairEx; import org.jim.common.cache.redis.JedisTemplate.PairEx;
import org.jim.common.utils.JsonKit; import org.jim.common.utils.JsonKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.utils.SystemTimer; import org.tio.utils.SystemTimer;
import org.tio.utils.cache.ICache;
/** /**
* *
* @author wchao * @author wchao

View File

@ -19,13 +19,13 @@
<id>wchao</id> <id>wchao</id>
<name>wchao</name> <name>wchao</name>
<email>wchaojava@163.com</email> <email>wchaojava@163.com</email>
<url>http://git.oschina.net/xchao/tio-im</url> <url>http://git.oschina.net/xchao/j-im</url>
</developer> </developer>
</developers> </developers>
<scm> <scm>
<connection>scm:git:git@gitee.com:xchao/tio-im.git</connection> <connection>scm:git:git@gitee.com:xchao/j-im.git</connection>
<developerConnection>scm:git:git@gitee.com:xchao/tio-im.git</developerConnection> <developerConnection>scm:git:git@gitee.com:xchao/j-im.git</developerConnection>
<url>git@gitee.com:xchao/tio-im.git</url> <url>git@gitee.com:xchao/j-im.git</url>
</scm> </scm>
<modules> <modules>
<module>../jim-client</module> <module>../jim-client</module>
@ -35,8 +35,8 @@
</modules> </modules>
<properties> <properties>
<jim.version>1.0.1.v20180515-RELEASE</jim.version> <jim.version>1.0.1.v20180515-RELEASE</jim.version>
<tio-core.version>2.0.8.v20180205-RELEASE</tio-core.version> <tio-core.version>2.4.0.v20180508-RELEASE</tio-core.version>
<tio-utils.version>2.0.8.v20180205-RELEASE</tio-utils.version> <tio-utils.version>2.4.0.v20180508-RELEASE</tio-utils.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@ -58,7 +58,7 @@
<commons-compress.version>1.14</commons-compress.version> <commons-compress.version>1.14</commons-compress.version>
<fastjson.version>1.2.35</fastjson.version> <fastjson.version>1.2.35</fastjson.version>
<testng.version>6.11</testng.version> <testng.version>6.11</testng.version>
<hutool.version>3.1.0</hutool.version> <hutool.version>4.0.10</hutool.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version> <commons-beanutils.version>1.9.3</commons-beanutils.version>
<jedis.version>2.7.3</jedis.version> <jedis.version>2.7.3</jedis.version>

View File

@ -10,6 +10,7 @@ import org.jim.server.handler.ImServerAioHandler;
import org.jim.server.handler.ServerHandlerManager; import org.jim.server.handler.ServerHandlerManager;
import org.jim.server.listener.ImServerAioListener; import org.jim.server.listener.ImServerAioListener;
import org.tio.server.ServerGroupContext; import org.tio.server.ServerGroupContext;
import org.tio.utils.Threads;
import org.tio.utils.thread.pool.DefaultThreadFactory; import org.tio.utils.thread.pool.DefaultThreadFactory;
import org.tio.utils.thread.pool.SynThreadPoolExecutor; import org.tio.utils.thread.pool.SynThreadPoolExecutor;
@ -29,7 +30,7 @@ public class ImServerGroupContext extends ServerGroupContext {
if (this.timExecutor == null) { if (this.timExecutor == null) {
LinkedBlockingQueue<Runnable> timQueue = new LinkedBlockingQueue<>(); LinkedBlockingQueue<Runnable> timQueue = new LinkedBlockingQueue<>();
String timThreadName = "jim"; String timThreadName = "jim";
this.timExecutor = new SynThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE, KEEP_ALIVE_TIME, timQueue, this.timExecutor = new SynThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE, Threads.KEEP_ALIVE_TIME, timQueue,
DefaultThreadFactory.getInstance(timThreadName, Thread.NORM_PRIORITY), timThreadName); DefaultThreadFactory.getInstance(timThreadName, Thread.NORM_PRIORITY), timThreadName);
this.timExecutor.prestartAllCoreThreads(); this.timExecutor.prestartAllCoreThreads();
} }

View File

@ -7,6 +7,8 @@ import java.nio.ByteBuffer;
import org.jim.common.ImConfig; import org.jim.common.ImConfig;
import org.tio.core.ChannelContext; import org.tio.core.ChannelContext;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler; import org.tio.server.intf.ServerAioHandler;
/** /**
* 版本: [1.0] 功能说明: 封装tioServerAioHandler提供更丰富的方法供客户端定制化; * 版本: [1.0] 功能说明: 封装tioServerAioHandler提供更丰富的方法供客户端定制化;
@ -16,4 +18,10 @@ public abstract class AbServerHandler implements ServerAioHandler{
public abstract String name(); public abstract String name();
public abstract void init(ImConfig imConfig)throws Exception; public abstract void init(ImConfig imConfig)throws Exception;
public abstract boolean isProtocol(ByteBuffer byteBuffer,ChannelContext channelContext)throws Throwable; public abstract boolean isProtocol(ByteBuffer byteBuffer,ChannelContext channelContext)throws Throwable;
public abstract Packet decode(ByteBuffer buffer, ChannelContext channelContext)throws AioDecodeException;
@Override
public Packet decode(ByteBuffer buffer, int limit, int position,int readableLength, ChannelContext channelContext)throws AioDecodeException {
return decode(buffer,channelContext);
}
} }

View File

@ -68,7 +68,7 @@ public class ImServerAioHandler implements ServerAioHandler {
* *
*/ */
@Override @Override
public Packet decode(ByteBuffer buffer, ChannelContext channelContext) throws AioDecodeException { public Packet decode(ByteBuffer buffer,int limit, int position, int readableLength,ChannelContext channelContext) throws AioDecodeException {
ImSessionContext imSessionContext = (ImSessionContext)channelContext.getAttribute(); ImSessionContext imSessionContext = (ImSessionContext)channelContext.getAttribute();
AbServerHandler handler = null; AbServerHandler handler = null;
if(imSessionContext == null){ if(imSessionContext == null){

View File

@ -77,7 +77,7 @@ public class HttpServerHandler extends AbServerHandler{
ImConfig.groupContext.setAttribute(GroupContextKey.HTTP_SERVER_CONFIG, httpConfig); ImConfig.groupContext.setAttribute(GroupContextKey.HTTP_SERVER_CONFIG, httpConfig);
long end = SystemTimer.currentTimeMillis(); long end = SystemTimer.currentTimeMillis();
long iv = end - start; long iv = end - start;
log.info("t-im Http Server初始化完毕,耗时:{}ms", iv); log.info("j-im Http Server初始化完毕,耗时:{}ms", iv);
} }
@Override @Override

View File

@ -15,73 +15,79 @@ public class ImServerAioListener implements ServerAioListener {
Logger logger = Logger.getLogger(ImServerAioListener.class); Logger logger = Logger.getLogger(ImServerAioListener.class);
/** /**
*
*
* @author: WChao * @author: WChao
* 2016年12月16日 下午5:52:06 * 2016年12月16日 下午5:52:06
* *
*/ */
public ImServerAioListener() { public ImServerAioListener() {}
}
/** /**
* @param args * 建链后触发本方法建链不一定成功需要关注参数isConnected
* * @param channelContext
* @param isConnected 是否连接成功,true:表示连接成功false:表示连接失败
* @param isReconnect 是否是重连, true: 表示这是重新连接false: 表示这是第一次连接
* @throws Exception
* @author: WChao * @author: WChao
* 2016年12月16日 下午5:52:06
*
*/ */
public static void main(String[] args) {
}
@Override @Override
public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) { public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) {
return; return;
} }
/** /**
* @see org.tio.core.intf.AioListener#onBeforeSent(org.tio.core.ChannelContext, org.tio.core.intf.Packet, int) * 消息包发送之后触发本方法
*
* @param channelContext * @param channelContext
* @param packet * @param packet
* @author: WChao * @param isSentSuccess true:发送成功false:发送失败
* 2016年12月20日 上午11:08:44 * @throws Exception
* * @author WChao
*/ */
@Override @Override
public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) { public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) {
} }
/**
/** * 连接关闭前触发本方法
* @see org.tio.core.intf.AioListener#onAfterReceived(org.tio.core.ChannelContext, org.tio.core.intf.Packet, int) * @param channelContext the channelcontext
* * @param throwable the throwable 有可能为空
* @param channelContext * @param remark the remark 有可能为空
* @param packet * @param isRemove
* @param packetSize * @author WChao
* @author: WChao * @throws Exception
* 2016年12月20日 上午11:08:44
*
*/ */
@Override
public void onAfterReceived(ChannelContext channelContext, Packet packet, int packetSize) {
}
/**
* @see org.tio.core.intf.AioListener#onAfterClose(org.tio.core.ChannelContext, java.lang.Throwable, java.lang.String)
*
* @param channelContext
* @param throwable
* @param remark
* @author: WChao
* 2017年2月1日 上午11:03:11
*
*/
@Override
public void onAfterClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
}
@Override @Override
public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) { public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
} }
/**
* 解码成功后触发本方法
* @param channelContext
* @param packet
* @param packetSize
* @throws Exception
* @author: WChao
*/
@Override
public void onAfterDecoded(ChannelContext channelContext, Packet packet,int packetSize) throws Exception {
}
/**
* 接收到TCP层传过来的数据后
* @param channelContext
* @param receivedBytes 本次接收了多少字节
* @throws Exception
*/
@Override
public void onAfterReceivedBytes(ChannelContext channelContext,int receivedBytes) throws Exception {
}
/**
* 处理一个消息包后
* @param channelContext
* @param packet
* @param cost 本次处理消息耗时单位毫秒
* @throws Exception
*/
@Override
public void onAfterHandled(ChannelContext channelContext, Packet packet,long cost) throws Exception {
}
} }