当前位置:
首页
文章
前端
详情

Play源码深入之二:Play应用启动时框架的初始化

接着 上篇在python的辅助下,理理输入启动命令之后,play框架进行的初始化工作。 application.py中的java_cmd方法中就有play.server.Server。

def java_cmd(self, java_args, cp_args=None, className='play.server.Server', args = None):

在这个类中可以看到亲切的main方法,就是框架的入口了。

public class Server {
    ...
    public static void main(String[] args) throws Exception {
        File root = new File(System.getProperty("application.path"));
        if (System.getProperty("precompiled", "false").equals("true")) {
            Play.usePrecompiled = true;
        }
        if (System.getProperty("writepid", "false").equals("true")) {
            writePID(root);
        }
        Play.init(root, System.getProperty("play.id", ""));
        if (System.getProperty("precompile") == null) {
            new Server(args);
        } else {
            Logger.info("Done.");
        }
    }
    ...
}

其中对play.Play:init()方法对框架进行了初始化,包括应用的状态、应用java代码、模板、路由的位置、cookie域名称等。

public class Play {
    ...
    public static void init(File root, String id) {
        ...
        // Main route file
        routes = appRoot.child("conf/routes");

        // Plugin route files
        modulesRoutes = new HashMap<String, VirtualFile>(16);

        // Load modules
        loadModules(appRoot);

        // Load the templates from the framework after the one from the modules
        templatesPath.add(VirtualFile.open(new File(frameworkPath, "framework/templates")));

        // Enable a first classloader
        classloader = new ApplicationClassloader();

        // Fix ctxPath
        if ("/".equals(Play.ctxPath)) {
            Play.ctxPath = "";
        }

        // Default cookie domain
        Http.Cookie.defaultDomain = configuration.getProperty("application.defaultCookieDomain", null);
        if (Http.Cookie.defaultDomain!=null) {
            Logger.info("Using default cookie domain: " + Http.Cookie.defaultDomain);
        }

        // Plugins
        pluginCollection.loadPlugins();
        ...
    }
    ...
}

其中最关键的是对play插件的读入。play.plugins.PluginCollection:loadPlugins()方法,play的应用每个事件,如应用启动、访问到达、访问结束、代码变动、应用停止等等,都会逐一交给各个插件处理。 在play源码中已经定义一些plugins,并有顺序。

0:play.CorePlugin
100:play.data.parsing.TempFilePlugin
200:play.data.validation.ValidationPlugin
300:play.db.DBPlugin
400:play.db.jpa.JPAPlugin
450:play.db.Evolutions
500:play.i18n.MessagesPlugin
600:play.libs.WS
700:play.jobs.JobsPlugin
100000:play.plugins.ConfigurablePluginDisablingPlugin

我们也可以定义自己的Plugins继承PlayPlugins,然后在应用app目录下如框架中一样,建立play.plugins文件,就也能接受play事件的推送了。

Play源码深入之二:Play应用启动时框架的初始化  

在Server的构造方法中,准备一些netty启动需要的参数之后,就开启了netty,其中重要的是 设定了HttpServerPipelineFactory

public class Server {
    ...
    public Server(String[] args) {
       ...
        ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
        );
        try {
            if (httpPort != -1) {
                bootstrap.setPipelineFactory(new HttpServerPipelineFactory());

                bootstrap.bind(new InetSocketAddress(address, httpPort));
                bootstrap.setOption("child.tcpNoDelay", true);
       ...
    }
    ...
}

当第一次访问到时,netty将请求转到play.server.HttpServerPipelineFactory中。其中配置了play.server.PlayHandler处理类。netty会用PlayHandler来处理请求。

private String pipelineConfig = Play.configuration.getProperty("play.netty.pipeline", "play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.PlayHandler");

在往后,就进入了下一篇文章。

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。