Java Code Examples for org.mortbay.jetty.Server#setStopAtShutdown()

The following examples show how to use org.mortbay.jetty.Server#setStopAtShutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JettyLauncher.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server();

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(PORT);
    server.addConnector(connector);
    server.setStopAtShutdown(true);

    // the orders of handlers is very important!
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/reports");
    contextHandler.setResourceBase("./reports/");
    contextHandler.addHandler(new ResourceHandler());
    server.addHandler(contextHandler);

    server.addHandler(new WebAppContext("webapp", "/nextreports-server"));

    long t = System.currentTimeMillis();
    server.start();
    t = System.currentTimeMillis() - t;
    String version = server.getClass().getPackage().getImplementationVersion();
    System.out.println("Started Jetty Server " + version + " on port " + PORT + " in " + t / 1000 + "s");
    
    server.join();
}
 
Example 2
Source File: JettyServer.java    From exhibitor with Apache License 2.0 5 votes vote down vote up
public JettyServer(int port, HttpsConfiguration httpsConf)
{
    server = new Server();

    SslSelectChannelConnector connector = new SslSelectChannelConnector();
    connector.setPort(port);
    connector.setKeystore(httpsConf.getServerKeystorePath());
    connector.setKeyPassword(httpsConf.getServerKeystorePassword());

    if ( httpsConf.isVerifyPeerCert() )
    {
        connector.setTruststore(httpsConf.getTruststorePath());
        connector.setTrustPassword(httpsConf.getTruststorePassword());
        connector.setNeedClientAuth(true);
    }

    connector.setWantClientAuth(httpsConf.isRequireClientCert());

    connector.setAcceptors(8);
    connector.setMaxIdleTime(5000);
    connector.setAcceptQueueSize(32);

    server.addConnector(connector);
    server.setStopAtShutdown(true);

    DefaultResourceConfig config = new DefaultResourceConfig(JettyServer.RestService.class);
    ServletContainer container = new ServletContainer(config);

    Context context = new Context(server, "/", Context.SESSIONS);
    context.addServlet(new ServletHolder(container), "/*");
}
 
Example 3
Source File: JettyContainer.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        String confPath = args[0];

        confPath = confPath.trim();

        Properties conf = new Properties();
        InputStream is = new FileInputStream(new File(confPath + "/conf/lts-admin.cfg"));
        conf.load(is);
        String port = conf.getProperty("port");
        if (port == null || port.trim().equals("")) {
            port = "8081";
        }

        String contextPath = conf.getProperty("contextPath");
        if (contextPath == null || contextPath.trim().equals("")) {
            contextPath = "/";
        }

        Server server = new Server(Integer.parseInt(port));
        WebAppContext webapp = new WebAppContext();
        webapp.setWar(confPath + "/war/lts-admin.war");
        webapp.setContextPath(contextPath);
        Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("lts.admin.config.path", confPath + "/conf");
        webapp.setInitParams(initParams);
        server.setHandler(webapp);
        server.setStopAtShutdown(true);
        server.start();

        System.out.println("LTS-Admin started. http://" + NetUtils.getLocalHost() + ":" + port + (contextPath == "/" ? "" : contextPath) + "/index.htm");

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example 4
Source File: AbstractServerMojo.java    From opoopress with Apache License 2.0 5 votes vote down vote up
protected Server createJettyServer(Site site){
    Server server = new Server();
    //FIXME
    server.setStopAtShutdown(true);

    Connector defaultConnector = createConnector(null, port);
    server.setConnectors( new Connector[] { defaultConnector } );

    String root = site.getRoot();
    String resourceBase = site.getDestination().getPath();
    getLog().info("Server resource base: " + resourceBase);

    if("".equals(root)){
        ResourceHandler resourceHandler = new ResourceHandler();
        //resourceHandler.setDirectoriesListed(true);
        resourceHandler.setWelcomeFiles(new String[]{"index.html"});

        resourceHandler.setResourceBase(resourceBase/*site.getDestination().getPath()*/);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler()});
        server.setHandler(handlers);
        //server.setHandlers(new Handler[]{handlers, logHandler});
        //getLog().info( "Startisng preview server on http://localhost:" + port + "/" );
    } else {
        getLog().info("Using " + ContextHandler.class.getName());
        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath(root);
        contextHandler.setHandler(new ResourceHandler());
        contextHandler.setResourceBase(resourceBase/*site.getDestination().getPath()*/);
        //server.setHandler(contextHandler);
        server.setHandlers(new Handler[]{contextHandler, new DefaultHandler()});
        //server.setHandlers(new Handler[]{contextHandler, logHandler});
        //log.info( "Starting preview server on http://localhost:" + port + root );
    }
    return server;
}
 
Example 5
Source File: RestServer.java    From hraven with Apache License 2.0 5 votes vote down vote up
@Override
protected void startUp() throws Exception {
  // setup the jetty config
  ServletHolder sh = new ServletHolder(ServletContainer.class);
  sh.setInitParameter("com.sun.jersey.config.property.packages", "com.twitter.hraven.rest");
  sh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");

  server = new Server();

  Connector connector = new SelectChannelConnector();
  connector.setPort(this.port);
  connector.setHost(address);

  server.addConnector(connector);

  // TODO: in the future we may want to provide settings for the min and max threads
  // Jetty sets the default max thread number to 250, if we don't set it.
  //
  QueuedThreadPool threadPool = new QueuedThreadPool();
  server.setThreadPool(threadPool);

  server.setSendServerVersion(false);
  server.setSendDateHeader(false);
  server.setStopAtShutdown(true);
  // set up context
  Context context = new Context(server, "/", Context.SESSIONS);
  context.addServlet(sh, "/*");

  // start server
  server.start();
}
 
Example 6
Source File: HbaseRestLocalCluster.java    From hadoop-mini-clusters with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
    VersionInfo.logVersion();
    Configuration conf = builder.getHbaseConfiguration();

    conf.set("hbase.rest.port", hbaseRestPort.toString());
    conf.set("hbase.rest.readonly", (hbaseRestReadOnly == null) ? "true" : hbaseRestReadOnly.toString());
    conf.set("hbase.rest.info.port", (hbaseRestInfoPort == null) ? "8085" : hbaseRestInfoPort.toString());
    String hbaseRestHost = (this.hbaseRestHost == null) ? "0.0.0.0" : this.hbaseRestHost;

    Integer hbaseRestThreadMax = (this.hbaseRestThreadMax == null) ? 100 : this.hbaseRestThreadMax;
    Integer hbaseRestThreadMin = (this.hbaseRestThreadMin == null) ? 2 : this.hbaseRestThreadMin;

    UserProvider userProvider = UserProvider.instantiate(conf);
    Pair<FilterHolder, Class<? extends ServletContainer>> pair = loginServerPrincipal(userProvider, conf);
    FilterHolder authFilter = pair.getFirst();
    Class<? extends ServletContainer> containerClass = pair.getSecond();
    RESTServlet.getInstance(conf, userProvider);

    // set up the Jersey servlet container for Jetty
    ServletHolder sh = new ServletHolder(containerClass);
    sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", ResourceConfig.class.getCanonicalName());
    sh.setInitParameter("com.sun.jersey.config.property.packages", "jetty");
    ServletHolder shPojoMap = new ServletHolder(containerClass);
    Map<String, String> shInitMap = sh.getInitParameters();
    for (Map.Entry<String, String> e : shInitMap.entrySet()) {
        shPojoMap.setInitParameter(e.getKey(), e.getValue());
    }
    shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");

    // set up Jetty and run the embedded server

    server = new Server();

    Connector connector = new SelectChannelConnector();
    if (conf.getBoolean(RESTServer.REST_SSL_ENABLED, false)) {
        SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
        String keystore = conf.get(RESTServer.REST_SSL_KEYSTORE_STORE);
        String password = HBaseConfiguration.getPassword(conf, RESTServer.REST_SSL_KEYSTORE_PASSWORD, null);
        String keyPassword = HBaseConfiguration.getPassword(conf, RESTServer.REST_SSL_KEYSTORE_KEYPASSWORD, password);
        sslConnector.setKeystore(keystore);
        sslConnector.setPassword(password);
        sslConnector.setKeyPassword(keyPassword);
        connector = sslConnector;
    }
    connector.setPort(hbaseRestPort);
    connector.setHost(hbaseRestHost);
    connector.setHeaderBufferSize(8192);


    server.addConnector(connector);

    QueuedThreadPool threadPool = new QueuedThreadPool(hbaseRestThreadMax);
    threadPool.setMinThreads(hbaseRestThreadMin);
    server.setThreadPool(threadPool);

    server.setSendServerVersion(false);
    server.setSendDateHeader(false);
    server.setStopAtShutdown(true);
    // set up context
    Context context = new Context(server, "/", Context.SESSIONS);
    context.addServlet(shPojoMap, "/status/cluster");
    context.addServlet(sh, "/*");
    if (authFilter != null) {
        context.addFilter(authFilter, "/*", 1);
    }

    HttpServerUtil.constrainHttpMethods(context);

    // Put up info server.
    int port = (hbaseRestInfoPort == null) ? 8085 : hbaseRestInfoPort;
    if (port >= 0) {
        conf.setLong("startcode", System.currentTimeMillis());
        String a = hbaseRestHost;
        infoServer = new InfoServer("rest", a, port, false, conf);
        infoServer.setAttribute("hbase.conf", conf);
        infoServer.start();
    }
    // start server
    server.start();
}