Search in sources :

Example 1 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project hive by apache.

the class ThriftHttpCLIService method run.

/**
   * Configure Jetty to serve http requests. Example of a client connection URL:
   * http://localhost:10000/servlets/thrifths2/ A gateway may cause actual target URL to differ,
   * e.g. http://gateway:port/hive2/servlets/thrifths2/
   */
@Override
public void run() {
    try {
        // HTTP Server
        httpServer = new org.eclipse.jetty.server.Server();
        // Server thread pool
        // Start with minWorkerThreads, expand till maxWorkerThreads and reject subsequent requests
        String threadPoolName = "HiveServer2-HttpHandler-Pool";
        ExecutorService executorService = new ThreadPoolExecutorWithOomHook(minWorkerThreads, maxWorkerThreads, workerKeepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryWithGarbageCleanup(threadPoolName), oomHook);
        ExecutorThreadPool threadPool = new ExecutorThreadPool(executorService);
        httpServer.setThreadPool(threadPool);
        // Connector configs
        SelectChannelConnector connector = new SelectChannelConnector();
        // Configure header size
        int requestHeaderSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_REQUEST_HEADER_SIZE);
        int responseHeaderSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_RESPONSE_HEADER_SIZE);
        connector.setRequestHeaderSize(requestHeaderSize);
        connector.setResponseHeaderSize(responseHeaderSize);
        boolean useSsl = hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL);
        String schemeName = useSsl ? "https" : "http";
        // Change connector if SSL is used
        if (useSsl) {
            String keyStorePath = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH).trim();
            String keyStorePassword = ShimLoader.getHadoopShims().getPassword(hiveConf, HiveConf.ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PASSWORD.varname);
            if (keyStorePath.isEmpty()) {
                throw new IllegalArgumentException(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname + " Not configured for SSL connection");
            }
            SslContextFactory sslContextFactory = new SslContextFactory();
            String[] excludedProtocols = hiveConf.getVar(ConfVars.HIVE_SSL_PROTOCOL_BLACKLIST).split(",");
            LOG.info("HTTP Server SSL: adding excluded protocols: " + Arrays.toString(excludedProtocols));
            sslContextFactory.addExcludeProtocols(excludedProtocols);
            LOG.info("HTTP Server SSL: SslContextFactory.getExcludeProtocols = " + Arrays.toString(sslContextFactory.getExcludeProtocols()));
            sslContextFactory.setKeyStorePath(keyStorePath);
            sslContextFactory.setKeyStorePassword(keyStorePassword);
            connector = new SslSelectChannelConnector(sslContextFactory);
        }
        connector.setPort(portNum);
        // Linux:yes, Windows:no
        connector.setReuseAddress(true);
        int maxIdleTime = (int) hiveConf.getTimeVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_MAX_IDLE_TIME, TimeUnit.MILLISECONDS);
        connector.setMaxIdleTime(maxIdleTime);
        httpServer.addConnector(connector);
        // Thrift configs
        hiveAuthFactory = new HiveAuthFactory(hiveConf);
        TProcessor processor = new TCLIService.Processor<Iface>(this);
        TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
        // Set during the init phase of HiveServer2 if auth mode is kerberos
        // UGI for the hive/_HOST (kerberos) principal
        UserGroupInformation serviceUGI = cliService.getServiceUGI();
        // UGI for the http/_HOST (SPNego) principal
        UserGroupInformation httpUGI = cliService.getHttpUGI();
        String authType = hiveConf.getVar(ConfVars.HIVE_SERVER2_AUTHENTICATION);
        TServlet thriftHttpServlet = new ThriftHttpServlet(processor, protocolFactory, authType, serviceUGI, httpUGI, hiveAuthFactory);
        // Context handler
        final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        if (hiveConf.getBoolean(ConfVars.HIVE_SERVER2_XSRF_FILTER_ENABLED.varname, false)) {
            // context.addFilter(Utils.getXSRFFilterHolder(null, null), "/" ,
            //    FilterMapping.REQUEST);
            // Filtering does not work here currently, doing filter in ThriftHttpServlet
            LOG.debug("XSRF filter enabled");
        } else {
            LOG.warn("XSRF filter disabled");
        }
        String httpPath = getHttpPath(hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH));
        httpServer.setHandler(context);
        context.addServlet(new ServletHolder(thriftHttpServlet), httpPath);
        // TODO: check defaults: maxTimeout, keepalive, maxBodySize, bodyRecieveDuration, etc.
        // Finally, start the server
        httpServer.start();
        String msg = "Started " + ThriftHttpCLIService.class.getSimpleName() + " in " + schemeName + " mode on port " + portNum + " path=" + httpPath + " with " + minWorkerThreads + "..." + maxWorkerThreads + " worker threads";
        LOG.info(msg);
        httpServer.join();
    } catch (Throwable t) {
        LOG.error("Error starting HiveServer2: could not start " + ThriftHttpCLIService.class.getSimpleName(), t);
        System.exit(-1);
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) ThreadFactoryWithGarbageCleanup(org.apache.hive.service.server.ThreadFactoryWithGarbageCleanup) TProcessor(org.apache.thrift.TProcessor) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TServlet(org.apache.thrift.server.TServlet) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) TProcessor(org.apache.thrift.TProcessor) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) ExecutorService(java.util.concurrent.ExecutorService) ExecutorThreadPool(org.eclipse.jetty.util.thread.ExecutorThreadPool) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project jfinal by jfinal.

the class JettyServerForIDEA method doStart.

private void doStart() {
    if (!available(port)) {
        throw new IllegalStateException("port: " + port + " already in use!");
    }
    deleteSessionData();
    System.out.println("Starting JFinal " + Const.JFINAL_VERSION);
    server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(port);
    server.addConnector(connector);
    webApp = new WebAppContext();
    // 在启动过程中允许抛出异常终止启动并退出 JVM
    webApp.setThrowUnavailableOnStartupException(true);
    webApp.setContextPath(context);
    // webApp.setWar(webAppDir);
    webApp.setResourceBase(webAppDir);
    webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    // webApp.setInitParams(Collections.singletonMap("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false"));
    webApp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
    persistSession(webApp);
    server.setHandler(webApp);
    try {
        System.out.println("Starting web server on port: " + port);
        server.start();
        System.out.println("Starting Complete. Welcome To The JFinal World :)");
        server.join();
    } catch (Exception e) {
        LogKit.error(e.getMessage(), e);
        System.exit(100);
    }
    return;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server) IOException(java.io.IOException)

Example 3 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project meteo by pierre.

the class JettyServer method start.

public void start(final Injector injector) throws Exception {
    final long startTime = System.currentTimeMillis();
    server = new Server();
    // Setup JMX
    final MBeanContainer mbContainer = new MBeanContainer(mbeanServer);
    server.getContainer().addEventListener(mbContainer);
    server.addBean(mbContainer);
    mbContainer.addBean(Log.getLog());
    final Connector connector = new SelectChannelConnector();
    connector.setStatsOn(config.isJettyStatsOn());
    connector.setHost(config.getLocalIp());
    connector.setPort(config.getLocalPort());
    server.addConnector(connector);
    server.setStopAtShutdown(true);
    final ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return injector;
        }
    });
    // Jersey insists on using java.util.logging (JUL)
    final EventListener listener = new SetupJULBridge();
    context.addEventListener(listener);
    // Make sure Guice filter all requests
    final FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
    context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    // Backend servlet for Guice - never used
    final ServletHolder sh = new ServletHolder(DefaultServlet.class);
    context.addServlet(sh, "/*");
    server.start();
    final long secondsToStart = (System.currentTimeMillis() - startTime) / 1000;
    log.info(String.format("Jetty server started in %d:%02d", secondsToStart / 60, secondsToStart % 60));
}
Also used : Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MBeanServer(javax.management.MBeanServer) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Injector(com.google.inject.Injector) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) EventListener(java.util.EventListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener)

Example 4 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project simple-java by angryziber.

the class Launcher method main.

public static void main(String[] args) throws Exception {
    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(8080);
    server.addConnector(connector);
    WebAppContext context = new WebAppContext("webapp", "/");
    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        // fix for Windows, so Jetty doesn't lock files
        context.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
    }
    server.setHandler(context);
    server.start();
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server)

Example 5 with SelectChannelConnector

use of org.eclipse.jetty.server.nio.SelectChannelConnector in project ats-framework by Axway.

the class ContainerStarter method startServer.

/**
     * Method for starting the Jetty server with the ATS Agent webapp.
     * @param port the port on which to start the server.
     * @return the started server.
     * @throws IOException
     */
private static Server startServer() throws IOException {
    addAppender();
    final int agentPort = getAgentDefaultPort();
    log.info("Starting ATS agent at port: " + agentPort);
    final String jettyHome = getJettyHome();
    logSystemInformation(jettyHome);
    // start the server
    Connector connector = new SelectChannelConnector();
    connector.setPort(agentPort);
    Server server = new Server();
    server.setConnectors(new Connector[] { connector });
    WebAppContext webApp = new WebAppContext();
    webApp.setContextPath("/agentapp");
    webApp.setWar(jettyHome + "/webapp/agentapp.war");
    server.setHandler(webApp);
    server.setStopAtShutdown(true);
    setExtraClasspath(webApp, jettyHome);
    try {
        server.start();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    log.info("ATS agent started");
    return server;
}
Also used : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server) SocketException(java.net.SocketException) AgentException(com.axway.ats.agentapp.standalone.exceptions.AgentException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)19 Server (org.eclipse.jetty.server.Server)14 Connector (org.eclipse.jetty.server.Connector)8 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)5 IOException (java.io.IOException)4 SslSelectChannelConnector (org.eclipse.jetty.server.ssl.SslSelectChannelConnector)4 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)3 UnknownHostException (java.net.UnknownHostException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)2 ServiceBindException (co.cask.cdap.common.ServiceBindException)1 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)1 AgentException (com.axway.ats.agentapp.standalone.exceptions.AgentException)1 Injector (com.google.inject.Injector)1 GuiceServletContextListener (com.google.inject.servlet.GuiceServletContextListener)1