Elvin
Elvin

Reputation: 477

How to configure Jetty in spring-boot (easily?)

By following the tutorial, I could bring up the spring-boot with Jetty running using the following dependencies.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

However, how could I configure the Jetty server such as:

  1. Server threads (Queue thread pool)
  2. Server connectors
  3. Https configurations.
  4. all those configuration available in Jetty...?

Is there an easy way to do in

  1. application.yml?
  2. Configuration class?

Any example would be greatly appreciated.

Many thanks!!

Upvotes: 32

Views: 46180

Answers (5)

Ashish Lahoti
Ashish Lahoti

Reputation: 1008

Spring Boot provides following Jetty specific configuration through property file:-

server:
  jetty:
    connection-idle-timeout: # Time that the connection can be idle before it is closed.
    max-http-form-post-size: # Maximum size of the form content in any HTTP post request e.g. 200000B
    accesslog:
      enabled: # Enable access log e.g. true
      append: # Enable append to log e.g. true
      custom-format: # Custom log format
      file-date-format: # Date format to place in log file name
      filename: # Log file name, if not specified, logs redirect to "System.err"
      format: # Log format e.g ncsa
      ignore-paths: # Request paths that should not be logged
      retention-period: # Number of days before rotated log files are deleted e.g. 31
    threads:
      acceptors: # Number of acceptor threads to use. When the value is -1, the default, the number of acceptors is derived from the operating environment.
      selectors: # Number of selector threads to use. When the value is -1, the default, the number of selectors is derived from the operating environment.
      min: # Minimum number of threads e.g. 8 
      max: # Maximum number of threads e.g. 200
      max-queue-capacity: # Maximum capacity of the thread pool's backing queue. A default is computed based on the threading configuration.
      idle-timeout: # Maximum thread idle time in millisecond e.g. 60000ms      

Please refer official Spring Boot documentation for more configuration details.

Upvotes: 1

Anurag
Anurag

Reputation: 916

As of the year 2020, while working on newer versions, this is what you need to do, to configure Jetty port, context path and thread pool properties. I tested this on Spring Boot version 2.1.6 while the document I referred to is for version 2.3.3

Create a server factory bean in a configuration file.


    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
        factory.setPort(8080);
        factory.setContextPath("/my-app");
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setMinThreads(10);
        threadPool.setMaxThreads(100);
        threadPool.setIdleTimeout(60000);
        factory.setThreadPool(threadPool);
        return factory;
    }

Following is the link to Spring Docs: customizing-embedded-containers

Upvotes: 0

orrymr
orrymr

Reputation: 2493

If anyone is using Spring Boot - you can easily configure this in you application.properties thusly:

server.max-http-post-size=n

where n is the maximum size to which you wish to set this property. For example I use:

server.max-http-post-size=5000000

Upvotes: 0

Markus
Markus

Reputation: 4689

Possibility to configure Jetty (in parts) programatically from http://howtodoinjava.com/spring/spring-boot/configure-jetty-server/

@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
    JettyEmbeddedServletContainerFactory jettyContainer = 
        new JettyEmbeddedServletContainerFactory();

    jettyContainer.setPort(9000);
    jettyContainer.setContextPath("/home");
    return jettyContainer;
}

Upvotes: 4

Dave Syer
Dave Syer

Reputation: 58094

There are some general extension points for servlet containers and also options for plugging Jetty API calls into those, so I assume everything you would want is in reach. General advice can be found in the docs. Jetty hasn't received as much attention yet so there may not be the same options available for declarative configuration as with Tomcat, and for sure it won't have been used much yet. If you would like to help change that, then help is welcome.

Upvotes: 15

Related Questions