or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

auto-configuration.mdconfiguration-properties.mdindex.mdserver-customization.mdweb-server-factories.md

server-customization.mddocs/

0

# Server Customization

1

2

Functional interfaces for customizing various aspects of the Tomcat server including connectors, contexts, and protocol handlers. These customizers enable fine-grained control over Tomcat configuration while integrating seamlessly with Spring Boot's auto-configuration system.

3

4

## Capabilities

5

6

### TomcatConnectorCustomizer

7

8

Functional interface for customizing Tomcat connectors, which handle incoming HTTP connections and protocol processing.

9

10

```java { .api }

11

/**

12

* Functional interface for customizing Tomcat connectors

13

*/

14

@FunctionalInterface

15

public interface TomcatConnectorCustomizer {

16

17

/**

18

* Customize the given Tomcat connector

19

* @param connector the Tomcat connector to customize

20

*/

21

void customize(Connector connector);

22

}

23

```

24

25

**Usage Examples:**

26

27

```java

28

import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;

29

import org.springframework.context.annotation.Bean;

30

import org.springframework.context.annotation.Configuration;

31

32

@Configuration

33

public class ConnectorConfig {

34

35

@Bean

36

public TomcatConnectorCustomizer httpsConnectorCustomizer() {

37

return connector -> {

38

connector.setScheme("https");

39

connector.setSecure(true);

40

connector.setPort(8443);

41

connector.setAttribute("keyAlias", "tomcat");

42

connector.setAttribute("keystoreFile", "/path/to/keystore.p12");

43

connector.setAttribute("keystoreType", "PKCS12");

44

connector.setAttribute("keystorePass", "password");

45

};

46

}

47

48

@Bean

49

public TomcatConnectorCustomizer performanceCustomizer() {

50

return connector -> {

51

// Connection pool settings

52

connector.setAttribute("maxThreads", "300");

53

connector.setAttribute("minSpareThreads", "20");

54

connector.setAttribute("maxConnections", "10000");

55

connector.setAttribute("acceptCount", "200");

56

57

// Timeout settings

58

connector.setAttribute("connectionTimeout", "30000");

59

connector.setAttribute("keepAliveTimeout", "15000");

60

connector.setAttribute("maxKeepAliveRequests", "100");

61

62

// HTTP settings

63

connector.setAttribute("compression", "on");

64

connector.setAttribute("compressionMinSize", "2048");

65

connector.setAttribute("compressableMimeType",

66

"text/html,text/xml,text/plain,text/css,application/json,application/javascript");

67

};

68

}

69

}

70

```

71

72

### TomcatContextCustomizer

73

74

Functional interface for customizing Tomcat contexts, which represent individual web applications.

75

76

```java { .api }

77

/**

78

* Functional interface for customizing Tomcat contexts

79

*/

80

@FunctionalInterface

81

public interface TomcatContextCustomizer {

82

83

/**

84

* Customize the given Tomcat context

85

* @param context the Tomcat context to customize

86

*/

87

void customize(Context context);

88

}

89

```

90

91

**Usage Examples:**

92

93

```java

94

@Configuration

95

public class ContextConfig {

96

97

@Bean

98

public TomcatContextCustomizer sessionCustomizer() {

99

return context -> {

100

// Session configuration

101

context.setSessionTimeout(30); // 30 minutes

102

context.setCookies(true);

103

context.setUseHttpOnly(true);

104

105

// Security settings

106

context.getManager().setSecureRandomClass("java.security.SecureRandom");

107

context.getManager().setSecureRandomAlgorithm("SHA1PRNG");

108

};

109

}

110

111

@Bean

112

public TomcatContextCustomizer staticResourceCustomizer() {

113

return context -> {

114

// Static resource caching

115

context.setCachingAllowed(true);

116

context.setCacheMaxSize(10240); // 10MB

117

context.setCacheTTL(86400000); // 24 hours

118

119

// MIME type mappings

120

context.addMimeMapping("woff", "application/font-woff");

121

context.addMimeMapping("woff2", "application/font-woff2");

122

};

123

}

124

125

@Bean

126

public TomcatContextCustomizer errorPageCustomizer() {

127

return context -> {

128

// Custom error pages

129

ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");

130

ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");

131

context.addErrorPage(error404);

132

context.addErrorPage(error500);

133

};

134

}

135

}

136

```

137

138

### TomcatProtocolHandlerCustomizer

139

140

Generic functional interface for customizing Tomcat protocol handlers, which manage the low-level protocol processing.

141

142

```java { .api }

143

/**

144

* Generic functional interface for customizing Tomcat protocol handlers

145

* @param <T> the type of protocol handler to customize

146

*/

147

@FunctionalInterface

148

public interface TomcatProtocolHandlerCustomizer<T> {

149

150

/**

151

* Customize the given protocol handler

152

* @param protocolHandler the protocol handler to customize

153

*/

154

void customize(T protocolHandler);

155

}

156

```

157

158

**Usage Examples:**

159

160

```java

161

@Configuration

162

public class ProtocolConfig {

163

164

@Bean

165

public TomcatProtocolHandlerCustomizer<Http11NioProtocol> nioProtocolCustomizer() {

166

return protocolHandler -> {

167

// NIO connector settings

168

protocolHandler.setAcceptorThreadCount(2);

169

protocolHandler.setMaxConnections(10000);

170

protocolHandler.setConnectionTimeout(30000);

171

172

// Buffer settings

173

protocolHandler.setSocketBuffer(65536);

174

protocolHandler.setAppReadBufSize(8192);

175

protocolHandler.setAppWriteBufSize(8192);

176

177

// Keep-alive settings

178

protocolHandler.setKeepAliveTimeout(15000);

179

protocolHandler.setMaxKeepAliveRequests(100);

180

};

181

}

182

183

@Bean

184

public TomcatProtocolHandlerCustomizer<AbstractProtocol<?>> genericProtocolCustomizer() {

185

return protocolHandler -> {

186

// Generic protocol handler settings

187

protocolHandler.setTcpNoDelay(true);

188

protocolHandler.setSoLinger(-1);

189

protocolHandler.setSoTimeout(30000);

190

191

// Thread pool configuration

192

if (protocolHandler.getExecutor() instanceof ThreadPoolExecutor) {

193

ThreadPoolExecutor executor = (ThreadPoolExecutor) protocolHandler.getExecutor();

194

executor.setKeepAliveTime(60, TimeUnit.SECONDS);

195

executor.allowCoreThreadTimeOut(true);

196

}

197

};

198

}

199

}

200

```

201

202

### Built-in Customizers

203

204

Spring Boot provides several built-in customizers that are automatically applied:

205

206

#### SslConnectorCustomizer

207

208

Configures SSL/TLS settings on Tomcat connectors based on `ServerProperties.Ssl` configuration.

209

210

```java { .api }

211

/**

212

* Customizes Tomcat connectors for SSL configuration

213

*/

214

public class SslConnectorCustomizer implements TomcatConnectorCustomizer {

215

216

/** Constructor with SSL configuration */

217

public SslConnectorCustomizer(Ssl ssl, SslStoreProvider sslStoreProvider);

218

219

/** Applies SSL configuration to the connector */

220

public void customize(Connector connector);

221

}

222

```

223

224

#### CompressionConnectorCustomizer

225

226

Configures HTTP compression settings on Tomcat connectors based on `ServerProperties.Compression` configuration.

227

228

```java { .api }

229

/**

230

* Customizes Tomcat connectors for HTTP compression

231

*/

232

public class CompressionConnectorCustomizer implements TomcatConnectorCustomizer {

233

234

/** Constructor with compression configuration */

235

public CompressionConnectorCustomizer(Compression compression);

236

237

/** Applies compression settings to the connector */

238

public void customize(Connector connector);

239

}

240

```

241

242

## Customizer Registration

243

244

Customizers are automatically discovered and applied by Spring Boot's auto-configuration:

245

246

### Automatic Discovery

247

248

All customizer beans in the application context are automatically applied:

249

250

```java

251

@Bean

252

public TomcatConnectorCustomizer myCustomizer() {

253

return connector -> {

254

// Custom configuration

255

};

256

}

257

```

258

259

### Factory Method Registration

260

261

Customizers can be added directly to web server factories:

262

263

```java

264

@Bean

265

public TomcatServletWebServerFactory tomcatFactory() {

266

TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

267

268

factory.addConnectorCustomizers(connector -> {

269

// Connector customization

270

});

271

272

factory.addContextCustomizers(context -> {

273

// Context customization

274

});

275

276

factory.addProtocolHandlerCustomizers((Http11NioProtocol protocol) -> {

277

// Protocol handler customization

278

});

279

280

return factory;

281

}

282

```

283

284

### Execution Order

285

286

Customizers are applied in the following order:

287

288

1. **Property-based customizers** - Applied from `server.tomcat.*` properties

289

2. **Built-in customizers** - SSL, compression, etc.

290

3. **User-defined customizers** - Custom beans discovered in application context

291

4. **Factory-specific customizers** - Added directly to factory instances

292

293

Within each category, customizers are ordered using Spring's `@Order` annotation or by implementing `Ordered` interface.

294

295

## Advanced Customization Patterns

296

297

### Conditional Customization

298

299

```java

300

@Configuration

301

public class ConditionalTomcatConfig {

302

303

@Bean

304

@ConditionalOnProperty("app.ssl.enabled")

305

public TomcatConnectorCustomizer sslCustomizer() {

306

return connector -> {

307

// SSL configuration

308

};

309

}

310

311

@Bean

312

@Profile("production")

313

public TomcatProtocolHandlerCustomizer<Http11NioProtocol> productionCustomizer() {

314

return protocol -> {

315

// Production-specific settings

316

};

317

}

318

}

319

```

320

321

### Multi-Connector Setup

322

323

```java

324

@Bean

325

public TomcatServletWebServerFactory multiConnectorFactory() {

326

TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

327

328

// Additional HTTP connector

329

Connector httpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

330

httpConnector.setPort(8080);

331

httpConnector.setScheme("http");

332

333

// Additional HTTPS connector

334

Connector httpsConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

335

httpsConnector.setPort(8443);

336

httpsConnector.setScheme("https");

337

httpsConnector.setSecure(true);

338

339

factory.addAdditionalTomcatConnectors(httpConnector, httpsConnector);

340

341

return factory;

342

}