Skip to content

Commit

Permalink
Remove unused connection retry parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Oct 10, 2019
1 parent c9fcf61 commit d93e50f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
}

publishing {
def coreVersion = '0.3.2';
def coreVersion = '0.4.0';
publications {
maven(MavenPublication) {
groupId = 'com.redhat.rhjmc'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

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 {
return connect(host, JFRConnection.DEFAULT_PORT);
}

public JFRConnection connect(String host, int port) throws Exception {
return new JFRConnection(cw, clock, host, port);
return new JFRConnection(cw, host, port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

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;
import org.mockito.Mock;
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
Expand Down

0 comments on commit d93e50f

Please sign in to comment.