Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyWebSocketServerConfig.java
Go to the documentation of this file.
1package dev.rafex.ether.websocket.jetty12;
2
3/*-
4 * #%L
5 * ether-websocket-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.util.Map;
30
31/**
32 * Immutable configuration for the Jetty WebSocket server.
33 *
34 * <p>Encapsulates port, thread pool sizing and idle timeout. Use
35 * {@link #fromEnv()} to populate the values from environment variables.</p>
36 *
37 * @param port the HTTP port the server binds to
38 * @param minThreads minimum threads in the Jetty thread pool
39 * @param maxThreads maximum threads in the Jetty thread pool
40 * @param idleTimeoutMs idle timeout in milliseconds for the thread pool
41 * @param threadPoolName the name prefix for Jetty thread pool threads
42 */
43public record JettyWebSocketServerConfig(int port, int minThreads, int maxThreads, int idleTimeoutMs,
44 String threadPoolName) {
45
46 /**
47 * Creates a configuration from the current process environment.
48 *
49 * <p>Delegates to {@link #fromEnv(Map)} using {@link System#getenv()}.</p>
50 *
51 * @return a config populated from environment variables
52 * @see #fromEnv(Map)
53 */
54 public static JettyWebSocketServerConfig fromEnv() {
55 return fromEnv(System.getenv());
56 }
57
58 /**
59 * Creates a configuration from the given environment map.
60 *
61 * <p>Recognised keys and their defaults:</p>
62 * <ul>
63 * <li>{@code HTTP_PORT} — default {@code 8080}</li>
64 * <li>{@code HTTP_MIN_THREADS} — default {@code 4}</li>
65 * <li>{@code HTTP_MAX_THREADS} — default {@code 32}</li>
66 * <li>{@code HTTP_IDLE_TIMEOUT_MS} — default {@code 30000}</li>
67 * <li>{@code HTTP_POOL_NAME} — default {@code "ether-websocket"}</li>
68 * </ul>
69 *
70 * @param env the environment map to read values from
71 * @return a config populated from the map
72 */
73 public static JettyWebSocketServerConfig fromEnv(final Map<String, String> env) {
74 return new JettyWebSocketServerConfig(parseInt(env.get("HTTP_PORT"), 8080),
75 parseInt(env.get("HTTP_MIN_THREADS"), 4), parseInt(env.get("HTTP_MAX_THREADS"), 32),
76 parseInt(env.get("HTTP_IDLE_TIMEOUT_MS"), 30_000),
77 env.getOrDefault("HTTP_POOL_NAME", "ether-websocket"));
78 }
79
80 /**
81 * Parses an integer value, falling back to the default on {@code null},
82 * blank or non-numeric input.
83 *
84 * @param value the raw string to parse
85 * @param fallback the value to use when parsing fails
86 * @return the parsed integer, or the fallback
87 */
88 private static int parseInt(final String value, final int fallback) {
89 if (value == null || value.isBlank()) {
90 return fallback;
91 }
92 try {
93 return Integer.parseInt(value.trim());
94 } catch (final NumberFormatException e) {
95 return fallback;
96 }
97 }
98}
record JettyWebSocketServerConfig(int port, int minThreads, int maxThreads, int idleTimeoutMs, String threadPoolName)
Immutable configuration for the Jetty WebSocket server.