Java Code Examples for org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory#addConnectorCustomizers()

The following examples show how to use org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory#addConnectorCustomizers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebServerConfiguration.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Bean  
public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() throws IOException, NoSuchAlgorithmException  
{  
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();  
    tomcatFactory.addConnectorCustomizers(new UKeFuTomcatConnectorCustomizer(maxthread, maxconnections));  
    File sslFile = new File(path , "ssl/https.properties") ;
    if(sslFile.exists()){
    	Properties sslProperties = new Properties();
    	FileInputStream in = new FileInputStream(sslFile);
    	sslProperties.load(in);
    	in.close();
    	if(!StringUtils.isBlank(sslProperties.getProperty("key-store")) && !StringUtils.isBlank(sslProperties.getProperty("key-store-password"))){
      Ssl ssl = new Ssl();
      ssl.setKeyStore(new File(path , "ssl/"+sslProperties.getProperty("key-store")).getAbsolutePath());
      ssl.setKeyStorePassword(UKTools.decryption(sslProperties.getProperty("key-store-password")));
      tomcatFactory.setSsl(ssl);
    	}
    }
    return tomcatFactory;  
}
 
Example 2
Source File: ServletContainerConfig.java    From portal-de-servicos with MIT License 6 votes vote down vote up
@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
    return servletContainer -> {
        addMimeMappingsForFonts(servletContainer);

        TomcatEmbeddedServletContainerFactory container = (TomcatEmbeddedServletContainerFactory) servletContainer;

        container.setRegisterJspServlet(false);

        container.addContextCustomizers(customizer -> customizer.addWelcomeFile("index.html"));
        container.addConnectorCustomizers(
                connector -> {
                    AbstractHttp11Protocol httpProtocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
                    httpProtocol.setCompression("on");
                    httpProtocol.setCompressionMinSize(256);
                    httpProtocol.setCompressableMimeTypes(COMPRESSIBLE_MEDIA_TYPES);
                }
        );
    };
}
 
Example 3
Source File: TomcatConfig.java    From find with MIT License 6 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory servletContainer() {
    final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

    if(useReverseProxy) {
        tomcat.addAdditionalTomcatConnectors(createAjpConnector());
    }

    // Set the web resources cache size (this defaults to 10MB but that is too small for Find)
    tomcat.addContextCustomizers(context -> {
        final WebResourceRoot resources = new StandardRoot(context);
        resources.setCacheMaxSize(webResourcesCacheSize);
        context.setResources(resources);
    });

    tomcat.addConnectorCustomizers(connector -> {
        connector.setMaxPostSize(connectorMaxPostSize);
    });

    return tomcat;
}
 
Example 4
Source File: SystemBootAutoConfiguration.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * Customization servlet container configuring. </br>
 * 
 * @see {@link EmbeddedServletContainerAutoConfiguration}
 * 
 * @return
 */
@Bean
public EmbeddedServletContainerCustomizer customEmbeddedServletContainerCustomizer() {
	return container -> {
		// Tomcat container customization
		if (container instanceof TomcatEmbeddedServletContainerFactory) {
			TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
			tomcat.addConnectorCustomizers(connector -> {
				ProtocolHandler handler = connector.getProtocolHandler();
				if (handler instanceof AbstractProtocol) {
					AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
					/**
					 * {@link org.apache.tomcat.util.net.NioEndpoint#startInternal()}
					 * {@link org.apache.tomcat.util.net.NioEndpoint#createExecutor()}
					 */
					protocol.setExecutor(customTomcatExecutor(protocol));
				}
			});
		} else {
			log.warn("Skip using custom servlet container, EmbeddedServletContainer: {}", container);
		}
	};

}
 
Example 5
Source File: TomcatContainerCustomizer.java    From radar with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
	MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
	mappings.add("woff", "application/x-font-woff");
	mappings.add("eot", "application/vnd.ms-fontobject");
	mappings.add("ttf", "application/x-font-ttf");
	container.setMimeMappings(mappings);

	if (!(container instanceof TomcatEmbeddedServletContainerFactory)) {
		return;
	}
	if (!environment.containsProperty(TOMCAT_ACCEPTOR_COUNT)) {
		return;
	}
	TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
	tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

		@Override
		public void customize(Connector connector) {
			ProtocolHandler handler = connector.getProtocolHandler();
			if (handler instanceof Http11NioProtocol) {
				Http11NioProtocol http = (Http11NioProtocol) handler;
				acceptCount = soaConfig.getTomcatAcceptCount();
				soaConfig.registerChanged(() -> {
					if (acceptCount != soaConfig.getTomcatAcceptCount()) {
						acceptCount = soaConfig.getTomcatAcceptCount();
						http.setBacklog(acceptCount);
					}
				});
				http.setBacklog(acceptCount);
				logger.info("Setting tomcat accept count to {}", acceptCount);
			}
		}
	});
}
 
Example 6
Source File: TomcatConfig.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
    tomcatFactory.addConnectorCustomizers(connector -> {
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        protocol.setKeepAliveTimeout(10 * 1000);
        protocol.setMaxKeepAliveRequests(constantProperties.getKeepAliveRequests());
    });
    return tomcatFactory;
}
 
Example 7
Source File: WebServerConfiguration.java    From goods-seckill with Apache License 2.0 5 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
	TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
	tomcatFactory.setPort(8080);
	tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
	return tomcatFactory;
}
 
Example 8
Source File: WebConfig.java    From yfs with Apache License 2.0 5 votes vote down vote up
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        // configure maxSwallowSize
        if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
            // -1 means unlimited, accept bytes
            ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
    });
    return tomcat;
}
 
Example 9
Source File: RagnarApplication.java    From fiery with Apache License 2.0 5 votes vote down vote up
@Bean
EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {
    return (ConfigurableEmbeddedServletContainer container) -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(
                    (connector) -> {
                        connector.setMaxPostSize(1000000000); // 1000 MB
                    }
            );
        }
    };
}
 
Example 10
Source File: WebRestApiApplication.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer()
{
	return servletContainer -> {
		final TomcatEmbeddedServletContainerFactory tomcatContainerFactory = (TomcatEmbeddedServletContainerFactory)servletContainer;
		tomcatContainerFactory.addConnectorCustomizers(connector -> {
			final AbstractHttp11Protocol<?> httpProtocol = (AbstractHttp11Protocol<?>)connector.getProtocolHandler();
			httpProtocol.setCompression("on");
			httpProtocol.setCompressionMinSize(256);
			final String mimeTypes = httpProtocol.getCompressibleMimeType();
			final String mimeTypesWithJson = mimeTypes + "," + MediaType.APPLICATION_JSON_VALUE + ",application/javascript";
			httpProtocol.setCompressibleMimeType(mimeTypesWithJson);
		});
	};
}
 
Example 11
Source File: TomcatConfig.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
@Bean
public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
        tomcatFactory.addConnectorCustomizers(connector -> {
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        protocol.setKeepAliveTimeout(constantProperties.getKeepAliveTimeout()* 1000);
        protocol.setMaxKeepAliveRequests(constantProperties.getKeepAliveRequests());
    });
    return tomcatFactory;
}
 
Example 12
Source File: BootServletContainerCustomizer.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(
                (connector) -> {
                	//connector 本身默认是 2 mb
                	connector.setMaxPostSize(FileUtils.parseSize(multipartProperties.getMaxRequestSize()));
                	Http11NioProtocol handler = (Http11NioProtocol)connector.getProtocolHandler();
                	if(tomcatProperties.getBacklog()!=-1){
                		//socket 连接队列大小
//                		handler.setBacklog(tomcatProperties.getBacklog());
                		handler.setAcceptCount(tomcatProperties.getAcceptCount());
                	}
                	if(tomcatProperties.getMaxConnections()!=-1){
                		//最大连接数,默认10000
                		handler.setMaxConnections(tomcatProperties.getMaxConnections());
                	}
                	if(tomcatProperties.getConnectionTimeout()!=-1){
                		handler.setConnectionTimeout(tomcatProperties.getConnectionTimeout());
                	}
                	if(tomcatProperties.getConnectionUploadTimeout()>0){
                		//为true,则上传文件时使用connectionTimeout, 为false,则使用connectionUploadTimeout
                		handler.setDisableUploadTimeout(false);
                		handler.setConnectionUploadTimeout(tomcatProperties.getConnectionUploadTimeout());
                	}
                	connector.setAsyncTimeout(tomcatProperties.getAsyncTimeout());
                }
            );
        }
		/*if(container instanceof TomcatEmbeddedServletContainerFactory){
			TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
			tomcat.addContextCustomizers(context->{
				context.setReloadable(true);
			});
		}*/
	}
 
Example 13
Source File: OAuth2SecurityConfiguration.java    From mobilecloud-15 with Apache License 2.0 5 votes vote down vote up
@Bean
  EmbeddedServletContainerCustomizer containerCustomizer(
          @Value("${keystore.file:src/main/resources/private/keystore}") String keystoreFile,
          @Value("${keystore.pass:changeit}") final String keystorePass) throws Exception {

// If you were going to reuse this class in another
// application, this is one of the key sections that you
// would want to change
  	
      final String absoluteKeystoreFile = new File(keystoreFile).getAbsolutePath();

      return new EmbeddedServletContainerCustomizer () {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(
                    new TomcatConnectorCustomizer() {
						@Override
						public void customize(Connector connector) {
							connector.setPort(8443);
	                        connector.setSecure(true);
	                        connector.setScheme("https");

	                        Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler();
	                        proto.setSSLEnabled(true);
	                        proto.setKeystoreFile(absoluteKeystoreFile);
	                        proto.setKeystorePass(keystorePass);
	                        proto.setKeystoreType("JKS");
	                        proto.setKeyAlias("tomcat");
						}
                    });
    
	}
      };
  }
 
Example 14
Source File: ContainerConfiguration.java    From spring-boot-security-example with MIT License 5 votes vote down vote up
@Bean
EmbeddedServletContainerCustomizer containerCustomizer(
        @Value("${keystore.file}") String keystoreFile,
        @Value("${server.port}") final String serverPort,
        @Value("${keystore.pass}") final String keystorePass)
        throws Exception {

    // This is boiler plate code to setup https on embedded Tomcat
    // with Spring Boot:

    final String absoluteKeystoreFile = new File(keystoreFile)
            .getAbsolutePath();

    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(connector -> {
                connector.setPort(Integer.parseInt(serverPort));
                connector.setSecure(true);
                connector.setScheme("https");

                Http11NioProtocol proto = (Http11NioProtocol) connector
                        .getProtocolHandler();
                proto.setSSLEnabled(true);

                proto.setKeystoreFile(absoluteKeystoreFile);
                proto.setKeystorePass(keystorePass);
                proto.setKeystoreType("JKS");
                proto.setKeyAlias("tomcat");
            });
        }
    };
}
 
Example 15
Source File: Port80ServletContainerCustomizer.java    From JSF-on-Spring-Boot with GNU General Public License v3.0 5 votes vote down vote up
public void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
      @Override
      public void customize(Connector connector) {
    	  connector.setPort(80);
//        Object defaultMaxThreads = connector.getAttribute("maxThreads");
//        connector.getAttribute("javax.faces.CLIENT_WINDOW_MODE")
        connector.setAttribute("maxThreads", MAX_THREADS);
      }
    });
  }
 
Example 16
Source File: MyEmbeddedServletContainerCustomizer.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
public void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            Object defaultMaxThreads = connector.getAttribute("maxThreads");
            connector.setAttribute("maxThreads", MAX_THREADS);
            LOG.info("Changed Tomcat connector maxThreads from " + defaultMaxThreads + " to " + MAX_THREADS);
        }
    });
}
 
Example 17
Source File: MyEmbeddedServletContainerCustomizer.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
public void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            Object defaultMaxThreads = connector.getAttribute("maxThreads");
            connector.setAttribute("maxThreads", MAX_THREADS);
            LOG.info("Changed Tomcat connector maxThreads from " + defaultMaxThreads + " to " + MAX_THREADS);
        }
    });
}
 
Example 18
Source File: TomcatContainerCustomizer.java    From pmq with Apache License 2.0 4 votes vote down vote up
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
	MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
	mappings.add("woff", "application/x-font-woff");
	mappings.add("eot", "application/vnd.ms-fontobject");
	mappings.add("ttf", "application/x-font-ttf");
	container.setMimeMappings(mappings);

	if (!(container instanceof TomcatEmbeddedServletContainerFactory)) {
		return;
	}
	if (!environment.containsProperty(TOMCAT_ACCEPTOR_COUNT)) {
		return;
	}
	TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
	tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

		@Override
		public void customize(Connector connector) {
			ProtocolHandler handler = connector.getProtocolHandler();
			if (handler instanceof Http11NioProtocol) {
				Http11NioProtocol http = (Http11NioProtocol) handler;
				acceptCount = soaConfig.getTomcatAcceptCount();
				maxThreads = soaConfig.getTomcatMaxThreads();
				minThreads = soaConfig.getTomcatMinThreads();
				soaConfig.registerChanged(() -> {						
					if (maxThreads != soaConfig.getTomcatMaxThreads()) {
						maxThreads = soaConfig.getTomcatMaxThreads();
						if (maxThreads > 0) {
							http.setMaxThreads(maxThreads);
						} else {
							http.setMaxThreads(200);
						}
					}
					if (minThreads != soaConfig.getTomcatMinThreads()) {
						minThreads = soaConfig.getTomcatMinThreads();
						if (minThreads > 0) {
							http.setMinSpareThreads(minThreads);
						} else {
							http.setMinSpareThreads(10);
						}
					}


				});
				http.setBacklog(acceptCount);
				http.setMaxThreads(maxThreads);
				http.setMinSpareThreads(minThreads);
				logger.info("Setting tomcat accept count to {}", acceptCount);
			}
		}
	});
}
 
Example 19
Source File: Application.java    From mobilecloud-15 with Apache License 2.0 4 votes vote down vote up
@Bean
EmbeddedServletContainerCustomizer containerCustomizer(
		@Value("${keystore.file}") String keystoreFile,
		@Value("${keystore.pass}") final String keystorePass)
		throws Exception {

	
	// This is boiler plate code to setup https on embedded Tomcat
	// with Spring Boot:
	
	final String absoluteKeystoreFile = new File(keystoreFile)
			.getAbsolutePath();

	return new EmbeddedServletContainerCustomizer() {
		@Override
		public void customize(ConfigurableEmbeddedServletContainer container) {
			TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
			tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

				@Override
				public void customize(Connector connector) {
					connector.setPort(8443);
					connector.setSecure(true);
					connector.setScheme("https");

					Http11NioProtocol proto = (Http11NioProtocol) connector
							.getProtocolHandler();
					proto.setSSLEnabled(true);
					
					// If you update the keystore, you need to change
					// these parameters to match the keystore that you generate
					proto.setKeystoreFile(absoluteKeystoreFile);
					proto.setKeystorePass(keystorePass);
					proto.setKeystoreType("JKS");
					proto.setKeyAlias("tomcat");

				}
			});
		}

	};
}
 
Example 20
Source File: Application.java    From mobilecloud-15 with Apache License 2.0 4 votes vote down vote up
@Bean
EmbeddedServletContainerCustomizer containerCustomizer(
		@Value("${keystore.file}") String keystoreFile,
		@Value("${keystore.pass}") final String keystorePass)
		throws Exception {

	
	// This is boiler plate code to setup https on embedded Tomcat
	// with Spring Boot:
	
	final String absoluteKeystoreFile = new File(keystoreFile)
			.getAbsolutePath();

	return new EmbeddedServletContainerCustomizer() {
		@Override
		public void customize(ConfigurableEmbeddedServletContainer container) {
			TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
			tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

				@Override
				public void customize(Connector connector) {
					connector.setPort(8443);
					connector.setSecure(true);
					connector.setScheme("https");

					Http11NioProtocol proto = (Http11NioProtocol) connector
							.getProtocolHandler();
					proto.setSSLEnabled(true);
					
					// If you update the keystore, you need to change
					// these parameters to match the keystore that you generate
					proto.setKeystoreFile(absoluteKeystoreFile);
					proto.setKeystorePass(keystorePass);
					proto.setKeystoreType("JKS");
					proto.setKeyAlias("tomcat");

				}
			});
		}

	};
}