從Test cases.launch 來解讀play 源碼 - play3

首先這個是從play.server.Server.main函數中啓動的(端口號時9000,使用netty來實現連接的)

Server

  public static void main(String[] args) throws Exception {
        //獲取項目路徑 C:\Users\Administrator\Test
        File root = new File(System.getProperty("application.path"));
        //初始化
         Play.init(root, System.getProperty("play.id", ""));
         //創建
        if (System.getProperty("precompile") == null) 
            new Server(args);

  }

下面來看具體的操作(netty 端口號9000)

    public static int httpPort;//http端口號
    public static int httpsPort;//https端口號
    public Server() {

      httpPort = Integer.parseInt(p.getProperty("http.port", "-1"));
      httpsPort = Integer.parseInt(p.getProperty("https.port", "-1"));

      if (httpPort == -1 && httpsPort == -1) 
            httpPort = 9000;
      //地址
      InetAddress address = null;
      InetAddress secureAddress = null;
       if (p.getProperty("http.address") != null) 
         address = InetAddress.getByName(p.getProperty("http.address"));
       if (p.getProperty("https.address") != null) 
         secureAddress = InetAddress.getByName(p.getProperty("https.address"));

       //開啓服務
       if (httpPort != -1) 
          openNettyServer(httpPort, address ,false);
        if (httpsPort != -1) 
          openNettyServer(httpsPort, address ,true);
   }

   //開啓服務
private void openNettyServer(int port,InetAddress address, boolean isHttps) {
    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
        bootstrap.setPipelineFactory(isHttps ? new SslHttpServerPipelineFactory() : newHttpServerPipelineFactory());
 bootstrap.bind(new InetSocketAddress(address, port));
 bootstrap.setOption("child.tcpNoDelay", true);
}

http是HttpServerPipelineFactory,https是SslHttpServerPipelineFactory

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章