Jetty version : 11.0.15
Java version : 17
I am trying to update my Spring boot project to 3.1.0. I need GZip compression for reduce response data size. For that I use spring-boot-starter-jetty instead of tomcat server. It download jettry version 11.0.15.
GZip config file
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.zip.Deflater;
@Configuration
public class GZipConfig {
@Bean
public JettyServletWebServerFactory jettyServletWebServerFactory() throws IOException {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.addServerCustomizers((Server server) -> {
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setInflateBufferSize(1);
gzipHandler.setHandler(server.getHandler());
gzipHandler.setIncludedMethods("GET", "POST", "DELETE", "PUT");
gzipHandler.setCompressionLevel(Deflater.BEST_SPEED);
HandlerCollection handlerCollection = new HandlerCollection(gzipHandler);
server.setHandler(handlerCollection);
});
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMinThreads(10);
threadPool.setMaxThreads(200);
threadPool.setIdleTimeout(600000);
factory.setThreadPool(threadPool);
return factory;
}
}
Versions before jetty 9.x has GzipHandler.setCompressionLevel() method to set compression level. But it is not available since jetty 10.x.
How I set compression level in latest jettry server versions.
The GzipHandler
now uses an InflaterPool
and DeflaterPool
, so you will need to configure this pool instead of using a gzipHandler.setCompressionLevel()
method.
Instead use something like this
gzipHandler.setDeflaterPool(new DeflaterPool(CompressionPool.DEFAULT_CAPACITY, Deflater.BEST_SPEED, true));
You could also just create the DeflaterPool
and add it as a bean on the server and it will be shared by any components which also need the pool, and it will be automatically detected by the GzipHandler
without using setDeflaterPool()
explictly.