Sv: Is the Jetty example available somewhere?

Billy Sjöberg billy.sjoberg at gmail.com
Sun Sep 1 08:51:35 UTC 2019


Thanks Alan,

I actually got it working eventually once I figured out that Jetty reserves threads for the connectors. My first attempt was to run on a single thread just as an example, but I found out you need at least two.
My solution for others interested in trying this out:

public static class FiberBackedThreadPool implements ThreadPool {
    //Jetty reserves at least 1 thread for IO connector, so 2 is magic number to get 1 worker thread.
    ExecutorService executorService = Executors.newWorkStealingPool(2);

    @Override
    public void execute(Runnable command) {
        FiberScope.background().schedule(executorService, command);
    }

    @Override
    public void join() throws InterruptedException {
        new CountDownLatch(1).await();
    }

    @Override
    public int getThreads() {
        return 0;
    }

    @Override
    public int getIdleThreads() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isLowOnThreads() {
        return false;
    }
}


Skickades från E-post för Windows 10

Från: Alan Bateman
Skickat: Sunday, 1 September 2019 09:51
Till: Billy Sjöberg; loom-dev at openjdk.java.net
Ämne: Re: Is the Jetty example available somewhere?

On 29/08/2019 06:32, Billy Sjöberg wrote:
> I’ve seen Alan showing the very interesting graphs comparing a regular blocking http server versus one where the threadpool is configured to use fibers.
> Is there a repo somewhere where this code is available? Or perhaps some sample on how to configure Jetty?
> I’ve tried reproducing it myself, but are stumbling on how to set up the executors correctly.
>
I meant to publish that as a project. I've just made a note of it now 
and will try to get to it soon.

BTW: It was very simple. The Jetty Server class (you'll create one of 
these when embedding the server) has a constructor that allows you to 
specify a ThreadPool. If its execute method is implemented to schedule a 
fiber instead then you'll getting a fiber created for each HTTP request. 
For demo purposes at least, it means the HttpServlet::doGet or the 
method implementing the REST service can block waiting for another 
service or waiting for a database server without tying up a thread in 
the Jetty thread pool.

-Alan



More information about the loom-dev mailing list