Netty 源码分析之SimpleChannelInboundHandler
2015-09-02 16:15
781 查看
SimpleChannelInboundHandler继承于ChannelHandlerAdapter,通过加入泛型可以使我们拦截特定类型的对象来进行处理,例如我们解码后得到的ThriftMessage对象,需要注意的是如果没有在构造器中明确指定,SimpleChannelInboundHandler会自动release对象
public abstract class SimpleChannelInboundHandler<I> extends ChannelHandlerAdapter {
//类型匹配器
private final TypeParameterMatcher matcher;
// 是否自动释放,refCount减一
private final boolean autoRelease;
/**
* @see {@link #SimpleChannelInboundHandler(boolean)} with {@code true} as boolean parameter.
*/
protected SimpleChannelInboundHandler() {
this(true);
}
/**
* Create a new instance which will try to detect the types to match out of the type parameter of the class.
*
* @param autoRelease {@code true} if handled messages should be released automatically by pass them to
* {@link ReferenceCountUtil#release(Object)}.
*/
protected SimpleChannelInboundHandler(boolean autoRelease) {
// 根据传入的泛型来确定类型拦截器
matcher = TypeParameterMatcher.find(this, SimpleChannelInboundHandler.class, "I");
this.autoRelease = autoRelease;
}
/**
* @see {@link #SimpleChannelInboundHandler(Class, boolean)} with {@code true} as boolean value.
*/
protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType) {
// 默认释放
this(inboundMessageType, true);
}
/**
* Create a new instance
*
* @param inboundMessageType The type of messages to match
* @param autoRelease {@code true} if handled messages should be released automatically by pass them to
* {@link ReferenceCountUtil#release(Object)}.
*/
protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType, boolean autoRelease) {
matcher = TypeParameterMatcher.get(inboundMessageType);
this.autoRelease = autoRelease;
}
/**
* Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next
* {@link ChannelHandler} in the {@link ChannelPipeline}.
*/
public boolean acceptInboundMessage(Object msg) throws Exception {
// 判断是否需要拦截处理
return matcher.match(msg);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
boolean release = true;
try {
if (acceptInboundMessage(msg)) {
// 判断如果是所需要的类型则进行类型转换,调用messageReceive(ctx, imsg)方法进行处理
@SuppressWarnings("unchecked")
I imsg = (I) msg;
messageReceived(ctx, imsg);
} else {
// 如果不是,则不处理,传入ChannelPipeline下一个Handler
release = false;
ctx.fireChannelRead(msg);
}
} finally {
// 如果需要释放则将msg的refCount减一
if (autoRelease && release) {
ReferenceCountUtil.release(msg);
}
}
}
/**
* Is called for each message of type {@link I}.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
* belongs to
* @param msg the message to handle
* @throws Exception is thrown if an error occurred
*/
// 需要用户来实现的实际处理
protected abstract void messageReceived(ChannelHandlerContext ctx, I msg) throws Exception;
}
public abstract class SimpleChannelInboundHandler<I> extends ChannelHandlerAdapter {
//类型匹配器
private final TypeParameterMatcher matcher;
// 是否自动释放,refCount减一
private final boolean autoRelease;
/**
* @see {@link #SimpleChannelInboundHandler(boolean)} with {@code true} as boolean parameter.
*/
protected SimpleChannelInboundHandler() {
this(true);
}
/**
* Create a new instance which will try to detect the types to match out of the type parameter of the class.
*
* @param autoRelease {@code true} if handled messages should be released automatically by pass them to
* {@link ReferenceCountUtil#release(Object)}.
*/
protected SimpleChannelInboundHandler(boolean autoRelease) {
// 根据传入的泛型来确定类型拦截器
matcher = TypeParameterMatcher.find(this, SimpleChannelInboundHandler.class, "I");
this.autoRelease = autoRelease;
}
/**
* @see {@link #SimpleChannelInboundHandler(Class, boolean)} with {@code true} as boolean value.
*/
protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType) {
// 默认释放
this(inboundMessageType, true);
}
/**
* Create a new instance
*
* @param inboundMessageType The type of messages to match
* @param autoRelease {@code true} if handled messages should be released automatically by pass them to
* {@link ReferenceCountUtil#release(Object)}.
*/
protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType, boolean autoRelease) {
matcher = TypeParameterMatcher.get(inboundMessageType);
this.autoRelease = autoRelease;
}
/**
* Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next
* {@link ChannelHandler} in the {@link ChannelPipeline}.
*/
public boolean acceptInboundMessage(Object msg) throws Exception {
// 判断是否需要拦截处理
return matcher.match(msg);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
boolean release = true;
try {
if (acceptInboundMessage(msg)) {
// 判断如果是所需要的类型则进行类型转换,调用messageReceive(ctx, imsg)方法进行处理
@SuppressWarnings("unchecked")
I imsg = (I) msg;
messageReceived(ctx, imsg);
} else {
// 如果不是,则不处理,传入ChannelPipeline下一个Handler
release = false;
ctx.fireChannelRead(msg);
}
} finally {
// 如果需要释放则将msg的refCount减一
if (autoRelease && release) {
ReferenceCountUtil.release(msg);
}
}
}
/**
* Is called for each message of type {@link I}.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
* belongs to
* @param msg the message to handle
* @throws Exception is thrown if an error occurred
*/
// 需要用户来实现的实际处理
protected abstract void messageReceived(ChannelHandlerContext ctx, I msg) throws Exception;
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 从源码安装Mysql/Percona 5.5
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- spymemcached源码中Reactor模式分析
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序