Skip to content

Commit 0242dc0

Browse files
authored
Provide better exception message for invalid PORT env variable (#3083)
- Update javadoc - Update reference documentation Fixes #3070
1 parent a9ca98f commit 0242dc0

File tree

17 files changed

+142
-27
lines changed

17 files changed

+142
-27
lines changed

docs/asciidoc/http-client.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and adds Reactive Streams backpressure.
1616

1717
To connect the `HTTP` client to a given `HTTP` endpoint, you must create and configure a
1818
{javadoc}/reactor/netty/http/client/HttpClient.html[`HttpClient`] instance.
19+
By default, the host is configured for `localhost` and the port is `80`.
1920
The following example shows how to do so:
2021

2122
====
@@ -56,6 +57,8 @@ include::{examplesdir}/address/Application.java[lines=18..33]
5657
<2> Configures the `HTTP` port
5758
====
5859

60+
NOTE: The port can be specified also with *PORT* environment variable.
61+
5962
== Eager Initialization
6063

6164
By default, the initialization of the `HttpClient` resources happens on demand. This means that the `first

docs/asciidoc/tcp-client.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ include::{examplesdir}/address/Application.java[lines=18..33]
4949
<2> Configures the `TCP` port
5050
====
5151

52+
NOTE: The port can be specified also with *PORT* environment variable.
53+
5254
== Eager Initialization
5355

5456
By default, the initialization of the `TcpClient` resources happens on demand. This means that the `connect

docs/asciidoc/udp-client.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ include::{examplesdir}/address/Application.java[lines=18..34]
4848
<2> Configures the `port` to which this client should connect
4949
====
5050

51+
NOTE: The port can be specified also with *PORT* environment variable.
52+
5153
== Eager Initialization
5254

5355
By default, the initialization of the `UdpClient` resources happens on demand. This means that the `connect

docs/asciidoc/udp-server.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ include::{examplesdir}/address/Application.java[lines=18..34]
4848
<2> Configures the `UDP` server port
4949
====
5050

51+
NOTE: The port can be specified also with *PORT* environment variable.
52+
5153
== Eager Initialization
5254

5355
By default, the initialization of the `UdpServer` resources happens on demand. This means that the `bind

reactor-netty-core/src/main/java/reactor/netty/tcp/TcpClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2011-2024 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -275,6 +275,14 @@ public <O> TcpClient option(ChannelOption<O> key, @Nullable O value) {
275275
return super.option(key, value);
276276
}
277277

278+
/**
279+
* The port to which this client should connect.
280+
* If a port is not specified, the default port {@code 12012} is used.
281+
* <p><strong>Note:</strong> The port can be specified also with {@code PORT} environment variable.
282+
*
283+
* @param port the port to connect to
284+
* @return a new {@link TcpClient}
285+
*/
278286
@Override
279287
public TcpClient port(int port) {
280288
return super.port(port);

reactor-netty-core/src/main/java/reactor/netty/tcp/TcpClientConnect.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2021 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2017-2024 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,8 +54,20 @@ protected TcpClient duplicate() {
5454
}
5555

5656
/**
57-
* The default port for reactor-netty servers. Defaults to 12012 but can be tuned via
57+
* The default port for reactor-netty TCP clients. Defaults to 12012 but can be tuned via
5858
* the {@code PORT} <b>environment variable</b>.
5959
*/
60-
static final int DEFAULT_PORT = System.getenv("PORT") != null ? Integer.parseInt(System.getenv("PORT")) : 12012;
60+
static final int DEFAULT_PORT;
61+
static {
62+
int port;
63+
String portStr = null;
64+
try {
65+
portStr = System.getenv("PORT");
66+
port = portStr != null ? Integer.parseInt(portStr) : 12012;
67+
}
68+
catch (NumberFormatException e) {
69+
throw new IllegalArgumentException("Invalid environment variable [PORT=" + portStr + "].", e);
70+
}
71+
DEFAULT_PORT = port;
72+
}
6173
}

reactor-netty-core/src/main/java/reactor/netty/tcp/TcpServer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2011-2024 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,6 +146,13 @@ public TcpServer noSSL() {
146146
return this;
147147
}
148148

149+
/**
150+
* The port to which this server should bind.
151+
* If a port is not specified, the system picks up an ephemeral port.
152+
*
153+
* @param port The port to bind to.
154+
* @return a new {@link TcpServer}
155+
*/
149156
@Override
150157
public TcpServer port(int port) {
151158
return super.port(port);

reactor-netty-core/src/main/java/reactor/netty/udp/UdpClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ public final <O> UdpClient option(ChannelOption<O> key, @Nullable O value) {
150150
return super.option(key, value);
151151
}
152152

153+
/**
154+
* The port to which this client should connect.
155+
* If a port is not specified, the default port {@code 12012} is used.
156+
* <p><strong>Note:</strong> The port can be specified also with {@code PORT} environment variable.
157+
*
158+
* @param port the port to connect to
159+
* @return a new {@link UdpClient} reference
160+
*/
153161
@Override
154162
public final UdpClient port(int port) {
155163
return super.port(port);

reactor-netty-core/src/main/java/reactor/netty/udp/UdpClientConnect.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2021 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2017-2024 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,8 +56,20 @@ protected UdpClient duplicate() {
5656
}
5757

5858
/**
59-
* The default port for reactor-netty servers. Defaults to 12012 but can be tuned via
59+
* The default port for reactor-netty UDP clients. Defaults to 12012 but can be tuned via
6060
* the {@code PORT} <b>environment variable</b>.
6161
*/
62-
static final int DEFAULT_PORT = System.getenv("PORT") != null ? Integer.parseInt(System.getenv("PORT")) : 12012;
62+
static final int DEFAULT_PORT;
63+
static {
64+
int port;
65+
String portStr = null;
66+
try {
67+
portStr = System.getenv("PORT");
68+
port = portStr != null ? Integer.parseInt(portStr) : 12012;
69+
}
70+
catch (NumberFormatException e) {
71+
throw new IllegalArgumentException("Invalid environment variable [PORT=" + portStr + "].", e);
72+
}
73+
DEFAULT_PORT = port;
74+
}
6375
}

reactor-netty-core/src/main/java/reactor/netty/udp/UdpServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2011-2024 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -217,6 +217,8 @@ public final <O> UdpServer option(ChannelOption<O> key, @Nullable O value) {
217217

218218
/**
219219
* The port to which this server should bind.
220+
* If a port is not specified, the default port {@code 12012} is used.
221+
* <p><strong>Note:</strong> The port can be specified also with {@code PORT} environment variable.
220222
*
221223
* @param port The port to bind to.
222224
* @return a new {@link UdpServer} reference

0 commit comments

Comments
 (0)