首页 > 代码库 > play 源码分析
play 源码分析
1、实例化ServerBootstrap 启动netty服务器(boss线程池、worker线程池),绑定IP、端口
2、指定filter ,也就是PLAY中的HttpServerPipelineFactory,用于处理输入和输出报文
//处理httprequest
pipeline.addLast("decoder", new HttpRequestDecoder());
//实现http分块
pipeline.addLast("aggregator", new StreamChunkAggregator(max));
//处理http返回
pipeline.addLast("encoder", new HttpResponseEncoder());
//http分块
pipeline.addLast("chunkedWriter", playHandler.chunkedWriteHandler);
Integer gzip = Integer.valueOf(Play.configuration.getProperty("play.netty.gzip", "0"));
if (gzip==1)
{
//压缩
pipeline.addLast("compress", new HttpContentCompressor());
//解压
pipeline.addLast("decompress", new HttpContentDecompressor());
}
//参数解析
pipeline.addLast("handler", playHandler);
3、playHandler处理分析
parseRequest:将nettyRequest解析为play.mvc.http.Request
Play.pluginCollection.rawInvocation:执行PLAY插件列表
CorePlugin:处理play内置的功能(状态):/@kill /@status
DBPlugin:处理play内置功能(启动h2Server):/@db
Evolutions:处理play内置功能(检查数据结构变更):/@evolutions/apply
Invoker.invoke(new NettyInvocation(request, response, ... :开始执行代码
放在线程池中去执行(play.pool)
Invoker.java:Run some code in a Play! context
Invocation:An Invocation in something to run in a Play! context
init():detectChanges 检测代码变化,热部署,仅DEV模式(配置文件变化时重启)
run():捕获异常,返回500
before(); 执行所有插件beforeInvocation
JPAPlugin事务开启
execute();
after(); 执行所有插件afterInvocation
JPAPlugin事务关闭
onSuccess();执行所有插件onInvocationSuccess
TempFilePlugin临时文件删除
execute():ActionInvoker.invoke 开始调用action
4、ActionInvoker.invoke分析
resolve:将request、response、params、Session放入线程变量;根据URL找到action类和方法
解析请求内容,生成action参数
handleBeforeValidations:执行action拦截器 @BeforeValidation @unless @only
Play.pluginCollection.beforeActionInvocation: 执行play插件beforeActionInvocation
PlayPlugin.beforeActionInvocation: 将错误信息加入Validation
handleBefores:执行action拦截器 @before @unless @only
invokeControllerMethod:执行controllers代码
inferResult:处理controllers代码执行后的返回结果
catch (InvocationTargetException ex) :捕获异常
返回结果的异常(Result):保存cache
其它异常:执行action拦截器 @Catch @unless @only
handleAfters(request);:执行action拦截器 @after @unless @only
catch (Result result)
Play.pluginCollection.onActionInvocationResult(result); :
ValidationPlugin.onActionInvocationResult:保存Validation.errors到cookies
保存Session(cookies)
result.apply(request, response);:根据result类型设置http code,http head,http body
Play.pluginCollection.afterActionInvocation();:暂无实现
handleFinallies:执行action拦截器 @Finally @unless @only
play 源码分析