Skip to content

Commit f54a00c

Browse files
Merge pull request #973 from fbacchella/keepalive_java11
No need to wrap SO_KEEPALIVE settings with Java 11.
2 parents 016b28f + 1afe2c5 commit f54a00c

File tree

2 files changed

+19
-74
lines changed

2 files changed

+19
-74
lines changed

src/main/java/zmq/io/net/tcp/TcpUtils.java

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,13 @@
88
import java.nio.channels.SelectableChannel;
99
import java.nio.channels.SocketChannel;
1010

11+
import jdk.net.ExtendedSocketOptions;
1112
import zmq.ZError;
1213
import zmq.io.net.Address;
1314
import zmq.util.Utils;
1415

1516
public class TcpUtils
1617
{
17-
public static final boolean WITH_EXTENDED_KEEPALIVE = SocketOptionsProvider.WITH_EXTENDED_KEEPALIVE;
18-
19-
@SuppressWarnings("unchecked")
20-
private static final class SocketOptionsProvider
21-
{
22-
// Wrapped in an inner class, to avoid the @SuppressWarnings for the whole class
23-
private static final SocketOption<Integer> TCP_KEEPCOUNT;
24-
private static final SocketOption<Integer> TCP_KEEPIDLE;
25-
private static final SocketOption<Integer> TCP_KEEPINTERVAL;
26-
private static final boolean WITH_EXTENDED_KEEPALIVE;
27-
static {
28-
SocketOption<Integer> tryCount = null;
29-
SocketOption<Integer> tryIdle = null;
30-
SocketOption<Integer> tryInterval = null;
31-
32-
boolean extendedKeepAlive = false;
33-
try {
34-
Class<?> eso = TcpUtils.class.getClassLoader().loadClass("jdk.net.ExtendedSocketOptions");
35-
tryCount = (SocketOption<Integer>) eso.getField("TCP_KEEPCOUNT").get(null);
36-
tryIdle = (SocketOption<Integer>) eso.getField("TCP_KEEPIDLE").get(null);
37-
tryInterval = (SocketOption<Integer>) eso.getField("TCP_KEEPINTERVAL").get(null);
38-
extendedKeepAlive = true;
39-
}
40-
catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
41-
// If failing, will keep extendedKeepAlive to false
42-
}
43-
TCP_KEEPCOUNT = tryCount;
44-
TCP_KEEPIDLE = tryIdle;
45-
TCP_KEEPINTERVAL = tryInterval;
46-
WITH_EXTENDED_KEEPALIVE = extendedKeepAlive;
47-
}
48-
}
49-
5018
private TcpUtils()
5119
{
5220
}
@@ -67,15 +35,15 @@ public static void tuneTcpKeepalives(Channel channel, int tcpKeepAlive, int tcpK
6735
if (channel instanceof SocketChannel) {
6836
setOption(channel, StandardSocketOptions.SO_KEEPALIVE, tcpKeepAlive == 1);
6937
}
70-
if (WITH_EXTENDED_KEEPALIVE && tcpKeepAlive == 1) {
38+
if (tcpKeepAlive == 1) {
7139
if (tcpKeepAliveCnt > 0) {
72-
setOption(channel, SocketOptionsProvider.TCP_KEEPCOUNT, tcpKeepAliveCnt);
40+
setOption(channel, ExtendedSocketOptions.TCP_KEEPCOUNT, tcpKeepAliveCnt);
7341
}
7442
if (tcpKeepAliveIdle > 0) {
75-
setOption(channel, SocketOptionsProvider.TCP_KEEPIDLE, tcpKeepAliveIdle);
43+
setOption(channel, ExtendedSocketOptions.TCP_KEEPIDLE, tcpKeepAliveIdle);
7644
}
7745
if (tcpKeepAliveIntvl > 0) {
78-
setOption(channel, SocketOptionsProvider.TCP_KEEPINTERVAL, tcpKeepAliveIntvl);
46+
setOption(channel, ExtendedSocketOptions.TCP_KEEPINTERVAL, tcpKeepAliveIntvl);
7947
}
8048
}
8149
}
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,33 @@
11
package zmq.io.net.tcp;
22

33
import java.io.IOException;
4-
import java.lang.reflect.InvocationTargetException;
5-
import java.lang.reflect.Method;
6-
import java.net.ServerSocket;
7-
import java.net.Socket;
8-
import java.net.SocketOption;
94
import java.nio.channels.ServerSocketChannel;
105
import java.nio.channels.SocketChannel;
116

127
import org.junit.Assert;
138
import org.junit.Test;
149

15-
import zmq.Options;
10+
import jdk.net.ExtendedSocketOptions;
1611

1712
public class KeepAliveTest
1813
{
1914
@Test
2015
public void testConsistent() throws IOException
2116
{
22-
try {
23-
int javaspec = Integer.parseInt(System.getProperty("java.specification.version"));
24-
// Test fails for jvm lest that 13
25-
if (javaspec >= 13) {
26-
Class<?> eso = Options.class.getClassLoader().loadClass("jdk.net.ExtendedSocketOptions");
27-
SocketOption<Integer> count = (SocketOption<Integer>) eso.getField("TCP_KEEPCOUNT").get(null);
28-
SocketOption<Integer> idle = (SocketOption<Integer>) eso.getField("TCP_KEEPIDLE").get(null);
29-
SocketOption<Integer> interval = (SocketOption<Integer>) eso.getField("TCP_KEEPINTERVAL").get(null);
30-
Method socketgetOption = Socket.class.getMethod("getOption", SocketOption.class);
31-
Method serversocketgetOption = ServerSocket.class.getMethod("getOption", SocketOption.class);
32-
Assert.assertTrue(TcpUtils.WITH_EXTENDED_KEEPALIVE);
17+
int javaspec = Integer.parseInt(System.getProperty("java.specification.version"));
18+
// Test fails for jvm less than 13
19+
if (javaspec >= 13) {
20+
SocketChannel sc = SocketChannel.open();
21+
TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4);
22+
Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT));
23+
Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE));
24+
Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL));
3325

34-
SocketChannel sc = SocketChannel.open();
35-
TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4);
36-
Assert.assertEquals(2, socketgetOption.invoke(sc.socket(), count));
37-
Assert.assertEquals(3, socketgetOption.invoke(sc.socket(), idle));
38-
Assert.assertEquals(4, socketgetOption.invoke(sc.socket(), interval));
39-
40-
ServerSocketChannel ssc = ServerSocketChannel.open();
41-
TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4);
42-
Assert.assertEquals(2, serversocketgetOption.invoke(ssc.socket(), count));
43-
Assert.assertEquals(3, serversocketgetOption.invoke(ssc.socket(), idle));
44-
Assert.assertEquals(4, serversocketgetOption.invoke(ssc.socket(), interval));
45-
}
46-
}
47-
catch (NumberFormatException ex) {
48-
// java 1.8, not a problem as keepalive is not handled
26+
ServerSocketChannel ssc = ServerSocketChannel.open();
27+
TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4);
28+
Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT));
29+
Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE));
30+
Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL));
4931
}
50-
catch (NoSuchMethodException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException |
51-
InvocationTargetException e) {
52-
// Not defined, just skip the test
53-
}
54-
5532
}
5633
}

0 commit comments

Comments
 (0)