Skip to content

Commit

Permalink
Merge branch '836-jmx-collision' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nebhale committed Nov 28, 2017
2 parents d46c067 + 147e0f2 commit f6c9134
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.management.JMException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.time.Duration;
Expand All @@ -57,6 +58,8 @@
@Value.Immutable
abstract class _DefaultConnectionContext implements ConnectionContext {

private static final int DEFAULT_PORT = 443;

private static final int RECEIVE_BUFFER_SIZE = 10 * 1024 * 1024;

private static final int SEND_BUFFER_SIZE = 10 * 1024 * 1024;
Expand All @@ -70,6 +73,16 @@ abstract class _DefaultConnectionContext implements ConnectionContext {
public final void dispose() {
getConnectionPool().ifPresent(PoolResources::dispose);
getThreadPool().dispose();

try {
ObjectName name = getByteBufAllocatorObjectName();

if (ManagementFactory.getPlatformMBeanServer().isRegistered(name)) {
ManagementFactory.getPlatformMBeanServer().unregisterMBean(name);
}
} catch (JMException e) {
this.logger.error("Unable to register ByteBufAllocator MBean", e);
}
}

/**
Expand Down Expand Up @@ -146,7 +159,6 @@ public Mono<Void> trust(String host, int port) {
/**
* The hostname of the API root. Typically something like {@code api.run.pivotal.io}.
*/
@Nullable
abstract String getApiHost();

/**
Expand Down Expand Up @@ -222,11 +234,21 @@ LoopResources getThreadPool() {
@PostConstruct
void monitorByteBufAllocator() {
try {
ManagementFactory.getPlatformMBeanServer()
.registerMBean(new ByteBufAllocatorMetricProviderWrapper(PooledByteBufAllocator.DEFAULT), ObjectName.getInstance("org.cloudfoundry.reactor:type=ByteBufAllocator"));
ObjectName name = getByteBufAllocatorObjectName();

if (ManagementFactory.getPlatformMBeanServer().isRegistered(name)) {
this.logger.warn("MBean '{}' is already registered and will be removed. You should only have a single DefaultConnectionContext per endpoint.", name);
ManagementFactory.getPlatformMBeanServer().unregisterMBean(name);
}

ManagementFactory.getPlatformMBeanServer().registerMBean(new ByteBufAllocatorMetricProviderWrapper(PooledByteBufAllocator.DEFAULT), name);
} catch (JMException e) {
this.logger.error("Unable to register ByteBufAllocator MBean", e);
}
}

private ObjectName getByteBufAllocatorObjectName() throws MalformedObjectNameException {
return ObjectName.getInstance(String.format("org.cloudfoundry.reactor:type=ByteBufAllocator,endpoint=%s/%d", getApiHost(), getPort().orElse(DEFAULT_PORT)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ public void getInfo() throws Exception {
.verify(Duration.ofSeconds(5));
}

@Test
public void multipleInstances() {
DefaultConnectionContext first = DefaultConnectionContext.builder()
.apiHost("test-host")
.build();

DefaultConnectionContext second = DefaultConnectionContext.builder()
.apiHost("test-host")
.build();

first.monitorByteBufAllocator();
second.monitorByteBufAllocator();

first.dispose();
second.dispose();
}

}

0 comments on commit f6c9134

Please sign in to comment.