All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vip.justlive.oxygen.web.server.Server Maven / Gradle / Ivy

There is a newer version: 3.0.10.1
Show newest version
/*
 * Copyright (C) 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vip.justlive.oxygen.web.server;

import vip.justlive.oxygen.core.Bootstrap;
import vip.justlive.oxygen.core.config.ConfigFactory;
import vip.justlive.oxygen.core.util.ServiceLoaderUtils;
import vip.justlive.oxygen.web.WebConf;
import vip.justlive.oxygen.web.server.aio.AioWebServer;

/**
 * server 启动类
 *
 * @author wubo
 */
public class Server {

  private WebServer webServer;

  private volatile boolean ready = false;

  /**
   * web server
   *
   * @return webServer
   */
  public static Server server() {
    Server server = new Server();
    server.webServer = ServiceLoaderUtils.loadServiceOrNull(WebServer.class);
    if (server.webServer == null) {
      server.webServer = new AioWebServer();
    }
    return server;
  }

  /**
   * 监听端口并启动服务
   */
  public void listen() {
    listen(8080);
  }

  /**
   * 监听端口并启动服务
   *
   * @param port 端口
   */
  public void listen(int port) {
    if (!ready) {
      ready = true;
      Bootstrap.initConfig();
      WebConf conf = ConfigFactory.load(WebConf.class);
      if (conf.getPort() != null) {
        port = conf.getPort();
      } else {
        conf.setPort(port);
      }
      webServer.listen(port);
    }
  }

  /**
   * 关闭服务
   */
  public void stop() {
    if (ready) {
      ready = false;
      webServer.stop();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy