<?xml version="1.0" encoding='utf-8'?>
<!-- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
-->
<?xml-stylesheet type="text/xsl" href="https://mbien.dev/roller-ui/styles/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom">
    <title type="html">Michael Bien&apos;s Weblog</title>
    <subtitle type="html">don&apos;t panic</subtitle>
    <id>https://mbien.dev/blog/feed/entries/atom</id>
        <link rel="self" type="application/atom+xml" href="https://mbien.dev/blog/feed/entries/atom?tags=jetty" />
    <link rel="alternate" type="text/html" href="https://mbien.dev/blog/" />
    <updated>2024-08-24T07:57:58+00:00</updated>
    <generator uri="http://roller.apache.org" version="6.1.4">Apache Roller</generator>
    <entry>
        <id>https://mbien.dev/blog/entry/configuring-eclipse-jetty-to-use</id>
        <title type="html">Configuring Eclipse Jetty to use Virtual Threads</title>
        <author><name>mbien</name></author>
        <link rel="alternate" type="text/html" href="https://mbien.dev/blog/entry/configuring-eclipse-jetty-to-use"/>
        <published>2020-08-05T12:16:33+00:00</published>
        <updated>2020-08-06T00:44:31+00:00</updated> 
        <category term="Java" label="Java" />
        <category term="concurrency" scheme="http://roller.apache.org/ns/tags/" />
        <category term="java" scheme="http://roller.apache.org/ns/tags/" />
        <category term="jetty" scheme="http://roller.apache.org/ns/tags/" />
        <category term="jvm" scheme="http://roller.apache.org/ns/tags/" />
        <content type="html">&lt;p&gt;
A quick guide about how to configure Jetty to use &lt;a href=&quot;https://wiki.openjdk.java.net/display/loom&quot;&gt;Project Loom&apos;s&lt;/a&gt; &lt;a href=&quot;//mbien.dev/blog/entry/taking-a-look-at-virtual&quot;&gt;virtual threads&lt;/a&gt; instead of plain old java threads.
&lt;/p&gt;

&lt;p&gt;
Jetty&apos;s default thread pool implementation can be swapped out by implementing Jetty&apos;s &lt;code&gt;ThreadPool&lt;/code&gt; interface and passing an instance to the &lt;code&gt;Server&lt;/code&gt; constructor. If you are using jetty stand alone, everything is initialized by xml files.
&lt;/p&gt;

&lt;p&gt;
Assuming you are using the recommended &lt;a href=&quot;https://www.eclipse.org/jetty/documentation/current/startup-base-and-home.html&quot;&gt;jetty home / jetty base&lt;/a&gt; folder structure, all what is needed is to create &lt;code&gt;jetty-threadpool.xml&lt;/code&gt; in &lt;code&gt;[jetty-base]/etc&lt;/code&gt; containing the following:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;
&amp;lt;Configure&amp;gt;
&amp;lt;New id=&quot;threadPool&quot; class=&quot;dev.mbien.virtualthreads4jetty.VirtualThreadExecutor&quot;/&amp;gt;
&amp;lt;/Configure&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
and put a jar containing the custom &lt;code&gt;VirtualThreadExecutor&lt;/code&gt; into &lt;code&gt;[jetty-base]/lib/ext&lt;/code&gt;.
I uploaded a build to the release section of the &lt;a href=&quot;https://github.com/mbien/vt4jetty&quot;&gt;vt4jetty&lt;/a&gt; github project.
&lt;/p&gt;

&lt;p&gt;
If you don&apos;t have an lib/ext folder yet you can enable it with:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
java -jar $JETTY_HOME/start.jar --add-to-start=ext
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
here the code:
&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;
package dev.mbien.virtualthreads4jetty;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.thread.ThreadPool;

/**
 * Executes each task in a new virtual thread.
 * 
 * &amp;lt;p&amp;gt;Java&apos;s default ForkJoinPool is used as scheduler. To influence carrier
 * thread count use -Djdk.defaultScheduler.parallelism=N. Default is
 * {@link Runtime#availableProcessors()}.
 * 
 * @author mbien
 */
public class VirtualThreadExecutor implements ThreadPool {
    
    private final ExecutorService executor;

    public VirtualThreadExecutor() {
        executor = Executors.newThreadExecutor(
                Thread.builder().virtual().name(&quot;jetty-vt#&quot;, 0).factory());
        // too early for logging libs
        System.out.println(&quot;VirtualThreadExecutor is active.&quot;);
    }
    
    @Override
    public void execute(Runnable command) {
        executor.execute(command);
    }

    @Override
    public void join() throws InterruptedException {
        executor.shutdown();
        executor.awaitTermination(3, TimeUnit.SECONDS);
    }

    // those are hopefully only used for stats/dashboards etc
    @Override
    public int getThreads() { return -1; }

    @Override
    public int getIdleThreads() { return -1; }

    @Override
    public boolean isLowOnThreads() { return false; }
    
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Tested with &lt;b&gt;JDK16-loom+4-56 (2020/7/25)&lt;/b&gt; early access build from &lt;a href=&quot;https://jdk.java.net/loom/&quot;&gt;here&lt;/a&gt; and latest Jetty.
&lt;/p&gt;

&lt;p&gt;
I encountered some JVM crashes while load testing Apache Roller with virtual threads enabled - keep in mind this is still all very much work in progress.
&lt;/p&gt;
</content>
    </entry>
</feed>

