Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Last active February 10, 2021 14:53
Show Gist options
  • Save diegopacheco/24169b36d9bb73087ba6ac1afd87b485 to your computer and use it in GitHub Desktop.
Save diegopacheco/24169b36d9bb73087ba6ac1afd87b485 to your computer and use it in GitHub Desktop.
How to Limit Threads in Jetty 9.4

Enanble JMX

https://gist.github.com/diegopacheco/3432ae63b514b878167a37976b004270

Limit threads

java -jar {$jetty.home}/start.jar --add-to-start=jmx
java -jar {$jetty.home}/start.jar --add-to-start=threadpool
java -jar {$jetty.home}/start.jar --add-to-start=threadlimit
vim start.ini
# --------------------------------------- 
# Module: jmx
# Enables JMX instrumentation for server beans and
# enables JMX agent.
# --------------------------------------- 
--module=jmx


# --------------------------------------- 
# Module: threadlimit
# --------------------------------------- 
--module=threadlimit

## Select style of proxy forwarded header
#jetty.threadlimit.forwardedHeader=X-Forwarded-For
#jetty.threadlimit.forwardedHeader=Forwarded

## Enabled by default?
jetty.threadlimit.enabled=true

## Thread limit per remote IP
#jetty.threadlimit.threadLimit=10


# --------------------------------------- 
# Module: threadpool
# Enables the Server thread pool.
# --------------------------------------- 
--module=threadpool


### Server Thread Pool Configuration
## Minimum Number of Threads
#jetty.threadPool.minThreads=10

## Maximum Number of Threads
jetty.threadPool.maxThreads=100

## Number of reserved threads (-1 for heuristic)
# jetty.threadPool.reservedThreads=-1

## Thread Idle Timeout (in milliseconds)
#jetty.threadPool.idleTimeout=60000

## Whether to Output a Detailed Dump
#jetty.threadPool.detailedDump=false
vim etc/jetty.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- =============================================================== -->
<!-- Documentation of this file format can be found at:              -->
<!-- https://www.eclipse.org/jetty/documentation/current/       -->
<!--                                                                 -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in. See start.ini file for the default         -->
<!-- configuration files.                                            -->
<!--                                                                 -->
<!-- For a description of the configuration mechanism, see the       -->
<!-- output of:                                                      -->
<!--   java -jar start.jar -?                                        -->
<!-- =============================================================== -->

<!-- =============================================================== -->
<!-- Configure a Jetty Server instance with an ID "Server"           -->
<!-- Other configuration files may also configure the "Server"       -->
<!-- ID, in which case they are adding configuration to the same     -->
<!-- instance.  If other configuration have a different ID, they     -->
<!-- will create and configure another instance of Jetty.            -->
<!-- Consult the javadoc of o.e.j.server.Server for all              -->
<!-- configuration that may be set here.                             -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <!--  <Arg name="threadpool"><Ref refid="threadPool"/></Arg> -->
	

    <Arg name="threadpool">

      <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
         <Set name="minThreads">10</Set>
     	 <Set name="maxThreads">100</Set>
      </New>

    </Arg>

    <!-- =========================================================== -->
    <!-- Add shared Scheduler instance                               -->
    <!-- =========================================================== -->
    <Call name="addBean">
      <Arg>
        <New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
      </Arg>
    </Call>
  

  <!-- ALL OTHER CONFS HERE .... -->
  
</Configure>
  
@diegopacheco
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment