Skip to content

Commit f59428d

Browse files
committed
Merge branch '2.x'
2 parents d802f2c + f6c9134 commit f59428d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/_DefaultConnectionContext.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.annotation.PostConstruct;
3838
import javax.annotation.PreDestroy;
3939
import javax.management.JMException;
40+
import javax.management.MalformedObjectNameException;
4041
import javax.management.ObjectName;
4142
import java.lang.management.ManagementFactory;
4243
import java.time.Duration;
@@ -56,6 +57,8 @@
5657
@Value.Immutable
5758
abstract class _DefaultConnectionContext implements ConnectionContext {
5859

60+
private static final int DEFAULT_PORT = 443;
61+
5962
private static final int RECEIVE_BUFFER_SIZE = 10 * 1024 * 1024;
6063

6164
private static final int SEND_BUFFER_SIZE = 10 * 1024 * 1024;
@@ -69,6 +72,16 @@ abstract class _DefaultConnectionContext implements ConnectionContext {
6972
public final void dispose() {
7073
getConnectionPool().ifPresent(PoolResources::dispose);
7174
getThreadPool().dispose();
75+
76+
try {
77+
ObjectName name = getByteBufAllocatorObjectName();
78+
79+
if (ManagementFactory.getPlatformMBeanServer().isRegistered(name)) {
80+
ManagementFactory.getPlatformMBeanServer().unregisterMBean(name);
81+
}
82+
} catch (JMException e) {
83+
this.logger.error("Unable to register ByteBufAllocator MBean", e);
84+
}
7285
}
7386

7487
@Override
@@ -149,7 +162,6 @@ public Mono<Void> trust(String host, int port) {
149162
/**
150163
* The hostname of the API root. Typically something like {@code api.run.pivotal.io}.
151164
*/
152-
@Nullable
153165
abstract String getApiHost();
154166

155167
/**
@@ -225,11 +237,21 @@ LoopResources getThreadPool() {
225237
@PostConstruct
226238
void monitorByteBufAllocator() {
227239
try {
228-
ManagementFactory.getPlatformMBeanServer()
229-
.registerMBean(new ByteBufAllocatorMetricProviderWrapper(PooledByteBufAllocator.DEFAULT), ObjectName.getInstance("org.cloudfoundry.reactor:type=ByteBufAllocator"));
240+
ObjectName name = getByteBufAllocatorObjectName();
241+
242+
if (ManagementFactory.getPlatformMBeanServer().isRegistered(name)) {
243+
this.logger.warn("MBean '{}' is already registered and will be removed. You should only have a single DefaultConnectionContext per endpoint.", name);
244+
ManagementFactory.getPlatformMBeanServer().unregisterMBean(name);
245+
}
246+
247+
ManagementFactory.getPlatformMBeanServer().registerMBean(new ByteBufAllocatorMetricProviderWrapper(PooledByteBufAllocator.DEFAULT), name);
230248
} catch (JMException e) {
231249
this.logger.error("Unable to register ByteBufAllocator MBean", e);
232250
}
233251
}
234252

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

cloudfoundry-client-reactor/src/test/java/org/cloudfoundry/reactor/DefaultConnectionContextTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,21 @@ public void getInfo() throws Exception {
6868
.verify(Duration.ofSeconds(5));
6969
}
7070

71+
@Test
72+
public void multipleInstances() {
73+
DefaultConnectionContext first = DefaultConnectionContext.builder()
74+
.apiHost("test-host")
75+
.build();
76+
77+
DefaultConnectionContext second = DefaultConnectionContext.builder()
78+
.apiHost("test-host")
79+
.build();
80+
81+
first.monitorByteBufAllocator();
82+
second.monitorByteBufAllocator();
83+
84+
first.dispose();
85+
second.dispose();
86+
}
87+
7188
}

0 commit comments

Comments
 (0)