How to multithread Jetty RESTful service on Heroku

2019-07-15 13:36发布

问题:

I was getting started with the JAX-RS tutorial on Heroku's site->

http://arcane-chamber-8582.herokuapp.com/

The main method looks like this:

package com.example;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 *
 * This class launches the web application in an embedded Jetty container.
 * This is the entry point to your application. The Java command that is used for
 * launching should fire this main method.
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        String webappDirLocation = "src/main/webapp/";

        // The port that we should run on can be set into an environment variable
        // Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if (webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        Server server = new Server(Integer.valueOf(webPort));
        WebAppContext root = new WebAppContext();

        root.setContextPath("/");
        root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
        root.setResourceBase(webappDirLocation);

        // Parent loader priority is a class loader setting that Jetty accepts.
        // By default Jetty will behave like most web containers in that it will
        // allow your application to replace non-server libraries that are part of the
        // container. Setting parent loader priority to true changes this behavior.
        // Read more here: http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading
        root.setParentLoaderPriority(true);

        server.setHandler(root);

        server.start();
        server.join();
    }

}

I was wondering can anybody explain to me what's going on with the server and root? If I assign a dyno to this process, does it automatically create multiple request threads in a thread pool to handle the RESTful requests? If so, which parts are shared/not shared?

Thanks!

回答1:

Jetty is just using the defaults in that scenario (Jetty's defaults, not Heroku's). You can change it like this:

How to use setThreadPool() in Jetty