Exemple #1
0
  public static void main(String[] args) throws Exception {
    String name = args.length > 0 ? args[0] : "app";
    Mocker mocker = Mocker.init(name);
    String webXml = mocker.getWebXml();
    int port = mocker.getPort();

    File fapp = new File(APP_PATH);
    final Connector conn = new SelectChannelConnector();
    conn.setPort(port);
    conn.setMaxIdleTime(60000);
    conn.setHeaderBufferSize(20480); // 缺省4K, Chrome下可能会报错: 413 FULL head

    Listener listerner =
        new Listener() {

          @Override
          public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) {
            logger.info("YDJT SYSTEM {} FAILED!!!", Application.VERSION);
          }

          @Override
          public void lifeCycleStarted(LifeCycle arg0) {
            logger.info("YDJT SYSTEM {} STARTED!", Application.VERSION);
          }

          @Override
          public void lifeCycleStarting(LifeCycle arg0) {
            logger.info("YDJT SYSTEM {} STARTING...", Application.VERSION);
          }

          @Override
          public void lifeCycleStopped(LifeCycle arg0) {
            logger.info("YDJT SYSTEM {} STOPPED!", Application.VERSION);
          }

          @Override
          public void lifeCycleStopping(LifeCycle arg0) {
            logger.info("YDJT SYSTEM {} STOPPING...", Application.VERSION);
          }
        };
    Server server = new Server();
    server.addLifeCycleListener(listerner);
    server.addConnector(conn);
    WebAppContext wapp = new WebAppContext(server, fapp.getCanonicalPath(), CONTEXT_PATH);

    HandlerCollection hc = new HandlerCollection();
    ContextHandlerCollection chc = new ContextHandlerCollection();
    hc.setHandlers(new Handler[] {chc, new DefaultHandler()});

    ContextDeployer cd = new ContextDeployer();
    cd.setContexts(chc);
    cd.setConfigurationDir(APP_PATH);
    cd.setDirectory(APP_PATH);
    cd.setScanInterval(5);

    wapp.setClassLoader(ClassLoader.getSystemClassLoader());
    wapp.setParentLoaderPriority(false);
    wapp.setExtractWAR(false);
    // setDescriptor() -- 使用其他文件作为web.xml
    // wapp.setDefaultsDescriptor(WEB_XML);
    // setWelcomeFiles()和web.xml中的welcome-file-list都没有用,
    // 因为/已定义在test-mvc的servlet-mapping中??
    wapp.setDescriptor(APP_PATH + "/WEB-INF/" + webXml);

    // server.addLifeCycle(wapp);
    server.addHandler(hc);
    server.start();
    server.join();
    wapp.start();
    server.addLifeCycle(cd);
  }
Exemple #2
0
  public void init(String host, int port) throws Exception {
    logger.info("Starting Server bound to '" + host + ":" + port + "'");

    String memory = Configurations.get("refine.memory");
    if (memory != null) {
      logger.info(
          "refine.memory size: " + memory + " JVM Max heap: " + Runtime.getRuntime().maxMemory());
    }

    int maxThreads = Configurations.getInteger("refine.queue.size", 30);
    int maxQueue = Configurations.getInteger("refine.queue.max_size", 300);
    long keepAliveTime = Configurations.getInteger("refine.queue.idle_time", 60);

    LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueue);

    threadPool =
        new ThreadPoolExecutor(maxThreads, maxQueue, keepAliveTime, TimeUnit.SECONDS, queue);

    this.setThreadPool(new ThreadPoolExecutorAdapter(threadPool));

    Connector connector = new SocketConnector();
    connector.setPort(port);
    connector.setHost(host);
    connector.setMaxIdleTime(Configurations.getInteger("refine.connection.max_idle_time", 60000));
    connector.setStatsOn(false);
    this.addConnector(connector);

    File webapp = new File(Configurations.get("refine.webapp", "main/webapp"));

    if (!isWebapp(webapp)) {
      webapp = new File("main/webapp");
      if (!isWebapp(webapp)) {
        webapp = new File("webapp");
        if (!isWebapp(webapp)) {
          logger.warn(
              "Warning: Failed to find web application at '" + webapp.getAbsolutePath() + "'");
          System.exit(-1);
        }
      }
    }

    final String contextPath = Configurations.get("refine.context_path", "/");

    logger.info(
        "Initializing context: '" + contextPath + "' from '" + webapp.getAbsolutePath() + "'");
    WebAppContext context = new WebAppContext(webapp.getAbsolutePath(), contextPath);
    context.setMaxFormContentSize(1048576);

    this.setHandler(context);
    this.setStopAtShutdown(true);
    this.setSendServerVersion(true);

    // Enable context autoreloading
    if (Configurations.getBoolean("refine.autoreload", false)) {
      scanForUpdates(webapp, context);
    }

    // start the server
    try {
      this.start();
    } catch (BindException e) {
      logger.error(
          "Failed to start server - is there another copy running already on this port/address?");
      throw e;
    }

    configure(context);
  }