diff --git a/build.gradle b/build.gradle index 09c3987c..3980fcbe 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ dependencies { } publishing { - def coreVersion = '0.3.2'; + def coreVersion = '0.4.0'; publications { maven(MavenPublication) { groupId = 'com.redhat.rhjmc' diff --git a/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnection.java b/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnection.java index 73e83b40..d4c45de2 100644 --- a/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnection.java +++ b/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnection.java @@ -4,6 +4,7 @@ import com.redhat.rhjmc.containerjfr.core.sys.Clock; import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter; + import org.openjdk.jmc.rjmx.IConnectionHandle; import org.openjdk.jmc.rjmx.IConnectionListener; import org.openjdk.jmc.rjmx.internal.DefaultConnectionHandle; @@ -20,21 +21,19 @@ public class JFRConnection implements AutoCloseable { public static final int DEFAULT_PORT = 9091; private final ClientWriter cw; - private final Clock clock; private final JMXServiceURL url; private final RJMXConnection rjmxConnection; private final IConnectionHandle handle; private final IFlightRecorderService service; - JFRConnection(ClientWriter cw, Clock clock, String host, int port) throws Exception { - this(cw, clock, new JMXServiceURL(String.format(URL_FORMAT, host, port))); + JFRConnection(ClientWriter cw, String host, int port) throws Exception { + this(cw, new JMXServiceURL(String.format(URL_FORMAT, host, port))); } - JFRConnection(ClientWriter cw, Clock clock, JMXServiceURL url) throws Exception { + JFRConnection(ClientWriter cw, JMXServiceURL url) throws Exception { this.cw = cw; - this.clock = clock; this.url = url; - this.rjmxConnection = attemptConnect(url, 0); + this.rjmxConnection = attemptConnect(url); this.handle = new DefaultConnectionHandle(rjmxConnection, "RJMX Connection", new IConnectionListener[0]); this.service = new FlightRecorderServiceFactory().getServiceInstance(handle); } @@ -68,31 +67,21 @@ public void close() { this.disconnect(); } - private RJMXConnection attemptConnect(JMXServiceURL url, int maxRetry) throws Exception { + private RJMXConnection attemptConnect(JMXServiceURL url) throws Exception { JMXConnectionDescriptor cd = new JMXConnectionDescriptor( url, new InMemoryCredentials(null, null)); ServerDescriptor sd = new ServerDescriptor(null, "Container", null); - int attempts = 0; - while (true) { - try { - RJMXConnection conn = new RJMXConnection(cd, sd, JFRConnection::failConnection); - if (!conn.connect()) { - failConnection(); - } - return conn; - } catch (Exception e) { - attempts++; - cw.println(String.format("Connection attempt %d failed.", attempts)); - if (attempts >= maxRetry) { - cw.println("Too many failed connections. Aborting."); - throw e; - } else { - cw.println(e); - } - clock.sleep(500); + try { + RJMXConnection conn = new RJMXConnection(cd, sd, JFRConnection::failConnection); + if (!conn.connect()) { + failConnection(); } + return conn; + } catch (Exception e) { + cw.println("connection attempt failed."); + throw e; } } diff --git a/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkit.java b/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkit.java index f8669e26..116befd5 100644 --- a/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkit.java +++ b/src/main/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkit.java @@ -2,21 +2,18 @@ import javax.management.remote.JMXServiceURL; -import com.redhat.rhjmc.containerjfr.core.sys.Clock; import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter; public class JFRConnectionToolkit { private final ClientWriter cw; - private final Clock clock; - public JFRConnectionToolkit(ClientWriter cw, Clock clock) { + public JFRConnectionToolkit(ClientWriter cw) { this.cw = cw; - this.clock = clock; } public JFRConnection connect(JMXServiceURL url) throws Exception { - return new JFRConnection(cw, clock, url); + return new JFRConnection(cw, url); } public JFRConnection connect(String host) throws Exception { @@ -24,6 +21,6 @@ public JFRConnection connect(String host) throws Exception { } public JFRConnection connect(String host, int port) throws Exception { - return new JFRConnection(cw, clock, host, port); + return new JFRConnection(cw, host, port); } } diff --git a/src/test/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkitTest.java b/src/test/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkitTest.java index 2919fa97..d4a8a68b 100644 --- a/src/test/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkitTest.java +++ b/src/test/java/com/redhat/rhjmc/containerjfr/core/net/JFRConnectionToolkitTest.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; +import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -9,19 +11,15 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.openjdk.jmc.rjmx.internal.WrappedConnectionException; -import com.redhat.rhjmc.containerjfr.core.sys.Clock; -import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter; - @ExtendWith(MockitoExtension.class) class JFRConnectionToolkitTest { JFRConnectionToolkit toolkit; @Mock ClientWriter cw; - @Mock Clock clock; @BeforeEach void setup() { - toolkit = new JFRConnectionToolkit(cw, clock); + toolkit = new JFRConnectionToolkit(cw); } @Test